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/day2/intcode.pl | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2019/day2/intcode.pl (limited to '2019/day2/intcode.pl') diff --git a/2019/day2/intcode.pl b/2019/day2/intcode.pl new file mode 100644 index 0000000..984784a --- /dev/null +++ b/2019/day2/intcode.pl @@ -0,0 +1,57 @@ +use strict; +use warnings; +use Data::Dumper; +# use Smart::Comments; + +my $inputline = ; +chomp $inputline; +my @memory = split /,/, $inputline; + +@memory = map {int $_} @memory; + +my $op_code_pos = 0; +my $pos_1 = 1; +my $pos_2 = 2; +my $loc_pos = 3; +my $pc = 4; + +my @actions = (sub {print "noop"}, sub {return $_[0] + $_[1]}, sub {return $_[0] * $_[1]}); + +my $output = 0; + +my $one_inc = 0; +my $two_inc = 0; +my $turn = 0; +my @mem = @memory; + +foreach my $x (0..99) { + foreach my $y (0..99) { + + @mem = @memory; + $mem[1] = $x; + $mem[2] = $y; + + for (my $add = 0; $add < scalar @mem; $add += $pc) { + + my $op_code = $mem[$add + $op_code_pos]; + last if ($op_code == 99); + + my $op_1 = $mem[$mem[$add + $pos_1]]; + my $op_2 = $mem[$mem[$add + $pos_2]]; + my $loc = $mem[$add + $loc_pos]; + my $res = $actions[$op_code]->($op_1, $op_2); + + $mem[$loc] = $res; + } + $output = $mem[0]; + ### $output + exit if $output == 19690720; + } +} + +# print Dumper \@mem; +# print join ',', @mem; +END { + print STDERR "Output: >$mem[0]<\nFor mem[1] = $mem[1] and mem[2] = $mem[2]\n"; + print 100 * $mem[1] + $mem[2]; +} -- cgit v1.2.3-70-g09d2