adventofcode/2020/08/part2.pm

45 lines
743 B
Perl
Raw Normal View History

2021-12-05 16:51:21 +00:00
package part2;
use 5.20.0;
use warnings;
require './part1.pm';
use experimental qw/
signatures
postderef
switch
/;
sub run_instance(@code) {
my $index = 0;
my $accum = 0;
my %seen;
until( $seen{$index}++ ) {
($index,$accum) = part1::run_once($index,$accum,\@code);
return $accum if $index >= @code;
}
return undef;
}
sub solution($lines) {
my @code = part1::parse_code($lines);
for(0..$#code) {
next if $code[$_][0] eq 'acc';
my @new_code = @code;
$new_code[$_] = [ $code[$_]->@* ];
$new_code[$_][0] = $new_code[$_][0] eq 'nop' ? 'jmp' : 'nop';
my $result = run_instance(@new_code);
return $result if $result;
}
}
1;