summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--2019/day1/README.md5
-rw-r--r--2019/day1/part1.in100
-rw-r--r--2019/day1/part2.in100
-rw-r--r--2019/day1/rec_fuel.py23
-rw-r--r--2019/day2/1202.in1
-rw-r--r--2019/day2/intcode.pl57
-rw-r--r--2019/day3/part1.in2
-rw-r--r--2019/day3/smalltest.in2
-rw-r--r--2019/day3/wires.pl68
-rw-r--r--2019/day4/password.lp17
-rw-r--r--2019/day4/password.pl29
-rw-r--r--2019/day5/intcode.pl92
-rw-r--r--2019/day5/part1.in1
-rw-r--r--2019/day6/orbits.pl39
-rw-r--r--2019/day6/orbits.py28
-rw-r--r--2019/day6/part1.in1069
-rw-r--r--2019/day7/comm.py19
-rw-r--r--2019/day7/intcode.pl109
-rw-r--r--2019/day7/part1.in1
19 files changed, 1762 insertions, 0 deletions
diff --git a/2019/day1/README.md b/2019/day1/README.md
new file mode 100644
index 0000000..af0b60e
--- /dev/null
+++ b/2019/day1/README.md
@@ -0,0 +1,5 @@
1I realized part 1 can be solved with a perl one liner;
2
3```bash
4perl -ne '$c += int ($_ / 3) - 2;END {print $c}' < part1.in
5```
diff --git a/2019/day1/part1.in b/2019/day1/part1.in
new file mode 100644
index 0000000..63d0a1a
--- /dev/null
+++ b/2019/day1/part1.in
@@ -0,0 +1,100 @@
193912
2138996
3112824
4110011
5139024
6132292
774029
881664
9138077
10109614
11121056
12136338
13132771
1486611
15131526
16123101
1761315
1893900
1962070
2097957
2167168
22119464
23119066
24111076
2556856
26144203
27109400
28120187
2957915
30143353
3171308
3267695
33141275
34106552
35136209
3686990
3798969
3857207
3999103
4071940
4163145
4291765
43121095
44139700
45128851
4677138
4766712
4891318
4996924
50132235
5199897
5267479
5387996
54121100
5555411
5661715
57130658
58121030
59141445
6083939
6190402
62121107
6359618
64120112
6558140
66103514
6790538
6855552
69142739
7061770
71147374
7280038
73128830
7493328
7552369
7671801
77144536
78147140
79118213
80128056
8192155
82114384
8389234
84124451
8594214
8679174
87108427
88111041
8996715
90128414
9162521
9293897
93107428
9490637
95126176
9678676
9769504
9893663
9980869
100124230
diff --git a/2019/day1/part2.in b/2019/day1/part2.in
new file mode 100644
index 0000000..63d0a1a
--- /dev/null
+++ b/2019/day1/part2.in
@@ -0,0 +1,100 @@
193912
2138996
3112824
4110011
5139024
6132292
774029
881664
9138077
10109614
11121056
12136338
13132771
1486611
15131526
16123101
1761315
1893900
1962070
2097957
2167168
22119464
23119066
24111076
2556856
26144203
27109400
28120187
2957915
30143353
3171308
3267695
33141275
34106552
35136209
3686990
3798969
3857207
3999103
4071940
4163145
4291765
43121095
44139700
45128851
4677138
4766712
4891318
4996924
50132235
5199897
5267479
5387996
54121100
5555411
5661715
57130658
58121030
59141445
6083939
6190402
62121107
6359618
64120112
6558140
66103514
6790538
6855552
69142739
7061770
71147374
7280038
73128830
7493328
7552369
7671801
77144536
78147140
79118213
80128056
8192155
82114384
8389234
84124451
8594214
8679174
87108427
88111041
8996715
90128414
9162521
9293897
93107428
9490637
95126176
9678676
9769504
9893663
9980869
100124230
diff --git a/2019/day1/rec_fuel.py b/2019/day1/rec_fuel.py
new file mode 100644
index 0000000..d837202
--- /dev/null
+++ b/2019/day1/rec_fuel.py
@@ -0,0 +1,23 @@
1import sys
2
3
4def recfuel(mass):
5 """Calculate the fuel required for the module
6 use the new weight of the fuel recursively
7 r(M) = r(M / 3 - 2)
8 """
9 M = mass // 3 - 2
10
11 if M < 0:
12 return 0
13 else:
14 return M + recfuel(M)
15
16
17total = 0
18
19for mass in sys.stdin:
20 ret = recfuel(int(mass))
21 total += ret
22
23print(total)
diff --git a/2019/day2/1202.in b/2019/day2/1202.in
new file mode 100644
index 0000000..6bbbce6
--- /dev/null
+++ b/2019/day2/1202.in
@@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,2,6,23,27,1,27,5,31,2,9,31,35,1,5,35,39,2,6,39,43,2,6,43,47,1,5,47,51,2,9,51,55,1,5,55,59,1,10,59,63,1,63,6,67,1,9,67,71,1,71,6,75,1,75,13,79,2,79,13,83,2,9,83,87,1,87,5,91,1,9,91,95,2,10,95,99,1,5,99,103,1,103,9,107,1,13,107,111,2,111,10,115,1,115,5,119,2,13,119,123,1,9,123,127,1,5,127,131,2,131,6,135,1,135,5,139,1,139,6,143,1,143,6,147,1,2,147,151,1,151,5,0,99,2,14,0,0
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}
diff --git a/2019/day3/part1.in b/2019/day3/part1.in
new file mode 100644
index 0000000..c2e5e1d
--- /dev/null
+++ b/2019/day3/part1.in
@@ -0,0 +1,2 @@
1R998,U494,L814,D519,R407,U983,R307,D745,R64,D29,L935,D919,L272,D473,R689,U560,L942,U264,R816,U745,R209,U227,R241,U111,L653,D108,R823,U254,L263,U987,L368,D76,R665,D646,L759,U425,L581,D826,R829,D388,L234,U33,L48,U598,L708,D764,L414,D75,L163,U802,L183,U893,L486,U947,L393,D694,L454,D600,R377,U312,R89,D178,L652,D751,R402,D946,R213,U985,R994,D336,R573,D105,L442,U965,R603,U508,L17,U191,L37,U678,L506,U823,R878,D709,L348,U167,L355,U314,L164,D672,L309,U895,R358,D769,R869,U598,R63,D68,R105,U133,R357,U588,L154,D631,L939,D235,R506,D885,R958,D896,L195,U292,L952,D616,L824,D497,R99,D121,R387,D155,L70,U580,L890,D368,L910,U645,L786,U977,R9,U781,L454,U783,L382,U321,L195,U196,L239,U764,R18,D71,R97,U77,L803,U963,L704,U94,L511,U747,L798,D905,L679,D135,R455,U650,R947,U14,L722,D245,L490,D183,L276,U559,L901,D767,R827,U522,L380,U494,R402,U70,R589,D582,R206,U756,L989,U427,L704,D864,R885,D9,R872,U454,R912,U752,R197,U304,L728,U879,R456,D410,L141,U473,R246,U498,R443,D297,R333,D123,R12,D665,R684,D531,R601,D13,L260,U60,R302,D514,R416,D496,L562,D334,L608,U74,R451,U251,R961,U166,L368,U146,R962,U973,R120,U808,R480,D536,L690,D958,R292,U333,R656,U305,R46,U831,L756,D907,L638,D969,L445,U541,R784,U148,R338,D264,R72,D637,R759,D709,L611,D34,R99,U305,R143,D191,R673,D753,R387,U994,R720,D896,R95,U703,L499,D453,R96,U808,L485,U127,L856,U357,L543,U382,R411,U969,L532,U64,R303,U457,L412,D140,R146,D67,R147,D681,L1,D994,L876,D504,R46,U683,L992,U640,L663,D681,L327,U840,R543,U97,R988,U792,R36
2L999,U148,L592,D613,L147,D782,R594,U86,R891,D448,R92,U756,R93,D763,L536,U906,L960,D988,L532,U66,R597,U120,L273,D32,R525,U628,L630,U89,L248,U594,R886,D544,L288,U380,L23,D191,L842,U394,L818,U593,L195,U183,L863,D456,L891,D653,R618,U314,L775,D220,R952,U960,R714,U946,L343,D873,L449,U840,R769,U356,L20,D610,L506,U733,R524,D450,L888,D634,R737,U171,R906,U369,L172,D625,L97,D437,R359,D636,R775,U749,L281,U188,R418,D437,R708,D316,L388,D717,R59,U73,R304,U148,L823,U137,R265,U59,R488,D564,R980,U798,L626,U47,L763,U858,L450,U663,R378,U93,L275,U472,L792,U544,R192,D979,L520,U835,L946,D615,L120,U923,L23,U292,R396,U605,L76,U813,L388,U500,L848,U509,L276,D538,R26,D806,R685,D319,R414,D989,L519,U603,R898,D477,L107,D828,R836,U432,L601,U888,L476,D974,L911,U122,L921,D401,L878,D962,L214,D913,L113,U418,R992,U844,L928,U534,L13,U457,L866,D208,L303,D732,L497,U673,R659,D639,R430,D301,L573,U373,L270,D901,L605,D935,R440,D183,R374,D883,L551,U771,R707,D141,R984,U346,R894,U1,R33,U492,R371,D631,R95,D62,L378,D343,R108,D611,L267,D871,L296,U391,R302,D424,R160,U141,R682,U949,R380,U251,L277,D404,R392,D210,L158,U896,R350,D891,L837,D980,R288,U597,L292,D639,L943,D827,L492,D625,L153,D570,R948,D855,L945,U840,L493,U800,L392,D438,R8,U966,R218,U597,R852,D291,L840,D831,L498,U846,R875,D244,R159,U243,R975,D246,R549,D304,R954,D123,R58,U5,L621,U767,R455,U550,R800,D417,R869,D184,L966,D51,L383,U132,L664,U220,L263,D307,R716,U346,L958,U84,L154,U90,L726,U628,L159,U791,L643,D652,L138,U577,R457,U655,L547,D441,L21
diff --git a/2019/day3/smalltest.in b/2019/day3/smalltest.in
new file mode 100644
index 0000000..620a05e
--- /dev/null
+++ b/2019/day3/smalltest.in
@@ -0,0 +1,2 @@
1R75,D30,R83,U83,L12,D49,R71,U7,L72
2U62,R66,U55,R34,D71,R55,D58,R83
diff --git a/2019/day3/wires.pl b/2019/day3/wires.pl
new file mode 100644
index 0000000..fa4ddf1
--- /dev/null
+++ b/2019/day3/wires.pl
@@ -0,0 +1,68 @@
1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5use List::Util 'min';
6# use Smart::Comments;
7use Data::Dumper;
8
9sub manhattan_distance {
10 my $ref = shift;
11 my ($x, $y) = @{$ref};
12
13 return abs(0 - $x) + abs(0 - $y);
14}
15
16sub pedometer {
17 my $ref = shift;
18 my ($x, $y, $step_1, $step_2) = @{$ref};
19 return $step_1 + $step_2;
20}
21
22my %directions = (
23 'U' => [0, 1],
24 'D' => [0,-1],
25 'R' => [1, 0],
26 'L' => [-1,0]
27);
28
29my $turn = 0;
30my @intersections = ();
31my %map;
32
33while(<>) {
34 chomp;
35 my @path = split /,/;
36 my @coord = (0,0); # Starting point
37 my $num_steps = 0;
38
39 if (!$turn){
40 foreach my $way (@path) {
41 my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!";
42 for (1..$speed) {
43 $coord[0] += @{$directions{$dir}}[0];
44 $coord[1] += @{$directions{$dir}}[1];
45 $num_steps++;
46 $map{"$coord[0],$coord[1]"} = $num_steps;
47 }
48 }
49 $turn = !$turn;
50 } else {
51 foreach my $way (@path) {
52 my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!";
53 for (1..$speed) {
54 $coord[0] += @{$directions{$dir}}[0];
55 $coord[1] += @{$directions{$dir}}[1];
56 $num_steps++;
57 if (exists $map{"$coord[0],$coord[1]"}) {
58 push @intersections, [@coord,$num_steps,$map{"$coord[0],$coord[1]"}];
59 }
60 }
61 }
62 }
63}
64
65# print Dumper \@intersections;
66print(min(map {pedometer($_);} @intersections));
67
68
diff --git a/2019/day4/password.lp b/2019/day4/password.lp
new file mode 100644
index 0000000..b9a1f1a
--- /dev/null
+++ b/2019/day4/password.lp
@@ -0,0 +1,17 @@
1%353096-843212
2digit(X) :- X = 0..9.
3place(N) :- N = 1..6.
4
51 {pass(Q,X) : digit(X)} 1 :- place(Q).
6value(N) :- N = A + B * 10 + C * 100 + D * 1000 + E * 10000 + F * 100000, pass(6,A), pass(5,B), pass(4,C), pass(3,D), pass(2,E), pass(1,F).
7
8:- value(N), N < 353096.
9:- value(N), N > 843212.
10
11:- pass(Q,X), pass(Q', Y), Q' = Q + 1, X > Y.
12%:- pass(Q,X), pass(Q', Y), pass(QQ, Z), Q' = Q + 1, QQ = Q' + 1, X = Y, Y = Z. % I couldn't figure out this so I did 581 - 2
13
14%1 {target(X,Y,XX,YY) : dir(DX,DY), XX-X = DX*S, YY-Y = DY*S, size(S), area(XX,YY,_)} 1 :- number(X,Y,_).
15
16#show pass/2.
17#show value/1.
diff --git a/2019/day4/password.pl b/2019/day4/password.pl
new file mode 100644
index 0000000..403cf91
--- /dev/null
+++ b/2019/day4/password.pl
@@ -0,0 +1,29 @@
1use strict;
2use warnings;
3
4use Data::Dumper;
5
6my $low_bound = 353096;
7my $upper_bound = 843212;
8
9my $count = 0;
10
11for (my $number = $low_bound; $number < $upper_bound; $number++) {
12 my @nums = split //, $number;
13 my @cmp = sort {$a <=> $b} @nums;
14 if (@nums == @cmp and join ("\0", @nums) eq join ("\0", @cmp)) {
15 my %digits = ();
16 foreach (@nums) {
17 $digits{$_}++;
18 }
19 foreach my $reps (values %digits) {
20 if ($reps == 2) {
21 print "$number\n";
22 $count++;
23 last;
24 }
25 }
26 }
27}
28
29print ">$count\n";
diff --git a/2019/day5/intcode.pl b/2019/day5/intcode.pl
new file mode 100644
index 0000000..613f067
--- /dev/null
+++ b/2019/day5/intcode.pl
@@ -0,0 +1,92 @@
1use strict;
2use warnings;
3use Data::Dumper;
4# use Smart::Comments;
5use v5.10;
6
7sub pos {
8 my ($tape_ref, $index) = @_;
9 my @tape = @{ $tape_ref };
10 ### returning: $tape[$tape[$index]]
11 ### for: $index
12 return $tape[$tape[$index]];
13}
14
15sub imm {
16 my ($tape_ref, $index) = @_;
17 my @tape = @{ $tape_ref };
18 return $tape[$index];
19}
20
21my $inputline = <STDIN>;
22chomp $inputline;
23my @tape = split /,/, $inputline;
24
25my $pc = 0; # program counter is no longer consistent
26
27my @actions = (
28 sub { print "noop" }, # no opcode 0
29 sub { return $_[0] + $_[1] }, # 1
30 sub { return $_[0] * $_[1] }, # 2
31 sub { return 5; }, # 3
32 sub { say $_[0]; }, # 4
33 sub { return $_[0] ? $_[1] : -1 }, # 5, ugh, you might want to jump to 0
34 sub { return $_[0] ? -1 : $_[1] }, # 6
35 sub { return $_[0] < $_[1] ? 1 : 0}, # 7
36 sub { return $_[0] == $_[1] ? 1 : 0}, # 8
37);
38
39my @modes = (\&pos, \&imm);
40my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/;
41my $inst_ptr = 0;
42
43while ( 1 ) {
44
45 my $raw_op_code = $tape[$inst_ptr];
46 last if ($raw_op_code == 99);
47
48 # print("====================================\n");
49
50 my @modes_and_opcode;
51 push @modes_and_opcode, $_ // 0 for $raw_op_code =~ m/^(\d)??(\d)??(\d)??0?(\d)$/g;
52 ### @modes_and_opcode
53
54 my $op_code = pop @modes_and_opcode;
55 ### $op_code
56
57 # foreach my $x (0..5) {
58 # print("TAPE[" . ($inst_ptr + $x) . "] = $tape[($inst_ptr + $x)]\n")
59 # }
60
61 my $toread = $offsets{$op_code} - 1; # excluding opcode
62 my @params;
63 foreach my $offset (1..$toread) {
64 push @params, $modes[ (!($op_code == 4 || $op_code == 5 || $op_code == 6) && $offset == $toread) ? 1 : pop @modes_and_opcode ]->(\@tape, $inst_ptr + $offset);
65 }
66 ### @params
67
68 $inst_ptr += $offsets{$op_code};
69
70 if ($op_code == 1 || $op_code == 2 || $op_code == 7 || $op_code == 8) { # arithmetic
71 my $res = $actions[$op_code]->($params[0], $params[1]);
72 $tape[$params[2]] = $res;
73 ### writing: $res
74 ### on address: $params[2]
75 }
76 elsif ($op_code == 3) { # input
77 my $res = $actions[$op_code]->();
78 $tape[$params[0]] = $res;
79 ### saved: $res
80 ### on: $params[0]
81 } elsif ($op_code == 4) { # output
82 $actions[$op_code]->($params[0]);
83 } elsif ($op_code == 5 || $op_code == 6) { # jumps
84 my $res = $actions[$op_code]->($params[0], $params[1]);
85 $inst_ptr = $res == -1 ? $inst_ptr : $res;
86 ### jumped to: $inst_ptr
87 next;
88 } else {
89 ### OH NO...
90 die;
91 }
92}
diff --git a/2019/day5/part1.in b/2019/day5/part1.in
new file mode 100644
index 0000000..9d9b84d
--- /dev/null
+++ b/2019/day5/part1.in
@@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1002,114,19,224,1001,224,-646,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,40,62,225,1101,60,38,225,1101,30,29,225,2,195,148,224,1001,224,-40,224,4,224,1002,223,8,223,101,2,224,224,1,224,223,223,1001,143,40,224,101,-125,224,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,101,29,139,224,1001,224,-99,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,14,34,225,102,57,39,224,101,-3420,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1101,70,40,225,1102,85,69,225,1102,94,5,225,1,36,43,224,101,-92,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,1102,94,24,224,1001,224,-2256,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1102,8,13,225,1101,36,65,224,1001,224,-101,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,677,226,224,1002,223,2,223,1006,224,329,1001,223,1,223,1108,226,226,224,1002,223,2,223,1005,224,344,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,359,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,374,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,389,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,404,101,1,223,223,1008,226,226,224,1002,223,2,223,1006,224,419,101,1,223,223,108,677,226,224,1002,223,2,223,1006,224,434,101,1,223,223,1108,677,226,224,102,2,223,223,1005,224,449,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,108,677,677,224,102,2,223,223,1005,224,479,101,1,223,223,7,677,677,224,102,2,223,223,1005,224,494,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,509,101,1,223,223,107,677,226,224,1002,223,2,223,1005,224,524,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,539,1001,223,1,223,1007,226,677,224,1002,223,2,223,1005,224,554,1001,223,1,223,8,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,584,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,599,101,1,223,223,1007,677,677,224,1002,223,2,223,1006,224,614,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,629,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,659,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226
diff --git a/2019/day6/orbits.pl b/2019/day6/orbits.pl
new file mode 100644
index 0000000..9f4aed5
--- /dev/null
+++ b/2019/day6/orbits.pl
@@ -0,0 +1,39 @@
1use strict;
2use warnings;
3
4my $file_name = $ARGV[0];
5
6if (not defined $file_name) {
7 die "missing filename\n";
8}
9
10open my $fh, "<", $file_name or die "Can't open $file_name, $!\n";
11
12my %orbit;
13
14while (<$fh>) {
15 chomp;
16 my ($from, $to) = split /\)/;
17 push @{ $orbit{$from} }, $to;
18}
19
20close $fh;
21
22my @to_value = ('---', 'COM');
23my $dist = 0;
24my $total = 0;
25
26while ( 1 ) {
27
28 my $cur = pop @to_value;
29
30 print $total and exit unless @to_value;
31
32 if ($cur eq '---') {
33 unshift @to_value, '---';
34 $dist++;
35 } else {
36 $total += $dist;
37 unshift @to_value, @{ $orbit{$cur} } if exists $orbit{$cur};
38 }
39}
diff --git a/2019/day6/orbits.py b/2019/day6/orbits.py
new file mode 100644
index 0000000..652256f
--- /dev/null
+++ b/2019/day6/orbits.py
@@ -0,0 +1,28 @@
1# this solves part 2
2import sys
3
4import networkx as nx
5
6
7def main(argv):
8
9 G = nx.DiGraph()
10 with open(argv[0]) as fp:
11 for line in fp:
12 line = line.rstrip("\n")
13 start, end = line.split(")")
14 G.add_node(start, label=start)
15 G.add_node(end, label=end)
16 G.add_edge(start, end)
17
18 jump_to = nx.lowest_common_ancestor(G, "SAN", "YOU") # SAN and YOU are given
19 jumps = (
20 len(nx.shortest_path(G, jump_to, "SAN"))
21 + len(nx.shortest_path(G, jump_to, "YOU"))
22 - 4
23 ) # 4 because len includes start and end, so -2 times 2
24 print(jumps)
25
26
27if __name__ == "__main__":
28 main(sys.argv[1:])
diff --git a/2019/day6/part1.in b/2019/day6/part1.in
new file mode 100644
index 0000000..8cafbd0
--- /dev/null
+++ b/2019/day6/part1.in
@@ -0,0 +1,1069 @@
1WGB)S14
2WN4)27C
318L)M18
41HY)6ZP
5TQ9)KQ6
6HQ3)HH1
7FLC)F1Z
8D6R)ZPC
92VD)GK3
10YY3)3TP
11PBL)3CK
125K4)CB5
13V5M)CNN
14L4T)RHS
15HHH)66F
16Q3Y)DTL
17DGN)YY3
18CCT)L3B
19Z6X)FM2
202QQ)VK9
21MX3)C9J
224JK)BPX
238BP)N13
24PBW)6Z6
252LT)DT9
26JHX)GXM
275LW)BHQ
28DNK)ZBT
2929Z)T9D
30WNP)TDC
31S38)GL6
32DW9)V2F
334MG)3FW
34Z9Z)CPK
35FKL)QNH
3655D)HT2
37D1D)N4Q
38Y7W)1Y8
39SFQ)79W
40JSR)62W
414WN)J18
42VK9)J2H
43LS5)DCX
446LR)P4X
45HDV)DGQ
461K9)KD1
472PX)17C
48KSB)GL8
49B4S)VTV
50ZW1)KNR
51BVH)43P
52VKP)6L3
53P5K)MHR
54XHR)STT
55WBG)5X5
56HZF)8JQ
57B47)NW4
58J5V)3ZW
59KGP)VVR
6024K)PK8
6131V)LXC
625XG)RHP
63P1G)HN8
64R76)3GY
655CH)17Q
66TVC)XJM
67598)RD3
68J66)LKC
694DY)YSQ
70M4Y)NLL
71SMP)M2M
72TBR)WNP
73K22)KGP
74MQ5)8MN
75B9Q)6HQ
76P9S)X92
77TJK)ZQK
78XS7)7KL
79H6J)DX1
80MTP)3Z6
81B17)B7P
82S12)PC2
8347V)5KW
84KCY)HWP
85FB2)S38
86V5M)FNT
87GXM)QPR
88HXR)2R2
892LV)NDP
906HQ)12S
9122P)4HL
92T8Q)9FB
938YW)TVZ
94DR1)NNN
959TH)87Z
9679W)TM2
975GB)HQM
981HY)4WN
99LFV)RYJ
100YCN)ZMK
1018SR)SB3
102P9H)PH9
103ZGQ)T3J
104KWW)1HY
105TLF)RPG
106PFD)HZR
1079SF)7PY
108DCX)VCC
109D1R)2RB
110GXC)NN9
111ZZW)SCC
112G44)Q8D
113923)3J2
114KY2)8F4
1151XQ)7LD
116GHX)Q6M
117TZ5)V32
118LM9)1XK
119Q7N)Z7H
120YKD)73H
1219RZ)C2Q
1225KN)P1G
1233FJ)L73
124ZPC)VYT
125Y7D)FFY
126C8W)J1Y
127X5T)55D
128Z3F)GK8
129WRS)PRR
130T9M)JK2
13181P)5WT
1327KL)5BK
133S3R)VCD
13456L)D1R
135PR2)92L
13691F)2F4
137ND4)PJ6
1389KY)YD4
139CLH)5D7
140J2F)L7Z
141M4Y)PYG
142891)P34
143VV6)18L
144RQQ)X8P
1457SR)8G6
146WJ8)CDL
1479FB)TXD
148RKK)2H5
1493W8)2QQ
15027C)YFC
151RZZ)91F
1524CP)BWH
153T4L)LS5
154788)G7S
15547V)3W8
156FGK)719
15716Q)4KW
1585H6)PC9
159KGS)TBR
16044Y)BVH
161GMF)VFM
162LKC)PM4
163DPL)DXS
1642WD)X5T
165XWX)NYR
166N44)Y36
16772S)56L
168W25)4MG
169P9S)HBC
170W84)3YP
171NW4)S78
17258Y)LDB
173QJ9)VV1
1745D3)4Q7
175T3J)5M4
176394)ZW7
177JXL)QVK
1787KL)FTL
179885)ZGQ
18058Y)8SR
181GXN)PBW
182HH1)JB4
183H6J)W95
184VYK)SQS
185CCS)7CZ
186PJ7)NLR
1872VW)MSP
188ZWK)H6X
189HJ4)C1S
190H41)1L3
1918B9)64N
192RZR)WBJ
193FNT)VL7
194K5M)S5Q
195XJM)TC7
196QWT)7Q5
19743P)MY2
198YP7)51N
199TDX)FWZ
200DB3)NCK
20137M)H2L
202Z3X)XRS
203SGV)R2T
204Y2F)63M
205ZVY)JTX
206DJB)KQD
207848)FQP
208SX3)FM5
209PH9)ZPG
21075S)Z9L
211GPD)Y7D
2129Y6)52N
213SL4)S3R
2144TH)T6F
215K4V)D8V
21689S)18F
217GDN)WN4
2186HT)TQZ
219V1Q)JVG
220R55)2LC
221KH3)NT5
222Q53)3DN
223SRV)JND
224XMC)MKK
225T5J)6HT
226HZR)M1M
227P34)3RY
228HF6)SD2
229PTM)C9X
2303MZ)T9M
231R76)MFF
232B9Y)3MC
233NFG)5FC
234M63)4CP
235FRG)PVQ
23658Z)GDM
237ZT8)4L5
238F5B)KF3
239SQT)NTZ
240M2M)252
24135Y)5WF
242C9J)8BP
243W8H)F78
244H8Z)2VW
24591P)5FP
246VTV)YN7
2472KM)1K9
248KSX)TR8
2499DK)XD6
250MFF)KQR
251414)6L9
252FQ7)T5K
253G8M)WPX
254794)FMS
255WZV)XS7
256VVR)5CH
257R8G)9RH
258B4D)2RT
259PJ6)GWB
26063M)NHF
2618G7)8B9
262QP5)9ZW
263FW6)CDM
264S5Q)172
265T24)TPD
266YRT)GMF
2671TJ)SGV
268RV3)C3D
269661)MHS
270QYT)D2K
271T49)MFP
272GY7)T2Q
273686)Z4G
274J49)R9L
275R5S)67X
276L7Z)5RM
277RPP)WG9
2785KW)2KM
2795N2)Y7W
280Z3X)JFD
281KD8)4H5
2825MP)RVK
28312S)S2Y
284TPD)D5F
28551N)81P
286DCH)SGQ
287L6N)VKP
2882XQ)6LR
2893DN)S3L
290VS4)83N
2918DJ)WZP
292DCX)FX1
293SF9)Z3F
294R49)S99
295D1C)794
296TKN)L83
29721R)GP3
2985RM)TG3
299ZMK)R49
3001QT)152
3019DX)GXC
302GYC)TQ9
303JND)LMK
304D8Z)SCW
305VNZ)VS4
306C1S)9RZ
307LKF)D8Z
308G4J)R44
30992L)J66
31088P)657
3118Z5)R55
312VV1)KRY
313N44)2QK
314KBC)KKG
31591P)L6N
316SVH)7W3
317P9Z)34H
318BWH)9TH
319JNX)RZZ
320YFG)ZT8
321DSM)FF3
322BMK)ZR6
3237W3)V82
324T9D)H2S
3252QF)PFD
326NDQ)F13
327ZVB)MX6
328KRY)7FB
329KKG)HJ4
330QNH)MFQ
3315X5)VQM
332HQM)HF6
333HLT)TD2
334WV4)FWH
335N2T)5B5
336D1R)P89
337HKT)3MZ
338ZQK)1DK
339QQQ)FLC
34073Z)TTM
341ZZW)769
3428G7)TYL
343MFP)WMS
344RQS)2YC
345NLL)JHX
346KCY)CSP
3479F8)51H
348SGQ)B27
3494KM)VYK
350JDY)MTW
351T8Q)DB3
3521VL)VV6
353VV5)B4D
354SPF)JR5
355LYS)6CK
356YMK)2VD
357TD2)1VL
358JKH)QHX
359VD4)58J
3609QQ)HKL
3618JP)HQ3
362NHS)31S
36381Z)Q5W
364R7Q)Z9M
365WMS)ZK2
3663J2)GY7
367MFQ)CLH
368S14)934
369HY7)YBT
3704SY)63F
371NQF)PPQ
372T9W)RZR
373WL2)6QM
374LZV)WRQ
375TVZ)T9P
3764X5)GN5
377NQ8)FPQ
378J5J)K51
379Y8T)WGM
380FPQ)B53
3811XK)TKX
382XDW)72V
383WW8)9QQ
384XX7)Q7N
385CDM)GHX
386VCC)HPP
387QRK)56B
388MTW)2QT
3897V5)58Z
390PYY)T24
3919HB)J8F
392TTM)PTB
393FF3)ZY3
394ZW7)D1D
395T4H)ZTG
3962PW)DSM
3979WB)4TH
39817C)FKX
399T6F)QP3
400G6R)XHR
401H5T)QYT
402DX1)Q9L
403GJF)ZF3
404LJP)JXL
405QHX)3XY
406DNF)8KQ
4078Q1)NDQ
408GP3)6MY
409FPQ)QQQ
410XRS)923
411Q6M)7BS
412K21)B47
413TQZ)WJ4
4149PB)3PQ
4158G6)X7M
416L3B)YOU
417L5V)G2L
418B8Y)JVS
419GL6)MTP
4209QZ)NRN
421486)T8Q
422HNN)PNM
423NFK)B4S
424G9C)LHT
4254K9)SL4
4268X5)179
427VQM)47V
428CNJ)J4R
429ZD5)2PX
4309TQ)X9Q
431Q3Q)9DK
43217Q)1KX
4335GN)24K
434K5Q)1NF
435LCK)9WB
436TYL)PYL
4377XG)R2L
438LXC)ZWK
439Q62)SPF
44089C)N7Z
441GK8)GR7
4426X1)5N2
443XM8)8Q1
444MCD)GXQ
445S2Y)N7G
446CB5)C8W
447NHF)44B
448QPR)GF5
449HGX)YMY
4503FW)2LV
4515WF)RQQ
452841)N31
453Q9L)876
454WQ8)HZ7
4556K1)QVC
456C2T)FQX
457J3M)HLT
458H2S)K5Q
459STQ)8ZF
460VDX)NQF
461YSR)G8M
462CSL)NLF
463MHS)3FH
464YN7)VWC
465RSW)X11
466FXS)L54
467YBT)HX4
468BHQ)FRG
46983H)K4Q
470NT5)2ZB
471GWB)4K9
472YMY)5KN
4734Q7)C3G
474D3J)HZF
47532D)GN7
476VGG)G3D
477LVG)JXR
47825V)GDN
479L6V)KL8
480FW2)STQ
481V6H)8G7
482COM)CB6
4836Z6)SQT
484W81)6M3
485D2K)XF6
4862NX)9KY
487KRQ)LKF
488P1B)VVQ
489QTV)Q3Y
490DTZ)ZLY
491R3T)FQ7
492D92)72S
493H8N)9Y6
494FWZ)WGB
495VQW)LHJ
4962HB)848
4979ZW)NT8
498NLR)QTV
49931V)DSZ
50092J)WXY
5018LK)QQ3
502769)ZD5
5038L9)T5J
504TB2)V5M
505VZQ)57T
506Z7H)JMR
50794D)YCN
508ZPF)6WK
509M1F)6C2
510MHV)ZCS
511Q53)FBC
512RPG)P3N
513RHS)JDY
514FTL)FB2
515J47)R3T
516Y9S)4JK
517ZVY)TK6
518LTX)BM5
519D8V)3R6
520J18)S19
521PVQ)WL2
522ZPV)QFW
523719)CSV
524XK9)9Q9
525BM5)L5V
526LDF)WZT
527MSP)DTZ
528HRQ)JBG
52919C)GSP
530GPB)HGX
5312F4)8HL
532886)C8J
533ZF3)LM9
534NQ1)394
535WM9)M79
536PM4)GNT
5376J4)ML2
5385WT)HQS
539KQD)5K4
540JBT)V1Q
541JVS)DDT
5423G2)52L
5438ZF)D6R
5444BQ)5H6
545G7T)7T6
546ZY3)83H
547HYC)G9C
548MX6)XC9
5492NW)2SH
550YXJ)JSR
551QNH)YJN
552TG3)886
553N7Z)G98
5545D7)KNW
5558TN)KH3
556C78)TSW
55787Z)DFM
558QGG)Q53
559NRN)YP7
560TTB)C2T
561ZLY)25P
5627KS)D5X
563LNX)CNJ
564QVK)YMK
565CNN)N43
5665Q9)MWG
567SCC)XFV
568885)G7T
5694BS)4BQ
570N4Q)Z6X
571FQX)7W4
572MLL)NF8
57352N)PZ2
574DNF)KBC
5756C2)CCS
576LZ8)P1B
577CSV)686
578PZ2)KKC
579JMR)327
5803TP)N6L
5813W8)YFG
582S62)J5V
583FF3)VXL
5844X4)MHQ
5853TP)7Z7
586L83)VDN
587Q8D)2HB
588JB4)5LR
589VYT)SAN
590L54)X63
59115J)XF2
592FWZ)WLR
593R44)K5M
594TK6)Q5J
595J81)QP5
596114)BGP
597QQ3)PJ7
598D5F)HNN
599MFF)WW8
600J18)MP3
6019JN)M8G
6022YC)CSL
603R2T)4TS
604ZBT)WQ8
605XFV)MPD
606R9S)XDW
6078HL)99X
6084MG)2QF
6098X5)BMK
610CN7)KSB
611YJN)44Y
612X11)WWY
6135MP)VDX
614R2L)PFY
6156ZP)HPW
616WGM)GPB
617WCZ)KCY
618NYR)TKN
6191L3)SKC
620MND)S5K
62117N)2D1
622VL7)16Q
6235FP)J5J
624NBL)TLF
625QDV)Z9Z
6262S1)VXW
627K22)G6R
628DTL)9Z8
629BXN)YXJ
630VYW)LNX
631WJ4)4LM
632JTX)DPL
633SLM)DNF
634YM4)J37
6354L5)BF4
6362RT)8DD
637FNB)KD8
638PK8)9PD
639RLR)4SY
640TM2)661
641PQ6)2FF
64292J)XMC
643GDM)21R
644ZTG)VV5
645X3B)ZZW
6465XQ)H6J
647WTL)W1N
648PNM)H8Z
6496MY)CG5
65072V)RLR
6513R6)WCZ
652PF8)YRM
653HPY)88T
654X8P)MHN
6557LY)6VJ
6562FF)C9G
657K4Q)4BS
658X63)2WD
659XM8)R8Q
660G98)Z3X
66144B)8DJ
662PWZ)V1P
663MDD)7ZX
664RRD)T49
665YBJ)DBX
666GXQ)BXP
6676FX)88P
668WXZ)H22
66918F)WTL
670NQF)885
671L4T)XM2
672V82)4DY
6736N6)TDX
674172)QRK
675N6L)23T
676CSP)5MP
677GLG)9TQ
6789P6)R76
679W1N)N3F
680R8G)JZ1
681H2L)F2D
682TGT)C9S
6837BS)2XQ
684FWH)TVM
68523T)K3L
686WQX)37M
6873ZW)QH7
688BGP)FW6
689YFC)FGH
690JCF)94D
691WRQ)9JN
692GN5)6RM
693V6H)QGG
694C1S)XK9
695FFY)9SF
696WPX)HKT
6977Y1)NBL
6986RN)GPD
699NYJ)YDT
700934)FKL
701P3N)FXS
702ZQS)KXC
7034KW)PVJ
704FMS)V26
705NT8)WM9
7067Z7)G4J
707NN9)2TT
708VXW)DJB
709C97)Q3Q
710VVQ)486
711CB6)DGN
712DGQ)WMN
713FM5)GJF
7146YY)DPN
715DDT)814
716KF3)SFQ
717G7S)TTB
718R4C)S12
719R8Q)35Y
720GXN)JKH
721J2H)K22
722F5B)7LY
723NCK)YSR
724SMP)KY2
725P4X)X3B
726DR5)LZ8
7278JQ)ZPV
728WG9)SX3
729NDQ)LM2
73032S)LFV
731K51)ND4
732DP3)QDV
7332W7)CCT
734RSK)YH6
7359NZ)NSB
736K3L)RV3
737HN8)414
73892N)75S
73961M)598
740ZR6)53T
741J8F)TB2
742H22)5YX
743HSJ)M63
7444KW)5GB
745HR7)89C
746FQP)15J
747TZ5)LYS
7485FP)WG4
7494HL)PBL
750C8J)D1C
751TS3)83C
752C3G)1QT
753GZD)DNK
7542RB)KSF
755BCD)SF9
756327)9QZ
7574FF)ZQS
7586L9)82F
759TJK)123
760X9M)FMY
761R92)TV1
762CDL)2PW
7637ZX)58Y
764C2N)H5T
7658MN)TH2
766GN7)G44
767HKL)61M
768XD6)1C9
769ZCS)NQ8
7702L8)QGH
771DFC)XX7
772S5K)XM8
77358J)8L9
774PRR)4KM
7756XT)N2T
776FM2)7Y1
777V26)HR7
7782TT)91P
77988F)ZW1
780JBG)891
781WZT)VZQ
782PYG)NHS
7832QT)P9H
784FB2)9QT
785MP3)PQ6
786WZV)YBJ
787H53)VYW
788N2T)VQW
789ZK2)6RT
790SQS)5XQ
791DPN)W8H
792TSW)2L8
79373H)R7Q
794F1Z)B9Q
795M8G)9KS
796NSB)H41
797WBJ)THR
798KSF)KGS
799PXH)3BS
8004NT)ZRV
801VNZ)8X5
80298S)DR1
8033XY)TJK
804JNX)P9Z
805F4P)6YY
806VRT)VKV
807TVM)92N
808WLR)S62
809D5X)ZVB
810152)9P6
811F2D)PN3
8122R2)D6K
813ZRV)2NX
81467X)TS3
815HWP)YBD
8165FC)SRV
817X92)CQR
8188N9)DFC
819Q5J)H8N
820GGG)8LK
821PPQ)841
8226RT)WJ8
823KQR)788
82492N)Q62
825W7S)98S
826S19)NFK
827VRM)ZVY
828GL8)DCH
8294YM)17N
830F13)D3J
831QVC)VGG
83231S)J81
833934)93X
834Q21)R4C
835TH2)HQ4
8361C9)114
83783N)X5B
838S3L)T4L
839SD2)ZWX
840SC1)6K1
841TXD)ZPF
8423GY)NQ1
843Z9M)9NS
844D6R)7V5
845WG4)C2N
846SVH)JBT
847TR8)K4V
848MPD)7SR
849Y36)DP3
850LM2)K21
851KD1)2S1
852FC8)J3M
853JFD)BWF
8546Y3)88F
855STT)GXN
856KKC)DW9
85752L)B3G
8585BK)XWX
859H6X)5BQ
860YH6)FGK
861VM3)6RN
862BWH)1PZ
863JK2)BCD
8649QT)LTX
8656WK)F5B
866HYC)C97
867GSP)3MR
8686M3)B9P
8697LD)9NZ
870N31)7KS
871GGG)9PB
8726RM)SC1
87383C)TGT
874BWT)1TJ
8757YH)CN7
876JLD)PFT
877W95)JNX
878ZBT)Y2F
879TPD)RSK
8803BS)BR6
881KG9)X9M
8826HL)5D3
883WMN)P9S
884V32)V6H
885RYJ)3HM
886SF9)SBK
887HQ4)19C
888BWF)2W7
8892LC)91R
890VCD)3G2
891VKJ)9BJ
892NL2)VKJ
893NLF)HY7
894BR6)L6V
895TQ4)8N9
896QH7)W84
897D6K)GGG
898W1J)SLM
899DB2)RQS
900M18)RRD
901DXS)32S
902KL8)YRT
903Q5W)VD4
904SCC)WQX
905GQW)VRT
90652N)J47
907Z9M)6FX
908FKX)GSK
909TKX)WRS
9107YH)NFG
9119Y6)WZV
9127BS)48W
913NT5)KSX
9141Y8)RYV
915814)J2F
916GSK)KRQ
917FBC)W1J
918C9G)VRY
919V2F)8TN
9205M4)Y9S
921X9Q)T71
922Q7G)9HB
923YLR)4YM
92418L)29D
925M1M)1BT
92682F)MX3
9279BJ)BBL
928R55)HRQ
9299RH)R5S
930414)MHV
931ML2)Y8T
932179)7YH
933GK3)5Q9
9347CZ)VRM
935QP3)LDF
936YRM)D92
937KXC)B8Y
9382ZB)6X1
939XWX)JCF
940B3G)CG7
941LHT)4NT
942NNN)5LW
94348W)R9S
94491R)6J4
94597T)BBF
9461BT)KWW
947B53)QL9
948HBC)DB2
9492D1)89S
950BBL)WBG
9517WK)LJP
952TV1)SNJ
953K5Q)L9Q
954HNN)YKD
955BXP)W81
95664N)GLG
957L9Q)M4Y
958PTB)8JP
959RHP)3FJ
960657)PWZ
961HZ7)T8S
962YKD)W25
963PYL)SMP
964YSQ)HSJ
965W84)9F8
966HQS)NL2
9673MC)6Y3
96824K)MDD
969G3D)B17
9702QF)PXH
97138D)FNB
972614)SVH
97334H)BWT
9746L3)GZD
975J1Y)QJ9
9763XF)GG9
977MHQ)25V
9783PQ)L4T
97957T)3W4
9808KQ)614
981PC9)P5K
982C2Q)F4P
983J37)FW2
984TDC)M1F
985YD4)LZV
9864H5)HYC
987VRY)5XG
9883W4)YLR
989JZ1)Q21
990SBK)1XQ
991RVK)6XT
9929NS)LVG
993JBT)612
994J4R)QWT
995YDT)31V
996123)C78
997PN3)PTM
9986QM)5GN
999CG7)PF8
1000HX4)T4H
10016CK)HXR
1002PFT)H53
100362W)FC8
1004KQ6)4X5
10053CK)8Z5
1006RHS)Q7G
1007N13)2LT
1008D3J)NYJ
1009PFY)N44
1010N3F)4FF
1011P89)29Z
1012MHN)J49
1013S78)LCK
10148DD)DR5
101551H)VM3
10161NF)MLL
1017S99)RKK
10189PD)JLD
10197Q5)7WK
10207WK)MND
1021CQR)KG9
1022X5B)VTN
1023SCW)RPP
1024BF4)3XF
1025QL9)R8G
1026LDB)4X4
1027VDN)73Z
10289KY)BXN
1029VTN)WJ1
10308F4)PR2
1031F78)92J
10327SX)GQW
1033GR7)22P
103456B)7SX
1035876)HPY
1036HNP)V7Y
1037SNJ)HHH
1038KNR)W7S
1039HT2)T9W
1040PC2)B9Y
10416VJ)HNP
10424TS)32D
10437FB)TZ5
10445BQ)PRT
1045PRT)97T
1046G44)9DX
1047THR)81Z
104825P)MCD
1049Z9L)R92
1050V1P)L1D
1051DSZ)TVC
1052VKV)TQ4
10534LM)YM4
1054T8S)6N6
1055HPP)WV4
105699X)8YW
1057B9P)HDV
1058T2Q)TNM
1059JVG)7XG
1060QGH)KTD
1061DBX)VNZ
1062DFM)RSW
106329D)GYC
1064LYS)38D
1065MKK)WXZ
10665B5)MQ5
1067BPX)2NW
10685LR)PYY
1069VXL)6HL
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 @@
1from itertools import permutations
2import os
3import subprocess
4
5perm = permutations(range(5))
6res = []
7
8for 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
19print(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 @@
1use strict;
2use warnings;
3use Data::Dumper;
4# use Smart::Comments;
5use v5.10;
6
7$| = 1;
8
9sub 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
17sub imm {
18 my ($tape_ref, $index) = @_;
19 my @tape = @{ $tape_ref };
20 return $tape[$index];
21}
22
23my $file_name = $ARGV[0];
24
25if (not defined $file_name) {
26 die "missing filename\n";
27}
28
29open my $fh, "<", $file_name or die "Can't open $file_name, $!\n";
30
31my $inputline = <$fh>;
32chomp $inputline;
33my @tape = split /,/, $inputline;
34close $fh;
35
36my $pc = 0; # program counter is no longer consistent
37
38my @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
56my @modes = (\&pos, \&imm);
57my %offsets = qw/1 4 2 4 3 2 4 2 5 3 6 3 7 4 8 4 99 1/;
58my $inst_ptr = 0;
59
60while ( 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