#!/usr/bin/perl use v5.35.0; use integer; my @rf = (0 .. 7); # Registers 0..3 have fixed contents while (<>) { $rf[4] = $1 if /Register A: (\d+)/; $rf[5] = $1 if /Register B: (\d+)/; $rf[6] = $1 if /Register C: (\d+)/; if (/Program: ((?:\d+)(?:,\d+)*)\s+(.*)/) { say $2 if defined $2; my @rom = split /,/, $1; my $ip = 0; my ($ir, $or); my @out; FETCH: say '', (map {sprintf"%011o ",$_} @rf[4..6]), (map {($ip == $_? ' >':' ').$rom[$_]} (0 .. $#rom)); if ($ip > $#rom) {goto HALT}; $ir = $rom[$ip++]; $or = $rom[$ip++]; goto "OP$ir"; OP0: $rf[4] >>= $rf[$or]; goto FETCH; OP1: $rf[5] ^= $or; goto FETCH; OP2: $rf[5] = $rf[$or] & 7; goto FETCH; OP3: $ip = $or if $rf[4]; goto FETCH; OP4: $rf[5] ^= $rf[6]; goto FETCH; OP5: push @out, $rf[$or] & 7; goto FETCH; OP6: $rf[5] = $rf[4] >> $rf[$or]; goto FETCH; OP7: $rf[6] = $rf[4] >> $rf[$or]; goto FETCH; HALT: say join ',', @out; say ''; } }