diff options
Diffstat (limited to '2019/day5/intcode.pl')
-rw-r--r-- | 2019/day5/intcode.pl | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/2019/day5/intcode.pl b/2019/day5/intcode.pl new file mode 100644 index 0000000..613f067 --- /dev/null +++ b/2019/day5/intcode.pl | |||
@@ -0,0 +1,92 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use Data::Dumper; | ||
4 | # use Smart::Comments; | ||
5 | use v5.10; | ||
6 | |||
7 | sub pos { | ||
8 | my ($tape_ref, $index) = @_; | ||
9 | my @tape = @{ $tape_ref }; | ||
10 | ### returning: $tape[$tape[$index]] | ||
11 | ### for: $index | ||
12 | return $tape[$tape[$index]]; | ||
13 | } | ||
14 | |||
15 | sub imm { | ||
16 | my ($tape_ref, $index) = @_; | ||
17 | my @tape = @{ $tape_ref }; | ||
18 | return $tape[$index]; | ||
19 | } | ||
20 | |||
21 | my $inputline = <STDIN>; | ||
22 | chomp $inputline; | ||
23 | my @tape = split /,/, $inputline; | ||
24 | |||
25 | my $pc = 0; # program counter is no longer consistent | ||
26 | |||
27 | my @actions = ( | ||
28 | sub { print "noop" }, # no opcode 0 | ||
29 | sub { return $_[0] + $_[1] }, # 1 | ||
30 | sub { return $_[0] * $_[1] }, # 2 | ||
31 | sub { return 5; }, # 3 | ||
32 | sub { say $_[0]; }, # 4 | ||
33 | sub { return $_[0] ? $_[1] : -1 }, # 5, ugh, you might want to jump to 0 | ||
34 | sub { return $_[0] ? -1 : $_[1] }, # 6 | ||
35 | sub { return $_[0] < $_[1] ? 1 : 0}, # 7 | ||
36 | sub { return $_[0] == $_[1] ? 1 : 0}, # 8 | ||
37 | ); | ||
38 | |||
39 | my @modes = (\&pos, \&imm); | ||
40 | my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/; | ||
41 | my $inst_ptr = 0; | ||
42 | |||
43 | while ( 1 ) { | ||
44 | |||
45 | my $raw_op_code = $tape[$inst_ptr]; | ||
46 | last if ($raw_op_code == 99); | ||
47 | |||
48 | # print("====================================\n"); | ||
49 | |||
50 | my @modes_and_opcode; | ||
51 | push @modes_and_opcode, $_ // 0 for $raw_op_code =~ m/^(\d)??(\d)??(\d)??0?(\d)$/g; | ||
52 | ### @modes_and_opcode | ||
53 | |||
54 | my $op_code = pop @modes_and_opcode; | ||
55 | ### $op_code | ||
56 | |||
57 | # foreach my $x (0..5) { | ||
58 | # print("TAPE[" . ($inst_ptr + $x) . "] = $tape[($inst_ptr + $x)]\n") | ||
59 | # } | ||
60 | |||
61 | my $toread = $offsets{$op_code} - 1; # excluding opcode | ||
62 | my @params; | ||
63 | foreach my $offset (1..$toread) { | ||
64 | push @params, $modes[ (!($op_code == 4 || $op_code == 5 || $op_code == 6) && $offset == $toread) ? 1 : pop @modes_and_opcode ]->(\@tape, $inst_ptr + $offset); | ||
65 | } | ||
66 | ### @params | ||
67 | |||
68 | $inst_ptr += $offsets{$op_code}; | ||
69 | |||
70 | if ($op_code == 1 || $op_code == 2 || $op_code == 7 || $op_code == 8) { # arithmetic | ||
71 | my $res = $actions[$op_code]->($params[0], $params[1]); | ||
72 | $tape[$params[2]] = $res; | ||
73 | ### writing: $res | ||
74 | ### on address: $params[2] | ||
75 | } | ||
76 | elsif ($op_code == 3) { # input | ||
77 | my $res = $actions[$op_code]->(); | ||
78 | $tape[$params[0]] = $res; | ||
79 | ### saved: $res | ||
80 | ### on: $params[0] | ||
81 | } elsif ($op_code == 4) { # output | ||
82 | $actions[$op_code]->($params[0]); | ||
83 | } elsif ($op_code == 5 || $op_code == 6) { # jumps | ||
84 | my $res = $actions[$op_code]->($params[0], $params[1]); | ||
85 | $inst_ptr = $res == -1 ? $inst_ptr : $res; | ||
86 | ### jumped to: $inst_ptr | ||
87 | next; | ||
88 | } else { | ||
89 | ### OH NO... | ||
90 | die; | ||
91 | } | ||
92 | } | ||