This commit is contained in:
Yanick Champoux 2017-12-18 11:39:40 -05:00
parent df6a180b91
commit 72b8a71cae
2 changed files with 92 additions and 0 deletions

51
18/1.pl Normal file
View File

@ -0,0 +1,51 @@
use 5.20.0;
my @commands = <>;
my $i = 0;
my %reg;
my $last_played;
use experimental qw/ signatures /;
sub valof($x) {
my $y = $x =~ /\d/ ? $x : $reg{$x};
die $_ if $y eq 'Inf';
return $y;
}
my $vr = qr/(-?\d+|.)/;
while() {
$_ = $commands[$i++];
warn $_ if /\bb\b/;
if( /snd (.)/ ) {
$last_played = valof($1);
}
elsif( /set (.) $vr/ ) {
$reg{$1} = valof($2);
}
elsif( /add (.) $vr/ ) {
$reg{$1} += valof($2);
}
elsif( /mul (.) $vr/ ) {
$reg{$1} *= valof($2);
}
elsif( /mul (.) $vr/ ) {
$reg{$1} *= valof($2);
}
elsif( /mod (.) $vr/ ) {
$reg{$1} %= valof($2);
}
elsif( /jgz (.) $vr/ ) {
next unless $reg{$1} > 0;
$i += -1 + valof($2);
}
elsif( /rcv (.)/ ) {
next unless $reg{$1};
die $last_played;
}
else { die "wut? $_"; }
}

41
18/input.txt Normal file
View File

@ -0,0 +1,41 @@
set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 622
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19