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/day1/README.md | 5 + 2019/day1/part1.in | 100 +++++ 2019/day1/part2.in | 100 +++++ 2019/day1/rec_fuel.py | 23 ++ 2019/day2/1202.in | 1 + 2019/day2/intcode.pl | 57 +++ 2019/day3/part1.in | 2 + 2019/day3/smalltest.in | 2 + 2019/day3/wires.pl | 68 +++ 2019/day4/password.lp | 17 + 2019/day4/password.pl | 29 ++ 2019/day5/intcode.pl | 92 +++++ 2019/day5/part1.in | 1 + 2019/day6/orbits.pl | 39 ++ 2019/day6/orbits.py | 28 ++ 2019/day6/part1.in | 1069 ++++++++++++++++++++++++++++++++++++++++++++++++ 2019/day7/comm.py | 19 + 2019/day7/intcode.pl | 109 +++++ 2019/day7/part1.in | 1 + 19 files changed, 1762 insertions(+) create mode 100644 2019/day1/README.md create mode 100644 2019/day1/part1.in create mode 100644 2019/day1/part2.in create mode 100644 2019/day1/rec_fuel.py create mode 100644 2019/day2/1202.in create mode 100644 2019/day2/intcode.pl create mode 100644 2019/day3/part1.in create mode 100644 2019/day3/smalltest.in create mode 100644 2019/day3/wires.pl create mode 100644 2019/day4/password.lp create mode 100644 2019/day4/password.pl create mode 100644 2019/day5/intcode.pl create mode 100644 2019/day5/part1.in create mode 100644 2019/day6/orbits.pl create mode 100644 2019/day6/orbits.py create mode 100644 2019/day6/part1.in create mode 100644 2019/day7/comm.py create mode 100644 2019/day7/intcode.pl create mode 100644 2019/day7/part1.in 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 @@ +I realized part 1 can be solved with a perl one liner; + +```bash +perl -ne '$c += int ($_ / 3) - 2;END {print $c}' < part1.in +``` 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 @@ +93912 +138996 +112824 +110011 +139024 +132292 +74029 +81664 +138077 +109614 +121056 +136338 +132771 +86611 +131526 +123101 +61315 +93900 +62070 +97957 +67168 +119464 +119066 +111076 +56856 +144203 +109400 +120187 +57915 +143353 +71308 +67695 +141275 +106552 +136209 +86990 +98969 +57207 +99103 +71940 +63145 +91765 +121095 +139700 +128851 +77138 +66712 +91318 +96924 +132235 +99897 +67479 +87996 +121100 +55411 +61715 +130658 +121030 +141445 +83939 +90402 +121107 +59618 +120112 +58140 +103514 +90538 +55552 +142739 +61770 +147374 +80038 +128830 +93328 +52369 +71801 +144536 +147140 +118213 +128056 +92155 +114384 +89234 +124451 +94214 +79174 +108427 +111041 +96715 +128414 +62521 +93897 +107428 +90637 +126176 +78676 +69504 +93663 +80869 +124230 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 @@ +93912 +138996 +112824 +110011 +139024 +132292 +74029 +81664 +138077 +109614 +121056 +136338 +132771 +86611 +131526 +123101 +61315 +93900 +62070 +97957 +67168 +119464 +119066 +111076 +56856 +144203 +109400 +120187 +57915 +143353 +71308 +67695 +141275 +106552 +136209 +86990 +98969 +57207 +99103 +71940 +63145 +91765 +121095 +139700 +128851 +77138 +66712 +91318 +96924 +132235 +99897 +67479 +87996 +121100 +55411 +61715 +130658 +121030 +141445 +83939 +90402 +121107 +59618 +120112 +58140 +103514 +90538 +55552 +142739 +61770 +147374 +80038 +128830 +93328 +52369 +71801 +144536 +147140 +118213 +128056 +92155 +114384 +89234 +124451 +94214 +79174 +108427 +111041 +96715 +128414 +62521 +93897 +107428 +90637 +126176 +78676 +69504 +93663 +80869 +124230 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 @@ +import sys + + +def recfuel(mass): + """Calculate the fuel required for the module + use the new weight of the fuel recursively + r(M) = r(M / 3 - 2) + """ + M = mass // 3 - 2 + + if M < 0: + return 0 + else: + return M + recfuel(M) + + +total = 0 + +for mass in sys.stdin: + ret = recfuel(int(mass)) + total += ret + +print(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 @@ +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]; +} 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 @@ +R998,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 +L999,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 @@ +R75,D30,R83,U83,L12,D49,R71,U7,L72 +U62,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 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use List::Util 'min'; +# use Smart::Comments; +use Data::Dumper; + +sub manhattan_distance { + my $ref = shift; + my ($x, $y) = @{$ref}; + + return abs(0 - $x) + abs(0 - $y); +} + +sub pedometer { + my $ref = shift; + my ($x, $y, $step_1, $step_2) = @{$ref}; + return $step_1 + $step_2; +} + +my %directions = ( + 'U' => [0, 1], + 'D' => [0,-1], + 'R' => [1, 0], + 'L' => [-1,0] +); + +my $turn = 0; +my @intersections = (); +my %map; + +while(<>) { + chomp; + my @path = split /,/; + my @coord = (0,0); # Starting point + my $num_steps = 0; + + if (!$turn){ + foreach my $way (@path) { + my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!"; + for (1..$speed) { + $coord[0] += @{$directions{$dir}}[0]; + $coord[1] += @{$directions{$dir}}[1]; + $num_steps++; + $map{"$coord[0],$coord[1]"} = $num_steps; + } + } + $turn = !$turn; + } else { + foreach my $way (@path) { + my ($dir, $speed) = ($1, $2) if ($way =~ /(\w)(\d+)/) or die "$!"; + for (1..$speed) { + $coord[0] += @{$directions{$dir}}[0]; + $coord[1] += @{$directions{$dir}}[1]; + $num_steps++; + if (exists $map{"$coord[0],$coord[1]"}) { + push @intersections, [@coord,$num_steps,$map{"$coord[0],$coord[1]"}]; + } + } + } + } +} + +# print Dumper \@intersections; +print(min(map {pedometer($_);} @intersections)); + + 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 @@ +%353096-843212 +digit(X) :- X = 0..9. +place(N) :- N = 1..6. + +1 {pass(Q,X) : digit(X)} 1 :- place(Q). +value(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). + +:- value(N), N < 353096. +:- value(N), N > 843212. + +:- pass(Q,X), pass(Q', Y), Q' = Q + 1, X > Y. +%:- 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 + +%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,_). + +#show pass/2. +#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 @@ +use strict; +use warnings; + +use Data::Dumper; + +my $low_bound = 353096; +my $upper_bound = 843212; + +my $count = 0; + +for (my $number = $low_bound; $number < $upper_bound; $number++) { + my @nums = split //, $number; + my @cmp = sort {$a <=> $b} @nums; + if (@nums == @cmp and join ("\0", @nums) eq join ("\0", @cmp)) { + my %digits = (); + foreach (@nums) { + $digits{$_}++; + } + foreach my $reps (values %digits) { + if ($reps == 2) { + print "$number\n"; + $count++; + last; + } + } + } +} + +print ">$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 @@ +use strict; +use warnings; +use Data::Dumper; +# use Smart::Comments; +use v5.10; + +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 $inputline = ; +chomp $inputline; +my @tape = split /,/, $inputline; + +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 { return 5; }, # 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/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 @@ +use strict; +use warnings; + +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 %orbit; + +while (<$fh>) { + chomp; + my ($from, $to) = split /\)/; + push @{ $orbit{$from} }, $to; +} + +close $fh; + +my @to_value = ('---', 'COM'); +my $dist = 0; +my $total = 0; + +while ( 1 ) { + + my $cur = pop @to_value; + + print $total and exit unless @to_value; + + if ($cur eq '---') { + unshift @to_value, '---'; + $dist++; + } else { + $total += $dist; + unshift @to_value, @{ $orbit{$cur} } if exists $orbit{$cur}; + } +} 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 @@ +# this solves part 2 +import sys + +import networkx as nx + + +def main(argv): + + G = nx.DiGraph() + with open(argv[0]) as fp: + for line in fp: + line = line.rstrip("\n") + start, end = line.split(")") + G.add_node(start, label=start) + G.add_node(end, label=end) + G.add_edge(start, end) + + jump_to = nx.lowest_common_ancestor(G, "SAN", "YOU") # SAN and YOU are given + jumps = ( + len(nx.shortest_path(G, jump_to, "SAN")) + + len(nx.shortest_path(G, jump_to, "YOU")) + - 4 + ) # 4 because len includes start and end, so -2 times 2 + print(jumps) + + +if __name__ == "__main__": + 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 @@ +WGB)S14 +WN4)27C +18L)M18 +1HY)6ZP +TQ9)KQ6 +HQ3)HH1 +FLC)F1Z +D6R)ZPC +2VD)GK3 +YY3)3TP +PBL)3CK +5K4)CB5 +V5M)CNN +L4T)RHS +HHH)66F +Q3Y)DTL +DGN)YY3 +CCT)L3B +Z6X)FM2 +2QQ)VK9 +MX3)C9J +4JK)BPX +8BP)N13 +PBW)6Z6 +2LT)DT9 +JHX)GXM +5LW)BHQ +DNK)ZBT +29Z)T9D +WNP)TDC +S38)GL6 +DW9)V2F +4MG)3FW +Z9Z)CPK +FKL)QNH +55D)HT2 +D1D)N4Q +Y7W)1Y8 +SFQ)79W +JSR)62W +4WN)J18 +VK9)J2H +LS5)DCX +6LR)P4X +HDV)DGQ +1K9)KD1 +2PX)17C +KSB)GL8 +B4S)VTV +ZW1)KNR +BVH)43P +VKP)6L3 +P5K)MHR +XHR)STT +WBG)5X5 +HZF)8JQ +B47)NW4 +J5V)3ZW +KGP)VVR +24K)PK8 +31V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X5 +98S)DR1 +3XY)TJK +JNX)P9Z +F4P)6YY +VRT)VKV +TVM)92N +WLR)S62 +D5X)ZVB +152)9P6 +F2D)PN3 +2R2)D6K +ZRV)2NX +67X)TS3 +HWP)YBD +5FC)SRV +X92)CQR +8N9)DFC +Q5J)H8N +GGG)8LK +PPQ)841 +6RT)WJ8 +KQR)788 +92N)Q62 +W7S)98S +S19)NFK +VRM)ZVY +GL8)DCH +4YM)17N +F13)D3J +QVC)VGG +31S)J81 +934)93X +Q21)R4C +TH2)HQ4 +1C9)114 +83N)X5B +S3L)T4L +SD2)ZWX +SC1)6K1 +TXD)ZPF +3GY)NQ1 +Z9M)9NS +D6R)7V5 +WG4)C2N +SVH)JBT +TR8)K4V +MPD)7SR +Y36)DP3 +LM2)K21 +KD1)2S1 +FC8)J3M +JFD)BWF +6Y3)88F +STT)GXN +KKC)DW9 +52L)B3G +5BK)XWX +H6X)5BQ +YH6)FGK +VM3)6RN +BWH)1PZ +JK2)BCD +9QT)LTX +6WK)F5B +HYC)C97 +GSP)3MR +6M3)B9P +7LD)9NZ +N31)7KS +GGG)9PB +6RM)SC1 +83C)TGT +BWT)1TJ +7YH)CN7 +JLD)PFT +W95)JNX +ZBT)Y2F +TPD)RSK +3BS)BR6 +KG9)X9M +6HL)5D3 +WMN)P9S +V32)V6H +RYJ)3HM +SF9)SBK +HQ4)19C +BWF)2W7 +2LC)91R +VCD)3G2 +VKJ)9BJ +NL2)VKJ +NLF)HY7 +BR6)L6V +TQ4)8N9 +QH7)W84 +D6K)GGG +W1J)SLM +DB2)RQS +M18)RRD +DXS)32S +KL8)YRT +Q5W)VD4 +SCC)WQX +GQW)VRT +52N)J47 +Z9M)6FX +FKX)GSK +TKX)WRS +7YH)NFG +9Y6)WZV +7BS)48W +NT5)KSX +1Y8)RYV +814)J2F +GSK)KRQ +FBC)W1J +C9G)VRY +V2F)8TN +5M4)Y9S +X9Q)T71 +Q7G)9HB +YLR)4YM +18L)29D +M1M)1BT +82F)MX3 +9BJ)BBL +R55)HRQ +9RH)R5S +414)MHV +ML2)Y8T +179)7YH +GK3)5Q9 +7CZ)VRM +QP3)LDF +YRM)D92 +KXC)B8Y +2ZB)6X1 +XWX)JCF +B3G)CG7 +LHT)4NT +NNN)5LW +48W)R9S +91R)6J4 +97T)BBF +1BT)KWW +B53)QL9 +HBC)DB2 +2D1)89S +BBL)WBG +7WK)LJP +TV1)SNJ +K5Q)L9Q +HNN)YKD +BXP)W81 +64N)GLG +L9Q)M4Y +PTB)8JP +RHP)3FJ +657)PWZ +HZ7)T8S +YKD)W25 +PYL)SMP +YSQ)HSJ +W84)9F8 +HQS)NL2 +3MC)6Y3 +24K)MDD +G3D)B17 +2QF)PXH +38D)FNB +614)SVH +34H)BWT +6L3)GZD +J1Y)QJ9 +3XF)GG9 +MHQ)25V +3PQ)L4T +57T)3W4 +8KQ)614 +PC9)P5K +C2Q)F4P +J37)FW2 +TDC)M1F +YD4)LZV +4H5)HYC +VRY)5XG +3W4)YLR +JZ1)Q21 +SBK)1XQ +RVK)6XT +9NS)LVG +JBT)612 +J4R)QWT +YDT)31V +123)C78 +PN3)PTM +6QM)5GN +CG7)PF8 +HX4)T4H +6CK)HXR +PFT)H53 +62W)FC8 +KQ6)4X5 +3CK)8Z5 +RHS)Q7G +N13)2LT +D3J)NYJ +PFY)N44 +N3F)4FF +P89)29Z +MHN)J49 +S78)LCK +8DD)DR5 +51H)VM3 +1NF)MLL +S99)RKK +9PD)JLD +7Q5)7WK +7WK)MND +CQR)KG9 +X5B)VTN +SCW)RPP +BF4)3XF +QL9)R8G +LDB)4X4 +VDN)73Z +9KY)BXN +VTN)WJ1 +8F4)PR2 +F78)92J +7SX)GQW +GR7)22P +56B)7SX +876)HPY +HNP)V7Y +SNJ)HHH +KNR)W7S +HT2)T9W +PC2)B9Y +6VJ)HNP +4TS)32D +7FB)TZ5 +5BQ)PRT +PRT)97T +G44)9DX +THR)81Z +25P)MCD +Z9L)R92 +V1P)L1D +DSZ)TVC +VKV)TQ4 +4LM)YM4 +T8S)6N6 +HPP)WV4 +99X)8YW +B9P)HDV +T2Q)TNM +JVG)7XG +QGH)KTD +DBX)VNZ +DFM)RSW +29D)GYC +LYS)38D +MKK)WXZ +5B5)MQ5 +BPX)2NW +5LR)PYY +VXL)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 @@ +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