summaryrefslogtreecommitdiffstats
path: root/2019/day5
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:40:39 +0300
committerYigit Sever2021-12-13 10:40:39 +0300
commitbf16b19b1f6deffd1983efca059db576f3b60ee5 (patch)
tree1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day5
parent74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff)
downloadaoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip
2019, tracking
Diffstat (limited to '2019/day5')
-rw-r--r--2019/day5/intcode.pl92
-rw-r--r--2019/day5/part1.in1
2 files changed, 93 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 @@
1use strict;
2use warnings;
3use Data::Dumper;
4# use Smart::Comments;
5use v5.10;
6
7sub 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
15sub imm {
16 my ($tape_ref, $index) = @_;
17 my @tape = @{ $tape_ref };
18 return $tape[$index];
19}
20
21my $inputline = <STDIN>;
22chomp $inputline;
23my @tape = split /,/, $inputline;
24
25my $pc = 0; # program counter is no longer consistent
26
27my @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
39my @modes = (\&pos, \&imm);
40my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/;
41my $inst_ptr = 0;
42
43while ( 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}
diff --git a/2019/day5/part1.in b/2019/day5/part1.in
new file mode 100644
index 0000000..9d9b84d
--- /dev/null
+++ b/2019/day5/part1.in
@@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1002,114,19,224,1001,224,-646,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,40,62,225,1101,60,38,225,1101,30,29,225,2,195,148,224,1001,224,-40,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1001,143,40,224,101,-125,224,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,101,29,139,224,1001,224,-99,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,14,34,225,102,57,39,224,101,-3420,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,70,40,225,1102,85,69,225,1102,94,5,225,1,36,43,224,101,-92,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,1102,94,24,224,1001,224,-2256,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1102,8,13,225,1101,36,65,224,1001,224,-101,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,677,226,224,1002,223,2,223,1006,224,329,1001,223,1,223,1108,226,226,224,1002,223,2,223,1005,224,344,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,359,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,374,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,389,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,404,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,419,101,1,223,223,108,677,226,224,1002,223,2,223,1006,224,434,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,449,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,108,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,7,677,677,224,102,2,223,223,1005,224,494,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,509,101,1,223,223,107,677,226,224,1002,223,2,223,1005,224,524,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,539,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,554,1001,223,1,223,8,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,584,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,614,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,629,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,659,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226