#!/usr/bin/perl -w

use strict;
use v5.30;

my @code = map { chomp; [ split /\s+/ ] } <>;

my $ip = 0;

my %tgl = (
	cpy => 'jnz',
	inc => 'dec',
	dec => 'inc',
	jnz => 'cpy',
	tgl => 'inc',
);

my %regs = (a => 12);
while ($ip < @code) {
	say join(' ', $ip, @{ $code[$ip] }, map { "$_=$regs{$_}" } sort keys %regs);
	my @ins = @{ $code[$ip] };
	if ($ins[0] eq 'cpy') {
		my $val = $ins[1];
		my $reg = $ins[2];
		$val = $regs{$val} if $val =~ /[a-z]/;
		$regs{$reg} = $val;
	} elsif ($ins[0] eq 'inc') {
		$regs{$ins[1]}++;
	} elsif ($ins[0] eq 'dec') {
		$regs{$ins[1]}--;
	} elsif ($ins[0] eq 'jnz') {
		my ($reg, $val) = @ins[1..2];
		$reg = $regs{$reg} if $reg =~ /[a-z]/;
		$val = $regs{$val} if $val =~ /[a-z]/;
		if ($reg) {
			$ip += $val - 1;
		}
	} elsif ($ins[0] eq 'tgl') {
		my $off = $ins[1];
		$off = $regs{$off} if $off =~ /[a-z]/;
		my $other = $code[$ip + $off];
		$other->[0] = $tgl{$other->[0]};
	} else {
		say "Unknown instrution: $_";
	}
	$ip++;
}

say $ip;
say $regs{a};

