]> www.fi.muni.cz Git - aoc2020.git/blob - 15.pl
Task 9 Perl Golf-style
[aoc2020.git] / 15.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my @code = map { [ split /\s+/ ] } <>;
6
7 my $acc = 0;
8 my $pc = 0;
9 my %seen;
10
11 while (!$seen{$pc}) {
12         $seen{$pc} = 1;
13         my ($ins, $arg) = @{ $code[$pc] };
14         print "pc=$pc, acc=$acc, $ins $arg\n";
15         if ($ins eq 'nop') {
16                 $pc++;
17         } elsif ($ins eq 'acc') {
18                 $acc += $arg; $pc++;
19         } elsif ($ins eq 'jmp') {
20                 $pc += $arg;
21         }
22 }
23 print "loop at $pc, acc=$acc\n";