sol1.pl (648B) [raw]
1 #!/usr/bin/env perl 2 use strict; 3 use warnings; 4 5 my $preamble = 25; 6 my $n = 0; 7 my @mem; 8 while (my $l = <ARGV>) { 9 chomp $l; 10 if ($n < $preamble) { 11 # build memory 12 push (@mem, $l); 13 $n++; 14 } else { 15 # use memory 16 check($l, \@mem); 17 shift(@mem); 18 push(@mem, $l); 19 } 20 } 21 exit 1; 22 23 sub check { 24 my ($l, $memA) = @_; 25 26 # TODO: optimize? 27 my %seen; 28 for my $m (@$memA) { 29 $seen{$m} = 1; 30 my $c = $l - $m; 31 if ($seen{$c} && $c != $m) { 32 # success! 33 return; 34 } 35 } 36 37 # nothing adds up! report & exit 38 print "$l\n"; 39 exit 0; 40 }