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/day2/intcode.pl | |
parent | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (diff) | |
download | aoc-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.pl | 57 |
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 @@ | |||
1 | use strict; | ||
2 | use warnings; | ||
3 | use Data::Dumper; | ||
4 | # use Smart::Comments; | ||
5 | |||
6 | my $inputline = <STDIN>; | ||
7 | chomp $inputline; | ||
8 | my @memory = split /,/, $inputline; | ||
9 | |||
10 | @memory = map {int $_} @memory; | ||
11 | |||
12 | my $op_code_pos = 0; | ||
13 | my $pos_1 = 1; | ||
14 | my $pos_2 = 2; | ||
15 | my $loc_pos = 3; | ||
16 | my $pc = 4; | ||
17 | |||
18 | my @actions = (sub {print "noop"}, sub {return $_[0] + $_[1]}, sub {return $_[0] * $_[1]}); | ||
19 | |||
20 | my $output = 0; | ||
21 | |||
22 | my $one_inc = 0; | ||
23 | my $two_inc = 0; | ||
24 | my $turn = 0; | ||
25 | my @mem = @memory; | ||
26 | |||
27 | foreach 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; | ||
54 | END { | ||
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 | } | ||