From 72b8a71caec07a90aa0b926c3a029682ede23964 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 18 Dec 2017 11:39:40 -0500 Subject: [PATCH] part1 --- 18/1.pl | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 18/input.txt | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 18/1.pl create mode 100644 18/input.txt diff --git a/18/1.pl b/18/1.pl new file mode 100644 index 0000000..cd3597a --- /dev/null +++ b/18/1.pl @@ -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? $_"; } + +} diff --git a/18/input.txt b/18/input.txt new file mode 100644 index 0000000..90173ae --- /dev/null +++ b/18/input.txt @@ -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