This commit is contained in:
Yanick Champoux 2018-02-14 12:31:28 -05:00
parent 4bbacd7687
commit d2312baf5b
2 changed files with 82 additions and 0 deletions

59
2016/12/1.pl Normal file
View File

@ -0,0 +1,59 @@
use 5.20.0;
undef $/;
my $code = <>;
$code =~ s/\b([a-d])\b/\$$1/g;
$code =~ s/cpy (\S+) (\S+)/$2 = $1/g;
$code =~ s/inc (\S+)/$1++/g;
$code =~ s/dec (\S+)/$1--/g;
my $i = 1;
$code =
join "\n",
map { $_->[1] }
map {
$_->[1] =~ s/jnz (\S+) (\S+)/"if($1){ goto LINE_" . ( $_->[0] + $2 ) . "}"/e;
$_ }
map { $_->[1] = "LINE_" . $_->[0] . ': ' . $_->[1]; $_ }
map { [ $i++, $_ ] } grep { length } split /\n/, $code;
$code =~ s/$/;/mg;
my( $c, $d) = ( 1,0);
eval $code;
die $@ if $@;
say $a;
__END__
use Scalar::Util qw/ looks_like_number /;
my $i = 0;
my @instructions = map { "LINE_" . $i++ . ': ' . $_ } <>;
my %reg;
while ( my $c = $instructions[$i++] ) {
say $i;
use DDP;
p %reg;
if( $c =~ /cpy (\S+) (.)/ ) {
$reg{$2} = looks_like_number($1) ? $1 : $reg{$1};
}
elsif ( $c =~ /(inc|dec) (.)/ ) {
$reg{$2} += ($1 eq 'inc') ? 1 : -1;
}
elsif( $c =~ /jnz (.) (.*)/ ) {
$i += -1 + $2 if $reg{$1};
}
else { die }
my $x = <STDIN>;
}
say $reg{a};

23
2016/12/input.txt Normal file
View File

@ -0,0 +1,23 @@
cpy 1 a
cpy 1 b
cpy 26 d
jnz c 2
jnz 1 5
cpy 7 c
inc d
dec c
jnz c -2
cpy a c
inc a
dec b
jnz b -2
cpy c b
dec d
jnz d -6
cpy 19 c
cpy 11 d
inc a
dec d
jnz d -2
dec c
jnz c -5