adventofcode/2017/23/2.pl

70 lines
1.1 KiB
Perl
Raw Normal View History

2017-12-23 20:06:05 +00:00
use 5.20.0;
2017-12-23 20:09:19 +00:00
my $script = "\$a = 1;\n";
2017-12-23 20:06:05 +00:00
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${.}: /;
2017-12-23 20:09:19 +00:00
$script .= $_;
2017-12-23 20:06:05 +00:00
}
2017-12-23 20:09:19 +00:00
for ( 1..35 ) {
2017-12-24 18:10:58 +00:00
my @c = $script =~ /(L$_[ :])/g;
2017-12-23 20:09:19 +00:00
next if @c > 1;
2017-12-24 18:10:58 +00:00
$script =~ s/L$_[ :] *//g;
2017-12-23 20:09:19 +00:00
}
print $script;
2017-12-23 20:06:05 +00:00
__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 = <STDIN>;
}
say $reg{h};