diff --git a/23/1.pl b/23/1.pl new file mode 100644 index 0000000..2fb76aa --- /dev/null +++ b/23/1.pl @@ -0,0 +1,52 @@ +use 5.20.0; + +my @commands = <>; + +my $i = 0; + +my %reg; +my $last_played; + +use experimental qw/ signatures /; + +sub valof($x) { + $x =~ /\d/ ? $x : $reg{$x}; +} + +my $vr = qr/(-?\d+|.)/; + +my $times; + +use DDP; + +my $t; +while() { + last if $i < 0 or $i > $#commands; + $_ = $commands[$i++]; +# say $_; + + + if( /set (.) $vr/ ) { + $reg{$1} = valof($2); + } + elsif( /sub (.) $vr/ ) { + $reg{$1} -= valof($2); + } + elsif( /mul (.) $vr/ ) { + $reg{$1} *= valof($2); + $times++; + die if $times > 10_000; +# say $times; + } + elsif( /jnz (.) $vr/ ) { + $i += -1 + valof($2) if valof($1) != 0; + } + else { die "wut? $_"; } + +# say $_; +# p %reg ;#unless $t++ % 10_000; + # my $dummy = ; + +} + +say $times; diff --git a/23/2.pl b/23/2.pl new file mode 100644 index 0000000..df13281 --- /dev/null +++ b/23/2.pl @@ -0,0 +1,60 @@ +use 5.20.0; + +while(<>) { + s/\b[a-h]\b/\$$&/g; + s/sub (\S+) (\S+)/$1 -= $2;/; + s/set (\S+) (\S+)/$1 = $2;/; + s/mul (\S+) (\S+)/$1 *= $2;/; + s/jnz (\S+) (\S+)/"goto L" . ($. + $2) . " if $1;"/e; + s/^/L${.}: /; + print; +} + +__END__ + +my $i = 0; + +my %reg = ( a => 1); +my $last_played; + +use experimental qw/ signatures /; + +sub valof($x) { + $x =~ /\d/ ? $x : $reg{$x}; +} + +my $vr = qr/(-?\d+|.)/; + +my $times; + +use DDP; + +my $t; +while() { + last if $i < 0 or $i > $#commands; + $_ = $commands[$i++]; +# say $_; + + + if( /set (.) $vr/ ) { + $reg{$1} = valof($2); + } + elsif( /sub (.) $vr/ ) { + $reg{$1} -= valof($2); + } + elsif( /mul (.) $vr/ ) { + $reg{$1} *= valof($2); + } + elsif( /jnz (.) $vr/ ) { + $i += -1 + valof($2) if valof($1) != 0; + } + else { die "wut? $_"; } + + say $i; +# say $_; + p %reg unless $t++ % 10_000; + # my $dummy = ; + +} + +say $reg{h}; diff --git a/23/2b.pl b/23/2b.pl new file mode 100644 index 0000000..582955c --- /dev/null +++ b/23/2b.pl @@ -0,0 +1,32 @@ +L1: $b = 79; +L2: $c = $b; +L3: goto L5 if $a; +L4: goto L9 if 1; +L5: $b *= 100; +L6: $b -= -100000; +L7: $c = $b; +L8: $c -= -17000; +L9: $f = 1; +L10: $d = 2; +L11: $e = 2; +L12: $g = $d; +L13: $g *= $e; +L14: $g -= $b; +L15: goto L17 if $g; +L16: $f = 0; +L17: $e -= -1; +L18: $g = $e; +L19: $g -= $b; +L20: goto L12 if $g; +L21: $d -= -1; +L22: $g = $d; +L23: $g -= $b; +L24: goto L11 if $g; +L25: goto L27 if $f; +L26: $h -= -1; +L27: $g = $b; +L28: $g -= $c; +L29: goto L31 if $g; +L30: goto L33 if 1; +L31: $b -= -17; +L32: goto L9 if 1; diff --git a/23/input.txt b/23/input.txt new file mode 100644 index 0000000..fbde82d --- /dev/null +++ b/23/input.txt @@ -0,0 +1,32 @@ +set b 79 +set c b +jnz a 2 +jnz 1 5 +mul b 100 +sub b -100000 +set c b +sub c -17000 +set f 1 +set d 2 +set e 2 +set g d +mul g e +sub g b +jnz g 2 +set f 0 +sub e -1 +set g e +sub g b +jnz g -8 +sub d -1 +set g d +sub g b +jnz g -13 +jnz f 2 +sub h -1 +set g b +sub g c +jnz g 2 +jnz 1 3 +sub b -17 +jnz 1 -23