aboutsummaryrefslogtreecommitdiff
path: root/2017
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2020-12-05 10:55:56 -0300
committerMatias Linares <matiaslina@gmail.com>2020-12-05 10:55:56 -0300
commita0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e (patch)
treea663ee4f6a427e98db0878ca539c9288bfbc70d0 /2017
parent5fa17cd54a1170449a91da48a2fe88a99349daa2 (diff)
downloadadvent-of-code-a0fcb600572ea0da9dee1b5b9e7a24ce674ebe7e.tar.gz
Add 2018 and 2017
Diffstat (limited to '2017')
-rwxr-xr-x2017/app.p627
-rw-r--r--2017/inputs/day11
-rw-r--r--2017/inputs/day216
-rw-r--r--2017/lib/AdventOfCode.pm678
-rw-r--r--2017/t/01-day.t17
-rw-r--r--2017/t/02-day.t23
-rw-r--r--2017/t/03-day.t11
7 files changed, 173 insertions, 0 deletions
diff --git a/2017/app.p6 b/2017/app.p6
new file mode 100755
index 0000000..1dc0ac7
--- /dev/null
+++ b/2017/app.p6
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl6
+use v6;
+use lib 'lib';
+use AdventOfCode;
+
+sub MAIN($day) {
+ given $day {
+ when 1 {
+ my $text = 'inputs/day1'.IO.slurp;
+ say "Day01 a: " ~ day01($text);
+ say "Day01 b: " ~ day01b($text);
+ }
+ when 2 {
+ my @spreadsheet;
+ for 'inputs/day2'.IO.lines -> $line {
+ @spreadsheet.push($line.split(/\s+/).map(*.Int));
+ }
+ say "Day02 a: " ~ checksum(@spreadsheet);
+ say "Day02 b: " ~ evenly-divisible-values(@spreadsheet);
+
+ }
+ when 3 {
+ say "Spiral memory for 12: " ~ spiral-memory(12);
+ }
+ default { say "uninplemented"; }
+ }
+}
diff --git a/2017/inputs/day1 b/2017/inputs/day1
new file mode 100644
index 0000000..037cd73
--- /dev/null
+++ b/2017/inputs/day1
@@ -0,0 +1 @@
+8231753674683997878179259195565332579493378483264978184143341284379682788518559178822225126625428318115396632681141871952894291898364781898929292614792884883249356728741993224889167928232261325123447569829932951268292953928766755779761837993812528527484487298117739869189415599461746944992651752768158611996715467871381527675219481185217357632445748912726487669881876129192932995282777848496561259839781188719233951619188388532698519298142112853776942545211859134185231768952888462471642851588368445761489225786919778983848113833773768236969923939838755997989537648222217996381757542964844337285428654375499359997792679256881378967852376848812795761118139288152799921176874256377615952758268844139579622754965461884862647423491918913628848748756595463191585555385849335742224855473769411212376446591654846168189278959857681336724221434846946124915271196433144335482787432683848594487648477532498952572515118864475621828118274911298396748213136426357769991314661642612786847135485969889237193822718111269561741563479116832364485724716242176288642371849569664594194674763319687735723517614962575592111286177553435651952853878775431234327919595595658641534765455489561934548474291254387229751472883423413196845162752716925199866591883313638846474321161569892518574346226751366315311145777448781862222126923449311838564685882695889397531413937666673233451216968414288135984394249684886554812761191289485457945866524228415191549168557957633386991931186773843869999284468773866221976873998168818944399661463963658784821796272987155278195355579386768156718813624559264574836134419725187881514665834441359644955768658663278765363789664721736533517774292478192143934318399418188298753351815388561359528533778996296279366394386455544446922653976725113889842749182361253582433319351193862788433113852782596161148992233558144692913791714859516653421917841295749163469751479835492713392861519993791967927773114713888458982796514977717987598165486967786989991998142488631168697963816156374216224386193941566358543266646516247854435356941566492841213424915682394928959116411457967897614457497279472661229548612777155998358618945222326558176486944695689777438164612198225816646583996426313832539918
diff --git a/2017/inputs/day2 b/2017/inputs/day2
new file mode 100644
index 0000000..545582a
--- /dev/null
+++ b/2017/inputs/day2
@@ -0,0 +1,16 @@
+157 564 120 495 194 520 510 618 244 443 471 473 612 149 506 138
+1469 670 47 604 1500 238 1304 1426 54 749 1218 1409 60 51 1436 598
+578 184 2760 3057 994 167 2149 191 2913 2404 213 1025 1815 588 2421 3138
+935 850 726 155 178 170 275 791 1028 75 781 138 176 621 773 688
+212 977 297 645 229 194 207 640 804 509 833 726 197 825 242 743
+131 43 324 319 64 376 231 146 382 162 464 314 178 353 123 446
+551 121 127 155 1197 288 1412 1285 557 137 145 1651 1549 1217 681 1649
+1723 1789 5525 4890 3368 188 3369 4842 3259 2502 4825 163 146 2941 126 5594
+311 2420 185 211 2659 2568 2461 231 2599 1369 821 506 2227 180 220 1372
+197 4490 141 249 3615 3314 789 4407 169 352 4383 5070 5173 3115 132 3513
+4228 2875 3717 504 114 2679 165 3568 3002 116 756 151 4027 261 4813 2760
+651 3194 2975 2591 1019 835 3007 248 3028 1382 282 3242 296 270 3224 3304
+1858 1650 1720 1848 95 313 500 1776 207 1186 72 259 281 1620 79 77
+3841 3217 440 3481 3643 940 3794 4536 1994 4040 3527 202 193 1961 230 217
+2837 2747 2856 426 72 78 2361 96 2784 2780 98 2041 2444 1267 2167 2480
+411 178 4263 4690 3653 162 3201 4702 3129 2685 3716 147 3790 4888 79 165
diff --git a/2017/lib/AdventOfCode.pm6 b/2017/lib/AdventOfCode.pm6
new file mode 100644
index 0000000..eef09d0
--- /dev/null
+++ b/2017/lib/AdventOfCode.pm6
@@ -0,0 +1,78 @@
+use v6;
+
+sub do-captcha(@input, $cmp-offset) returns Int {
+ my @tmp = @input.rotate($cmp-offset);
+ (@input Z @tmp).map(-> $z {
+ if $z[0] == $z[1] { $z[0] } else { 0 }
+ }).reduce(&[+])
+}
+
+sub day01(Str $input) returns Int is export {
+ do-captcha($input.comb, 1)
+}
+
+sub day01b(Str $input) returns Int is export {
+ do-captcha($input.comb, $input.comb.elems/2)
+}
+
+sub checksum(@input) returns Int is export {
+ [+] @input.map(-> @i {
+ @i.max - @i.min
+ })
+}
+
+sub find-divisible(@row) returns Int {
+ for @row -> $x {
+ for @row -> $y {
+ if $x %% $y and $x != $y {
+ return ($x/$y).Int
+ }
+ }
+ }
+}
+
+sub evenly-divisible-values(@input) returns Int is export {
+ [+] @input.map(-> @i {
+ find-divisible(@i)
+ })
+}
+
+# Day 3
+
+sub position(Int $num) {
+ my Int $num-square = (^Inf).map(*²).grep(* ≥ $num)[0];
+ my Int $square-size = $num-square.sqrt.Int;
+ my $max-step = ($square-size/2).floor;
+ my @retval = $max-step, $max-step;
+ say "$num, $num-square, $square-size, $max-step, {@retval}";
+ if $num-square == $num { return @retval; }
+
+ while $num-square > $num {
+ say "$num-square > ($square-size - 1)";
+ $num-square -= ($square-size -1);
+ }
+
+ $num-square > $num or die(" Error !! $num-square > $num");
+
+ if $num-square == $num { return @retval; }
+
+ for ^($square-size*4 -1) -> $i {
+ $num-square -= 1;
+ say "Step 2-$i: $num-square == $num ? {@retval[0]}";
+ if $num-square != $num {
+ @retval[0] = (@retval[0]-1) % ($max-step + 1);
+ }
+ }
+ say "Returning: {@retval}";
+ return @retval;
+}
+
+sub manhattan-distance(@position) returns Int {
+ say "Sum {@position.perl}";
+ [+] @position
+}
+
+sub spiral-memory(Int $input) returns Int is export {
+ say "position: {position($input).perl}";
+ manhattan-distance(position($input))
+}
diff --git a/2017/t/01-day.t b/2017/t/01-day.t
new file mode 100644
index 0000000..7a3333b
--- /dev/null
+++ b/2017/t/01-day.t
@@ -0,0 +1,17 @@
+use v6;
+use Test;
+use AdventOfCode;
+
+plan 4 + 5;
+
+is day01('1122'), 3;
+is day01('1111'), 4;
+is day01('1234'), 0;
+is day01('91212129'), 9;
+
+
+is day01b('1212'), 6;
+is day01b('1221'), 0;
+is day01b('123425'), 4;
+is day01b('123123'), 12;
+is day01b('12131415'), 4;
diff --git a/2017/t/02-day.t b/2017/t/02-day.t
new file mode 100644
index 0000000..787caff
--- /dev/null
+++ b/2017/t/02-day.t
@@ -0,0 +1,23 @@
+use v6;
+use Test;
+use AdventOfCode;
+
+plan 2;
+
+# First part
+my @mat = (
+ <5 1 9 5>,
+ <7 5 3>,
+ <2 4 6 8>,
+);
+
+is checksum(@mat), 18;
+
+# Second part
+my @spreadsheet2 = (
+ <5 9 2 8>,
+ <9 4 7 3>,
+ <3 8 6 5>,
+);
+
+is evenly-divisible-values(@spreadsheet2), 9;
diff --git a/2017/t/03-day.t b/2017/t/03-day.t
new file mode 100644
index 0000000..7a06465
--- /dev/null
+++ b/2017/t/03-day.t
@@ -0,0 +1,11 @@
+use v6;
+use Test;
+use AdventOfCode;
+
+plan 5;
+
+is spiral-memory(1), 0;
+is spiral-memory(9), 2;
+is spiral-memory(12), 3;
+is spiral-memory(23), 2;
+is spiral-memory(1024), 31;