#!/usr/bin/env perl
use strict;
use warnings;

open(my $fh, '<', 'input') or die "$!";
my @lines;
{
    local $/;
    my $content = <$fh>;
    @lines = split("\n", $content);
}

my $acc = 0;
my $ptr = 0;
my %seen;
my %C = (
    acc => sub { $acc += $_[0]; $ptr++ },
    nop => sub { $ptr++ },
    jmp => sub { $ptr += $_[0] },
);

while (1) {
    if (exists $seen{$ptr}) {
        print "$acc\n";
        exit(0);
    }
    my ($ins, $val) = split(' ', $lines[$ptr]);
    # print "$ptr\t$acc\t$lines[$ptr]\n";
    $seen{$ptr} = 1;
    $C{$ins}->($val);
}

# heh infinite loop if we don't find an infinite loop
