From bf16b19b1f6deffd1983efca059db576f3b60ee5 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Mon, 13 Dec 2021 10:40:39 +0300 Subject: 2019, tracking --- 2019/day7/comm.py | 19 +++++++++ 2019/day7/intcode.pl | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2019/day7/part1.in | 1 + 3 files changed, 129 insertions(+) create mode 100644 2019/day7/comm.py create mode 100644 2019/day7/intcode.pl create mode 100644 2019/day7/part1.in (limited to '2019/day7') 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 @@ +from itertools import permutations +import os +import subprocess + +perm = permutations(range(5)) +res = [] + +for seq in perm: + signal = 0 + for phase in seq: + out = subprocess.run( + ["perl", "intcode.pl", "part1.in"], + capture_output=True, + input=b"%d\n%d" % (phase, signal), + ) + signal = int(out.stdout) + res.append(signal) + +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 @@ +use strict; +use warnings; +use Data::Dumper; +# use Smart::Comments; +use v5.10; + +$| = 1; + +sub pos { + my ($tape_ref, $index) = @_; + my @tape = @{ $tape_ref }; + ### returning: $tape[$tape[$index]] + ### for: $index + return $tape[$tape[$index]]; +} + +sub imm { + my ($tape_ref, $index) = @_; + my @tape = @{ $tape_ref }; + return $tape[$index]; +} + +my $file_name = $ARGV[0]; + +if (not defined $file_name) { + die "missing filename\n"; +} + +open my $fh, "<", $file_name or die "Can't open $file_name, $!\n"; + +my $inputline = <$fh>; +chomp $inputline; +my @tape = split /,/, $inputline; +close $fh; + +my $pc = 0; # program counter is no longer consistent + +my @actions = ( + sub { print "noop" }, # no opcode 0 + sub { return $_[0] + $_[1] }, # 1 + sub { return $_[0] * $_[1] }, # 2 + sub { + print STDERR ("Getting input!\n"); + my $in = ; + print STDERR ("Read >$in<\n"); + chomp $in; + return $in; + }, # 3 + sub { say $_[0]; }, # 4 + sub { return $_[0] ? $_[1] : -1 }, # 5, ugh, you might want to jump to 0 + sub { return $_[0] ? -1 : $_[1] }, # 6 + sub { return $_[0] < $_[1] ? 1 : 0}, # 7 + sub { return $_[0] == $_[1] ? 1 : 0}, # 8 +); + +my @modes = (\&pos, \&imm); +my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/; +my $inst_ptr = 0; + +while ( 1 ) { + + my $raw_op_code = $tape[$inst_ptr]; + last if ($raw_op_code == 99); + + # print("====================================\n"); + + my @modes_and_opcode; + push @modes_and_opcode, $_ // 0 for $raw_op_code =~ m/^(\d)??(\d)??(\d)??0?(\d)$/g; + ### @modes_and_opcode + + my $op_code = pop @modes_and_opcode; + ### $op_code + + # foreach my $x (0..5) { + # print("TAPE[" . ($inst_ptr + $x) . "] = $tape[($inst_ptr + $x)]\n") + # } + + my $toread = $offsets{$op_code} - 1; # excluding opcode + my @params; + foreach my $offset (1..$toread) { + push @params, $modes[ (!($op_code == 4 || $op_code == 5 || $op_code == 6) && $offset == $toread) ? 1 : pop @modes_and_opcode ]->(\@tape, $inst_ptr + $offset); + } + ### @params + + $inst_ptr += $offsets{$op_code}; + + if ($op_code == 1 || $op_code == 2 || $op_code == 7 || $op_code == 8) { # arithmetic + my $res = $actions[$op_code]->($params[0], $params[1]); + $tape[$params[2]] = $res; + ### writing: $res + ### on address: $params[2] + } + elsif ($op_code == 3) { # input + my $res = $actions[$op_code]->(); + $tape[$params[0]] = $res; + ### saved: $res + ### on: $params[0] + } elsif ($op_code == 4) { # output + $actions[$op_code]->($params[0]); + } elsif ($op_code == 5 || $op_code == 6) { # jumps + my $res = $actions[$op_code]->($params[0], $params[1]); + $inst_ptr = $res == -1 ? $inst_ptr : $res; + ### jumped to: $inst_ptr + next; + } else { + ### OH NO... + die; + } +} 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 -- cgit v1.2.3-70-g09d2