aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

sol1.pl (610B) [raw]


      1 #!/usr/bin/env perl
      2 use strict;
      3 use warnings;
      4 
      5 open(my $fh, '<', 'input') or die "$!";
      6 my @lines;
      7 {
      8     local $/;
      9     my $content = <$fh>;
     10     @lines = split("\n", $content);
     11 }
     12 
     13 my $acc = 0;
     14 my $ptr = 0;
     15 my %seen;
     16 my %C = (
     17     acc => sub { $acc += $_[0]; $ptr++ },
     18     nop => sub { $ptr++ },
     19     jmp => sub { $ptr += $_[0] },
     20 );
     21 
     22 while (1) {
     23     if (exists $seen{$ptr}) {
     24         print "$acc\n";
     25         exit(0);
     26     }
     27     my ($ins, $val) = split(' ', $lines[$ptr]);
     28     # print "$ptr\t$acc\t$lines[$ptr]\n";
     29     $seen{$ptr} = 1;
     30     $C{$ins}->($val);
     31 }
     32 
     33 # heh infinite loop if we don't find an infinite loop