diff options
author | Yigit Sever | 2021-12-13 10:40:39 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-13 10:40:39 +0300 |
commit | bf16b19b1f6deffd1983efca059db576f3b60ee5 (patch) | |
tree | 1262f68d8eb2c326684d395aebcd5a1cc0b0f748 /2019/day7 | |
parent | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff) | |
download | aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2 aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip |
2019, tracking
Diffstat (limited to '2019/day7')
-rw-r--r-- | 2019/day7/comm.py | 19 | ||||
-rw-r--r-- | 2019/day7/intcode.pl | 109 | ||||
-rw-r--r-- | 2019/day7/part1.in | 1 |
3 files changed, 129 insertions, 0 deletions
diff --git a/2019/day7/comm.py b/2019/day7/comm.py new file mode 100644 index 0000000..458d8b1 --- /dev/null +++ b/2019/day7/comm.py | |||
@@ -0,0 +1,19 @@ | |||
1 | from itertools import permutations | ||
2 | import os | ||
3 | import subprocess | ||
4 | |||
5 | perm = permutations(range(5)) | ||
6 | res = [] | ||
7 | |||
8 | for seq in perm: | ||
9 | signal = 0 | ||
10 | for phase in seq: | ||
11 | out = subprocess.run( | ||
12 | ["perl", "intcode.pl", "part1.in"], | ||
13 | capture_output=True, | ||
14 | input=b"%d\n%d" % (phase, signal), | ||
15 | ) | ||
16 | signal = int(out.stdout) | ||
17 | res.append(signal) | ||
18 | |||
19 | print(sorted(res)[-1]) | ||
diff --git a/2019/day7/intcode.pl b/2019/day7/intcode.pl new file mode 100644 index 0000000..d83d2db --- /dev/null +++ b/2019/day7/intcode.pl | |||
@@ -0,0 +1,109 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use Data::Dumper; | ||
4 | # use Smart::Comments; | ||
5 | use v5.10; | ||
6 | |||
7 | $| = 1; | ||
8 | |||
9 | sub pos { | ||
10 | my ($tape_ref, $index) = @_; | ||
11 | my @tape = @{ $tape_ref }; | ||
12 | ### returning: $tape[$tape[$index]] | ||
13 | ### for: $index | ||
14 | return $tape[$tape[$index]]; | ||
15 | } | ||
16 | |||
17 | sub imm { | ||
18 | my ($tape_ref, $index) = @_; | ||
19 | my @tape = @{ $tape_ref }; | ||
20 | return $tape[$index]; | ||
21 | } | ||
22 | |||
23 | my $file_name = $ARGV[0]; | ||
24 | |||
25 | if (not defined $file_name) { | ||
26 | die "missing filename\n"; | ||
27 | } | ||
28 | |||
29 | open my $fh, "<", $file_name or die "Can't open $file_name, $!\n"; | ||
30 | |||
31 | my $inputline = <$fh>; | ||
32 | chomp $inputline; | ||
33 | my @tape = split /,/, $inputline; | ||
34 | close $fh; | ||
35 | |||
36 | my $pc = 0; # program counter is no longer consistent | ||
37 | |||
38 | my @actions = ( | ||
39 | sub { print "noop" }, # no opcode 0 | ||
40 | sub { return $_[0] + $_[1] }, # 1 | ||
41 | sub { return $_[0] * $_[1] }, # 2 | ||
42 | sub { | ||
43 | print STDERR ("Getting input!\n"); | ||
44 | my $in = <STDIN>; | ||
45 | print STDERR ("Read >$in<\n"); | ||
46 | chomp $in; | ||
47 | return $in; | ||
48 | }, # 3 | ||
49 | sub { say $_[0]; }, # 4 | ||
50 | sub { return $_[0] ? $_[1] : -1 }, # 5, ugh, you might want to jump to 0 | ||
51 | sub { return $_[0] ? -1 : $_[1] }, # 6 | ||
52 | sub { return $_[0] < $_[1] ? 1 : 0}, # 7 | ||
53 | sub { return $_[0] == $_[1] ? 1 : 0}, # 8 | ||
54 | ); | ||
55 | |||
56 | my @modes = (\&pos, \&imm); | ||
57 | my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/; | ||
58 | my $inst_ptr = 0; | ||
59 | |||
60 | while ( 1 ) { | ||
61 | |||
62 | my $raw_op_code = $tape[$inst_ptr]; | ||
63 | last if ($raw_op_code == 99); | ||
64 | |||
65 | # print("====================================\n"); | ||
66 | |||
67 | my @modes_and_opcode; | ||
68 | push @modes_and_opcode, $_ // 0 for $raw_op_code =~ m/^(\d)??(\d)??(\d)??0?(\d)$/g; | ||
69 | ### @modes_and_opcode | ||
70 | |||
71 | my $op_code = pop @modes_and_opcode; | ||
72 | ### $op_code | ||
73 | |||
74 | # foreach my $x (0..5) { | ||
75 | # print("TAPE[" . ($inst_ptr + $x) . "] = $tape[($inst_ptr + $x)]\n") | ||
76 | # } | ||
77 | |||
78 | my $toread = $offsets{$op_code} - 1; # excluding opcode | ||
79 | my @params; | ||
80 | foreach my $offset (1..$toread) { | ||
81 | push @params, $modes[ (!($op_code == 4 || $op_code == 5 || $op_code == 6) && $offset == $toread) ? 1 : pop @modes_and_opcode ]->(\@tape, $inst_ptr + $offset); | ||
82 | } | ||
83 | ### @params | ||
84 | |||
85 | $inst_ptr += $offsets{$op_code}; | ||
86 | |||
87 | if ($op_code == 1 || $op_code == 2 || $op_code == 7 || $op_code == 8) { # arithmetic | ||
88 | my $res = $actions[$op_code]->($params[0], $params[1]); | ||
89 | $tape[$params[2]] = $res; | ||
90 | ### writing: $res | ||
91 | ### on address: $params[2] | ||
92 | } | ||
93 | elsif ($op_code == 3) { # input | ||
94 | my $res = $actions[$op_code]->(); | ||
95 | $tape[$params[0]] = $res; | ||
96 | ### saved: $res | ||
97 | ### on: $params[0] | ||
98 | } elsif ($op_code == 4) { # output | ||
99 | $actions[$op_code]->($params[0]); | ||
100 | } elsif ($op_code == 5 || $op_code == 6) { # jumps | ||
101 | my $res = $actions[$op_code]->($params[0], $params[1]); | ||
102 | $inst_ptr = $res == -1 ? $inst_ptr : $res; | ||
103 | ### jumped to: $inst_ptr | ||
104 | next; | ||
105 | } else { | ||
106 | ### OH NO... | ||
107 | die; | ||
108 | } | ||
109 | } | ||
diff --git a/2019/day7/part1.in b/2019/day7/part1.in new file mode 100644 index 0000000..a700dd4 --- /dev/null +++ b/2019/day7/part1.in | |||
@@ -0,0 +1 @@ | |||
3,8,1001,8,10,8,105,1,0,0,21,46,59,72,93,110,191,272,353,434,99999,3,9,101,4,9,9,1002,9,3,9,1001,9,5,9,102,2,9,9,1001,9,5,9,4,9,99,3,9,1002,9,5,9,1001,9,5,9,4,9,99,3,9,101,4,9,9,1002,9,4,9,4,9,99,3,9,102,3,9,9,101,3,9,9,1002,9,2,9,1001,9,5,9,4,9,99,3,9,1001,9,2,9,102,4,9,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99 | |||