#!/usr/bin/perl -w

use v5.16;
use strict;

my %regs = ( a => 0, b => 0 );
my @code = map { chomp; [ split /[ ,+]+/ ] } <>;
my $ip = 0;
while ($ip < @code) {
	my ($ins, $v1, $v2) = @{ $code[$ip] };
	say "$ip: ", join(' ', @{ $code[$ip] }), " a=$regs{a} b=$regs{b}";
	if ($ins eq 'hlf') {
		$regs{$v1} = int($regs{$v1}/2);
	} elsif ($ins eq 'tpl') {
		$regs{$v1} *= 3;
	} elsif ($ins eq 'inc') {
		$regs{$v1}++;
	} elsif ($ins eq 'jmp') {
		$ip += $v1 - 1;
	} elsif ($ins eq 'jie') {
		$ip += $v2 - 1 if $regs{$v1} % 2 == 0;
	} elsif ($ins eq 'jio') {
		$ip += $v2 - 1 if $regs{$v1} == 1;
	} else {
		say "Invalid insn $ins";
	}
	$ip++;
}

say "ip=$ip";
say $regs{b};
