summaryrefslogtreecommitdiffstats
path: root/2019/day2/intcode.pl
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/day2/intcode.pl
parent74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff)
downloadaoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.gz
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.tar.bz2
aoc-bf16b19b1f6deffd1983efca059db576f3b60ee5.zip
2019, tracking
Diffstat (limited to '2019/day2/intcode.pl')
-rw-r--r--2019/day2/intcode.pl57
1 files changed, 57 insertions, 0 deletions
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 @@
1use strict;
2use warnings;
3use Data::Dumper;
4# use Smart::Comments;
5
6my $inputline = <STDIN>;
7chomp $inputline;
8my @memory = split /,/, $inputline;
9
10@memory = map {int $_} @memory;
11
12my $op_code_pos = 0;
13my $pos_1 = 1;
14my $pos_2 = 2;
15my $loc_pos = 3;
16my $pc = 4;
17
18my @actions = (sub {print "noop"}, sub {return $_[0] + $_[1]}, sub {return $_[0] * $_[1]});
19
20my $output = 0;
21
22my $one_inc = 0;
23my $two_inc = 0;
24my $turn = 0;
25my @mem = @memory;
26
27foreach my $x (0..99) {
28 foreach my $y (0..99) {
29
30 @mem = @memory;
31 $mem[1] = $x;
32 $mem[2] = $y;
33
34 for (my $add = 0; $add < scalar @mem; $add += $pc) {
35
36 my $op_code = $mem[$add + $op_code_pos];
37 last if ($op_code == 99);
38
39 my $op_1 = $mem[$mem[$add + $pos_1]];
40 my $op_2 = $mem[$mem[$add + $pos_2]];
41 my $loc = $mem[$add + $loc_pos];
42 my $res = $actions[$op_code]->($op_1, $op_2);
43
44 $mem[$loc] = $res;
45 }
46 $output = $mem[0];
47 ### $output
48 exit if $output == 19690720;
49 }
50}
51
52# print Dumper \@mem;
53# print join ',', @mem;
54END {
55 print STDERR "Output: >$mem[0]<\nFor mem[1] = $mem[1] and mem[2] = $mem[2]\n";
56 print 100 * $mem[1] + $mem[2];
57}