#!/usr/bin/perl -w

use strict;

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

for my $i (0 .. $#code) {
	if ($code[$i]->[0] eq 'nop') {
		local $code[$i]->[0] = 'jmp';
		interp(\@code);
	} elsif ($code[$i]->[0] eq 'jmp') {
		local $code[$i]->[0] = 'nop';
		interp(\@code);
	}
}
		
sub interp {
	my ($code) = @_;
	my $acc = 0;
	my $pc = 0;
	my %seen;

	while (!$seen{$pc} && $pc != @$code) {
		$seen{$pc} = 1;
		my ($ins, $arg) = @{ $code->[$pc] };
		# print "pc=$pc, acc=$acc, $ins $arg\n";
		if ($ins eq 'nop') {
			$pc++;
		} elsif ($ins eq 'acc') {
			$acc += $arg; $pc++;
		} elsif ($ins eq 'jmp') {
			$pc += $arg;
		}
	}
	if ($pc == @$code) {
		print "terminating with acc=$acc.\n";
		exit 0;
	}
	# print "\n";
}
