6 my @code = map { chomp; [ split /\s+/ ] } <>;
11 my %regs = (a => ++$start, b => 0, c => 0, d => 0);
22 say join(' ', $ip, @{ $code[$ip] }, map { "$_=$regs{$_}" } sort keys %regs)
24 my $key = join(',', $ip, map { $regs{$_} } qw(a b c d));
25 if (defined $state{$key} && $nouts > $state{$key}
26 && ($nouts - $state{$key}) % 2 == 0) {
30 $state{$key} = $nouts;
32 my @ins = @{ $code[$ip] };
33 if ($ins[0] eq 'cpy') {
36 $val = $regs{$val} if $val =~ /[a-z]/;
38 } elsif ($ins[0] eq 'inc') {
40 } elsif ($ins[0] eq 'dec') {
42 } elsif ($ins[0] eq 'jnz') {
43 my ($reg, $val) = @ins[1..2];
44 if ($val eq '-2' && $ip >= 2 && $code[$ip-1][0] eq 'dec'
45 && $code[$ip-2][0] eq 'inc'
46 && $code[$ip-1][1] eq $reg) {
47 $regs{$code[$ip-2][1]} += $regs{$reg};
50 $val = $regs{$val} if $val =~ /[a-z]/;
51 $reg = $regs{$reg} if $reg =~ /[a-z]/;
55 } elsif ($ins[0] eq 'out') {
57 $val = $regs{$val} if $val =~ /[a-z]/;
58 if ($val != ($nouts & 1)) {
63 say "Unknown instrution: $_";