From 7bc0f0ba5aeeecd90f6b7ca546d1b330f56f2996 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 11 Dec 2018 18:14:14 +0000 Subject: [PATCH] Day 10 --- data/advent10-small.txt | 31 +++ data/advent10.txt | 394 +++++++++++++++++++++++++++ problems/day10.html | 253 +++++++++++++++++ src/advent10/advent10-iterate-zip.hs | 117 ++++++++ src/advent10/advent10.hs | 118 ++++++++ 5 files changed, 913 insertions(+) create mode 100644 data/advent10-small.txt create mode 100644 data/advent10.txt create mode 100644 problems/day10.html create mode 100644 src/advent10/advent10-iterate-zip.hs create mode 100644 src/advent10/advent10.hs diff --git a/data/advent10-small.txt b/data/advent10-small.txt new file mode 100644 index 0000000..e499c03 --- /dev/null +++ b/data/advent10-small.txt @@ -0,0 +1,31 @@ +position=< 9, 1> velocity=< 0, 2> +position=< 7, 0> velocity=<-1, 0> +position=< 3, -2> velocity=<-1, 1> +position=< 6, 10> velocity=<-2, -1> +position=< 2, -4> velocity=< 2, 2> +position=<-6, 10> velocity=< 2, -2> +position=< 1, 8> velocity=< 1, -1> +position=< 1, 7> velocity=< 1, 0> +position=<-3, 11> velocity=< 1, -2> +position=< 7, 6> velocity=<-1, -1> +position=<-2, 3> velocity=< 1, 0> +position=<-4, 3> velocity=< 2, 0> +position=<10, -3> velocity=<-1, 1> +position=< 5, 11> velocity=< 1, -2> +position=< 4, 7> velocity=< 0, -1> +position=< 8, -2> velocity=< 0, 1> +position=<15, 0> velocity=<-2, 0> +position=< 1, 6> velocity=< 1, 0> +position=< 8, 9> velocity=< 0, -1> +position=< 3, 3> velocity=<-1, 1> +position=< 0, 5> velocity=< 0, -1> +position=<-2, 2> velocity=< 2, 0> +position=< 5, -2> velocity=< 1, 2> +position=< 1, 4> velocity=< 2, 1> +position=<-2, 7> velocity=< 2, -2> +position=< 3, 6> velocity=<-1, -1> +position=< 5, 0> velocity=< 1, 0> +position=<-6, 0> velocity=< 2, 0> +position=< 5, 9> velocity=< 1, -2> +position=<14, 7> velocity=<-2, 0> +position=<-3, 6> velocity=< 2, -1> diff --git a/data/advent10.txt b/data/advent10.txt new file mode 100644 index 0000000..63f5ca5 --- /dev/null +++ b/data/advent10.txt @@ -0,0 +1,394 @@ +position=< 52672, 52690> velocity=<-5, -5> +position=<-20892, 10646> velocity=< 2, -1> +position=<-10378, -52423> velocity=< 1, 5> +position=< 21199, -20892> velocity=<-2, 2> +position=< 21187, 42176> velocity=<-2, -4> +position=<-31392, 10644> velocity=< 3, -1> +position=< 21152, 31665> velocity=<-2, -3> +position=<-20880, -41905> velocity=< 2, 4> +position=< 21200, -10373> velocity=<-2, 1> +position=< 31652, 42178> velocity=<-3, -4> +position=<-20873, 31671> velocity=< 2, -3> +position=< 52717, 42181> velocity=<-5, -4> +position=<-10350, 10650> velocity=< 1, -1> +position=< 31691, -10372> velocity=<-3, 1> +position=<-52390, 21157> velocity=< 5, -2> +position=<-10354, 52690> velocity=< 1, -5> +position=< 52696, -41906> velocity=<-5, 4> +position=<-10354, 42182> velocity=< 1, -4> +position=<-20879, -20883> velocity=< 2, 2> +position=<-52382, 52688> velocity=< 5, -5> +position=<-41906, -20891> velocity=< 4, 2> +position=<-10394, -31401> velocity=< 1, 3> +position=<-20873, 31670> velocity=< 2, -3> +position=< 52700, 31663> velocity=<-5, -3> +position=<-52414, -20888> velocity=< 5, 2> +position=<-52438, 42182> velocity=< 5, -4> +position=< 42205, 10641> velocity=<-4, -1> +position=< 42169, 10646> velocity=<-4, -1> +position=<-52406, 10647> velocity=< 5, -1> +position=< 42178, 52689> velocity=<-4, -5> +position=< 52725, -31399> velocity=<-5, 3> +position=<-31372, -41905> velocity=< 3, 4> +position=< 10657, 31670> velocity=<-1, -3> +position=<-31363, -20890> velocity=< 3, 2> +position=<-10352, 52689> velocity=< 1, -5> +position=<-41884, 42178> velocity=< 4, -4> +position=< 52720, -20883> velocity=<-5, 2> +position=< 21143, -31399> velocity=<-2, 3> +position=<-31381, 52685> velocity=< 3, -5> +position=<-20863, -52425> velocity=< 2, 5> +position=<-20885, 21158> velocity=< 2, -2> +position=<-31376, 52687> velocity=< 3, -5> +position=<-20873, -31399> velocity=< 2, 3> +position=< 21155, 10648> velocity=<-2, -1> +position=< 42214, 52690> velocity=<-4, -5> +position=< 10636, -41906> velocity=<-1, 4> +position=<-10365, -52416> velocity=< 1, 5> +position=< 42161, 21154> velocity=<-4, -2> +position=< 10644, -10380> velocity=<-1, 1> +position=<-41867, 10641> velocity=< 4, -1> +position=<-31390, 10650> velocity=< 3, -1> +position=< 31698, -20884> velocity=<-3, 2> +position=< 52709, -31402> velocity=<-5, 3> +position=< 10630, -52425> velocity=<-1, 5> +position=< 42186, 31663> velocity=<-4, -3> +position=<-41887, -31399> velocity=< 4, 3> +position=< 42217, -52425> velocity=<-4, 5> +position=<-52386, -52421> velocity=< 5, 5> +position=<-20892, 42182> velocity=< 2, -4> +position=< 10657, -10376> velocity=<-1, 1> +position=<-20892, -41908> velocity=< 2, 4> +position=< 21168, 42175> velocity=<-2, -4> +position=<-31403, -20883> velocity=< 3, 2> +position=< 31695, 10649> velocity=<-3, -1> +position=< 31687, 31664> velocity=<-3, -3> +position=<-41914, -10380> velocity=< 4, 1> +position=<-10351, -20883> velocity=< 1, 2> +position=< 10678, 31667> velocity=<-1, -3> +position=< 52701, -31395> velocity=<-5, 3> +position=< 31686, -41914> velocity=<-3, 4> +position=<-52398, -31399> velocity=< 5, 3> +position=<-41887, 10642> velocity=< 4, -1> +position=< 42209, -52419> velocity=<-4, 5> +position=<-52422, 52693> velocity=< 5, -5> +position=<-20881, 52690> velocity=< 2, -5> +position=< 31671, -31400> velocity=<-3, 3> +position=<-41924, -41910> velocity=< 4, 4> +position=<-20852, 52689> velocity=< 2, -5> +position=< 42201, -52422> velocity=<-4, 5> +position=< 31691, 21161> velocity=<-3, -2> +position=<-41870, -31399> velocity=< 4, 3> +position=<-20889, -52423> velocity=< 2, 5> +position=< 42169, 21153> velocity=<-4, -2> +position=< 52672, -52420> velocity=<-5, 5> +position=< 52691, -10381> velocity=<-5, 1> +position=<-41882, 52690> velocity=< 4, -5> +position=<-31403, 10649> velocity=< 3, -1> +position=<-41871, -20885> velocity=< 4, 2> +position=<-52393, 21158> velocity=< 5, -2> +position=<-41867, 31667> velocity=< 4, -3> +position=< 31686, -52421> velocity=<-3, 5> +position=<-52385, -20887> velocity=< 5, 2> +position=<-31412, -52425> velocity=< 3, 5> +position=<-31395, 10643> velocity=< 3, -1> +position=< 52706, 52685> velocity=<-5, -5> +position=< 31670, 10645> velocity=<-3, -1> +position=<-10374, -31397> velocity=< 1, 3> +position=<-41922, -31403> velocity=< 4, 3> +position=<-20862, -41905> velocity=< 2, 4> +position=< 52672, 21160> velocity=<-5, -2> +position=< 31693, -20883> velocity=<-3, 2> +position=<-20889, -10375> velocity=< 2, 1> +position=<-31360, 10642> velocity=< 3, -1> +position=< 10662, 21156> velocity=<-1, -2> +position=< 21158, -10377> velocity=<-2, 1> +position=<-31384, 52691> velocity=< 3, -5> +position=< 10684, 10642> velocity=<-1, -1> +position=<-20846, -31399> velocity=< 2, 3> +position=<-52382, -20885> velocity=< 5, 2> +position=< 31710, -41908> velocity=<-3, 4> +position=<-10349, -20890> velocity=< 1, 2> +position=< 52672, 10647> velocity=<-5, -1> +position=< 10644, -10380> velocity=<-1, 1> +position=<-41882, 52686> velocity=< 4, -5> +position=<-31356, 31670> velocity=< 3, -3> +position=<-10346, -20885> velocity=< 1, 2> +position=<-31384, 42183> velocity=< 3, -4> +position=<-41895, -52416> velocity=< 4, 5> +position=< 52683, 10647> velocity=<-5, -1> +position=<-10375, 21157> velocity=< 1, -2> +position=< 42205, 42174> velocity=<-4, -4> +position=<-20889, -41905> velocity=< 2, 4> +position=< 10636, -52418> velocity=<-1, 5> +position=<-20901, -52421> velocity=< 2, 5> +position=< 10681, -10373> velocity=<-1, 1> +position=<-20889, -20888> velocity=< 2, 2> +position=<-31387, -20891> velocity=< 3, 2> +position=<-31371, 10646> velocity=< 3, -1> +position=<-41879, -10372> velocity=< 4, 1> +position=< 31671, 31665> velocity=<-3, -3> +position=< 10689, -20883> velocity=<-1, 2> +position=< 10641, 52691> velocity=<-1, -5> +position=<-41874, 10643> velocity=< 4, -1> +position=<-52405, 10645> velocity=< 5, -1> +position=<-20852, 10641> velocity=< 2, -1> +position=<-41914, -20891> velocity=< 4, 2> +position=< 52699, 42183> velocity=<-5, -4> +position=< 10630, -10381> velocity=<-1, 1> +position=< 21147, -31400> velocity=<-2, 3> +position=< 42222, 52693> velocity=<-4, -5> +position=< 31695, -41913> velocity=<-3, 4> +position=< 31706, 21161> velocity=<-3, -2> +position=<-31363, 31665> velocity=< 3, -3> +position=< 42193, -41914> velocity=<-4, 4> +position=< 52672, 31670> velocity=<-5, -3> +position=<-10384, -20889> velocity=< 1, 2> +position=< 10684, 21156> velocity=<-1, -2> +position=<-10341, -52421> velocity=< 1, 5> +position=<-41876, 21156> velocity=< 4, -2> +position=< 52728, 42182> velocity=<-5, -4> +position=< 31677, -41909> velocity=<-3, 4> +position=<-31395, 52693> velocity=< 3, -5> +position=< 52689, 10641> velocity=<-5, -1> +position=<-31399, 10641> velocity=< 3, -1> +position=< 52717, 21155> velocity=<-5, -2> +position=<-52398, -10375> velocity=< 5, 1> +position=<-10375, 10641> velocity=< 1, -1> +position=< 42161, 21155> velocity=<-4, -2> +position=< 21139, -31402> velocity=<-2, 3> +position=<-52425, -41905> velocity=< 5, 4> +position=<-52406, -41911> velocity=< 5, 4> +position=<-31389, 21157> velocity=< 3, -2> +position=<-31388, 21157> velocity=< 3, -2> +position=<-52425, -20885> velocity=< 5, 2> +position=<-10394, -10372> velocity=< 1, 1> +position=<-31384, -20891> velocity=< 3, 2> +position=< 42206, 31665> velocity=<-4, -3> +position=<-52385, -31398> velocity=< 5, 3> +position=<-10341, -31400> velocity=< 1, 3> +position=<-10386, -52423> velocity=< 1, 5> +position=<-10346, 52690> velocity=< 1, -5> +position=< 21164, -20883> velocity=<-2, 2> +position=< 42177, 10645> velocity=<-4, -1> +position=< 52725, 10644> velocity=<-5, -1> +position=< 42217, 52685> velocity=<-4, -5> +position=< 31650, -41911> velocity=<-3, 4> +position=<-31379, -41912> velocity=< 3, 4> +position=<-41887, 42181> velocity=< 4, -4> +position=<-52422, -31396> velocity=< 5, 3> +position=<-31376, -20889> velocity=< 3, 2> +position=<-41878, 21156> velocity=< 4, -2> +position=< 10684, 31669> velocity=<-1, -3> +position=< 31677, -41905> velocity=<-3, 4> +position=<-31376, -41905> velocity=< 3, 4> +position=<-52427, -10376> velocity=< 5, 1> +position=< 21142, 42174> velocity=<-2, -4> +position=<-20863, -52421> velocity=< 2, 5> +position=<-52409, -41908> velocity=< 5, 4> +position=< 21192, 21158> velocity=<-2, -2> +position=<-41895, -31400> velocity=< 4, 3> +position=< 52731, 52685> velocity=<-5, -5> +position=<-52398, 10646> velocity=< 5, -1> +position=<-31408, -20883> velocity=< 3, 2> +position=< 21197, 31667> velocity=<-2, -3> +position=<-31389, 52685> velocity=< 3, -5> +position=<-41874, 42177> velocity=< 4, -4> +position=<-52382, 31664> velocity=< 5, -3> +position=<-52385, -31396> velocity=< 5, 3> +position=< 42181, -31399> velocity=<-4, 3> +position=<-20904, 10645> velocity=< 2, -1> +position=<-10370, 42176> velocity=< 1, -4> +position=<-52437, -31403> velocity=< 5, 3> +position=< 31659, -31402> velocity=<-3, 3> +position=< 10656, -41906> velocity=<-1, 4> +position=<-41914, 42179> velocity=< 4, -4> +position=<-20865, 21155> velocity=< 2, -2> +position=<-10374, -31396> velocity=< 1, 3> +position=<-20870, 42178> velocity=< 2, -4> +position=< 21195, 31670> velocity=<-2, -3> +position=< 21139, -31401> velocity=<-2, 3> +position=<-31375, -31399> velocity=< 3, 3> +position=< 42174, -41905> velocity=<-4, 4> +position=< 31687, 31666> velocity=<-3, -3> +position=<-31375, -10372> velocity=< 3, 1> +position=<-10333, -52416> velocity=< 1, 5> +position=< 21167, 52693> velocity=<-2, -5> +position=< 42185, 52686> velocity=<-4, -5> +position=<-10366, 52685> velocity=< 1, -5> +position=< 42185, -10373> velocity=<-4, 1> +position=< 52691, -52425> velocity=<-5, 5> +position=<-41887, 52692> velocity=< 4, -5> +position=< 31663, 31666> velocity=<-3, -3> +position=< 42201, 31665> velocity=<-4, -3> +position=<-41900, 42174> velocity=< 4, -4> +position=< 31710, 42181> velocity=<-3, -4> +position=<-52382, -20890> velocity=< 5, 2> +position=< 42214, -41914> velocity=<-4, 4> +position=< 10647, -10377> velocity=<-1, 1> +position=< 31703, -20883> velocity=<-3, 2> +position=< 31690, -20883> velocity=<-3, 2> +position=< 10644, -20891> velocity=<-1, 2> +position=< 21152, 52691> velocity=<-2, -5> +position=<-20873, 10648> velocity=< 2, -1> +position=<-31408, -52423> velocity=< 3, 5> +position=< 31658, -41910> velocity=<-3, 4> +position=<-10337, -41910> velocity=< 1, 4> +position=< 21196, -10381> velocity=<-2, 1> +position=< 42205, -10381> velocity=<-4, 1> +position=<-10383, 52691> velocity=< 1, -5> +position=<-10362, 52688> velocity=< 1, -5> +position=<-52380, -41914> velocity=< 5, 4> +position=< 52704, 31665> velocity=<-5, -3> +position=< 52700, -31398> velocity=<-5, 3> +position=< 52714, 10650> velocity=<-5, -1> +position=< 42201, -52417> velocity=<-4, 5> +position=<-20849, 21161> velocity=< 2, -2> +position=<-20902, 42174> velocity=< 2, -4> +position=< 31692, -41905> velocity=<-3, 4> +position=< 10631, -20888> velocity=<-1, 2> +position=< 42197, 10641> velocity=<-4, -1> +position=<-10351, 42174> velocity=< 1, -4> +position=< 10677, -20888> velocity=<-1, 2> +position=< 31701, 10645> velocity=<-3, -1> +position=<-31400, 31670> velocity=< 3, -3> +position=< 21181, 21152> velocity=<-2, -2> +position=< 10673, 21154> velocity=<-1, -2> +position=< 52733, -10372> velocity=<-5, 1> +position=< 10644, -41908> velocity=<-1, 4> +position=< 42190, 10650> velocity=<-4, -1> +position=<-20905, 52688> velocity=< 2, -5> +position=<-10362, -10376> velocity=< 1, 1> +position=< 31711, 21155> velocity=<-3, -2> +position=< 10652, 52692> velocity=<-1, -5> +position=<-41891, -31399> velocity=< 4, 3> +position=< 21171, -41908> velocity=<-2, 4> +position=< 31682, -10377> velocity=<-3, 1> +position=< 52705, 42174> velocity=<-5, -4> +position=< 52681, -41913> velocity=<-5, 4> +position=< 42164, -41914> velocity=<-4, 4> +position=<-10346, -31397> velocity=< 1, 3> +position=<-31363, 42175> velocity=< 3, -4> +position=< 42189, 10641> velocity=<-4, -1> +position=<-20892, 42181> velocity=< 2, -4> +position=< 21155, -10381> velocity=<-2, 1> +position=< 10636, -52420> velocity=<-1, 5> +position=<-41871, 52693> velocity=< 4, -5> +position=< 42212, 42178> velocity=<-4, -4> +position=< 31707, 42174> velocity=<-3, -4> +position=< 52728, 31668> velocity=<-5, -3> +position=< 31703, -20886> velocity=<-3, 2> +position=<-52438, -10375> velocity=< 5, 1> +position=<-20892, 21152> velocity=< 2, -2> +position=<-31384, -41913> velocity=< 3, 4> +position=< 52677, -52425> velocity=<-5, 5> +position=<-52438, 31668> velocity=< 5, -3> +position=<-20885, 52685> velocity=< 2, -5> +position=<-31384, -41914> velocity=< 3, 4> +position=< 31698, -10381> velocity=<-3, 1> +position=<-52398, -10373> velocity=< 5, 1> +position=< 31709, -10381> velocity=<-3, 1> +position=< 10685, -31399> velocity=<-1, 3> +position=<-41868, -10381> velocity=< 4, 1> +position=< 52688, -52417> velocity=<-5, 5> +position=< 10669, -31399> velocity=<-1, 3> +position=< 10628, -52424> velocity=<-1, 5> +position=< 31698, -31397> velocity=<-3, 3> +position=<-52379, -10376> velocity=< 5, 1> +position=< 21187, 52687> velocity=<-2, -5> +position=<-41914, 10649> velocity=< 4, -1> +position=< 42201, 10648> velocity=<-4, -1> +position=<-41866, -31402> velocity=< 4, 3> +position=< 31650, 31663> velocity=<-3, -3> +position=<-20901, -52421> velocity=< 2, 5> +position=< 42209, 21160> velocity=<-4, -2> +position=< 21150, -31398> velocity=<-2, 3> +position=< 31671, -20891> velocity=<-3, 2> +position=<-20871, -52421> velocity=< 2, 5> +position=<-10378, 52691> velocity=< 1, -5> +position=<-10338, -20886> velocity=< 1, 2> +position=<-52420, -20888> velocity=< 5, 2> +position=< 10676, 52690> velocity=<-1, -5> +position=<-10394, -41914> velocity=< 1, 4> +position=<-41869, -41910> velocity=< 4, 4> +position=< 52693, 52688> velocity=<-5, -5> +position=< 10629, 10641> velocity=<-1, -1> +position=<-20848, -10381> velocity=< 2, 1> +position=<-31400, 31668> velocity=< 3, -3> +position=<-31375, 42174> velocity=< 3, -4> +position=<-10378, -41909> velocity=< 1, 4> +position=< 21181, 31672> velocity=<-2, -3> +position=<-41882, 21160> velocity=< 4, -2> +position=< 52688, 42179> velocity=<-5, -4> +position=<-41879, -31399> velocity=< 4, 3> +position=<-41914, -41907> velocity=< 4, 4> +position=<-10358, 21156> velocity=< 1, -2> +position=< 21164, -20892> velocity=<-2, 2> +position=<-20849, -31394> velocity=< 2, 3> +position=< 21179, -10381> velocity=<-2, 1> +position=<-20897, -41910> velocity=< 2, 4> +position=< 31711, -31401> velocity=<-3, 3> +position=< 42177, -41911> velocity=<-4, 4> +position=< 42169, -31401> velocity=<-4, 3> +position=<-31368, 31666> velocity=< 3, -3> +position=<-10338, 42178> velocity=< 1, -4> +position=< 10681, -31394> velocity=<-1, 3> +position=<-52401, -10378> velocity=< 5, 1> +position=< 52672, 31667> velocity=<-5, -3> +position=< 10647, -31399> velocity=<-1, 3> +position=< 10672, 42183> velocity=<-1, -4> +position=<-52398, -10375> velocity=< 5, 1> +position=<-31398, -31399> velocity=< 3, 3> +position=< 31675, -52416> velocity=<-3, 5> +position=<-31389, -10381> velocity=< 3, 1> +position=< 42170, 10643> velocity=<-4, -1> +position=<-52389, -10377> velocity=< 5, 1> +position=<-31371, -41911> velocity=< 3, 4> +position=< 52672, 52692> velocity=<-5, -5> +position=< 31677, -41905> velocity=<-3, 4> +position=< 21155, -31395> velocity=<-2, 3> +position=< 10686, 52689> velocity=<-1, -5> +position=< 21165, 31663> velocity=<-2, -3> +position=<-10373, 42176> velocity=< 1, -4> +position=< 31662, -10374> velocity=<-3, 1> +position=< 31671, -31395> velocity=<-3, 3> +position=<-10386, -52418> velocity=< 1, 5> +position=<-41898, 10649> velocity=< 4, -1> +position=<-31357, -52421> velocity=< 3, 5> +position=<-20849, 52688> velocity=< 2, -5> +position=<-52428, 10645> velocity=< 5, -1> +position=<-52396, -41914> velocity=< 5, 4> +position=< 21187, 52686> velocity=<-2, -5> +position=< 10689, 42175> velocity=<-1, -4> +position=< 21147, 21152> velocity=<-2, -2> +position=< 52713, -52421> velocity=<-5, 5> +position=<-41906, -52416> velocity=< 4, 5> +position=< 42188, -31398> velocity=<-4, 3> +position=<-52394, -20888> velocity=< 5, 2> +position=<-41924, -52421> velocity=< 4, 5> +position=< 31703, -31395> velocity=<-3, 3> +position=<-10378, -52421> velocity=< 1, 5> +position=<-20881, 10642> velocity=< 2, -1> +position=< 31682, 21154> velocity=<-3, -2> +position=< 31710, 21158> velocity=<-3, -2> +position=<-41911, -20883> velocity=< 4, 2> +position=< 10652, 31666> velocity=<-1, -3> +position=< 42173, -41906> velocity=<-4, 4> +position=<-10382, -41907> velocity=< 1, 4> +position=<-20888, -52421> velocity=< 2, 5> +position=<-10333, -31400> velocity=< 1, 3> +position=< 21148, -52424> velocity=<-2, 5> +position=<-31398, -10381> velocity=< 3, 1> +position=< 42185, 42180> velocity=<-4, -4> +position=< 21187, -52424> velocity=<-2, 5> +position=<-20864, 10641> velocity=< 2, -1> +position=< 31684, -31403> velocity=<-3, 3> +position=< 21152, -41910> velocity=<-2, 4> +position=< 42174, -20888> velocity=<-4, 2> +position=< 42169, 52691> velocity=<-4, -5> +position=<-10394, -20884> velocity=< 1, 2> +position=<-41919, 31671> velocity=< 4, -3> +position=<-52388, 10645> velocity=< 5, -1> +position=< 21165, -31394> velocity=<-2, 3> +position=<-41898, 10646> velocity=< 4, -1> +position=<-52385, 10647> velocity=< 5, -1> diff --git a/problems/day10.html b/problems/day10.html new file mode 100644 index 0000000..c02bb3f --- /dev/null +++ b/problems/day10.html @@ -0,0 +1,253 @@ + + + + +Day 10 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 20*

        //2018

+ + + +
+

--- Day 10: The Stars Align ---

It's no use; your navigation system simply isn't capable of providing walking directions in the arctic circle, and certainly not in 1018.

+

The Elves suggest an alternative. In times like these, North Pole rescue operations will arrange points of light in the sky to guide missing Elves back to base. Unfortunately, the message is easy to miss: the points move slowly enough that it takes hours to align them, but have so much momentum that they only stay aligned for a second. If you blink at the wrong time, it might be hours before another message appears.

+

You can see these points of light floating in the distance, and record their position in the sky and their velocity, the relative change in position per second (your puzzle input). The coordinates are all given from your perspective; given enough time, those positions and velocities will move the points into a cohesive message!

+

Rather than wait, you decide to fast-forward the process and calculate what the points will eventually spell.

+

For example, suppose you note the following points:

+
position=< 9,  1> velocity=< 0,  2>
+position=< 7,  0> velocity=<-1,  0>
+position=< 3, -2> velocity=<-1,  1>
+position=< 6, 10> velocity=<-2, -1>
+position=< 2, -4> velocity=< 2,  2>
+position=<-6, 10> velocity=< 2, -2>
+position=< 1,  8> velocity=< 1, -1>
+position=< 1,  7> velocity=< 1,  0>
+position=<-3, 11> velocity=< 1, -2>
+position=< 7,  6> velocity=<-1, -1>
+position=<-2,  3> velocity=< 1,  0>
+position=<-4,  3> velocity=< 2,  0>
+position=<10, -3> velocity=<-1,  1>
+position=< 5, 11> velocity=< 1, -2>
+position=< 4,  7> velocity=< 0, -1>
+position=< 8, -2> velocity=< 0,  1>
+position=<15,  0> velocity=<-2,  0>
+position=< 1,  6> velocity=< 1,  0>
+position=< 8,  9> velocity=< 0, -1>
+position=< 3,  3> velocity=<-1,  1>
+position=< 0,  5> velocity=< 0, -1>
+position=<-2,  2> velocity=< 2,  0>
+position=< 5, -2> velocity=< 1,  2>
+position=< 1,  4> velocity=< 2,  1>
+position=<-2,  7> velocity=< 2, -2>
+position=< 3,  6> velocity=<-1, -1>
+position=< 5,  0> velocity=< 1,  0>
+position=<-6,  0> velocity=< 2,  0>
+position=< 5,  9> velocity=< 1, -2>
+position=<14,  7> velocity=<-2,  0>
+position=<-3,  6> velocity=< 2, -1>
+
+

Each line represents one point. Positions are given as <X, Y> pairs: X represents how far left (negative) or right (positive) the point appears, while Y represents how far up (negative) or down (positive) the point appears.

+

At 0 seconds, each point has the position given. Each second, each point's velocity is added to its position. So, a point with velocity <1, -2> is moving to the right, but is moving upward twice as quickly. If this point's initial position were <3, 9>, after 3 seconds, its position would become <6, 3>.

+

Over time, the points listed above would move like this:

+
Initially:
+........#.............
+................#.....
+.........#.#..#.......
+......................
+#..........#.#.......#
+...............#......
+....#.................
+..#.#....#............
+.......#..............
+......#...............
+...#...#.#...#........
+....#..#..#.........#.
+.......#..............
+...........#..#.......
+#...........#.........
+...#.......#..........
+
+After 1 second:
+......................
+......................
+..........#....#......
+........#.....#.......
+..#.........#......#..
+......................
+......#...............
+....##.........#......
+......#.#.............
+.....##.##..#.........
+........#.#...........
+........#...#.....#...
+..#...........#.......
+....#.....#.#.........
+......................
+......................
+
+After 2 seconds:
+......................
+......................
+......................
+..............#.......
+....#..#...####..#....
+......................
+........#....#........
+......#.#.............
+.......#...#..........
+.......#..#..#.#......
+....#....#.#..........
+.....#...#...##.#.....
+........#.............
+......................
+......................
+......................
+
+After 3 seconds:
+......................
+......................
+......................
+......................
+......#...#..###......
+......#...#...#.......
+......#...#...#.......
+......#####...#.......
+......#...#...#.......
+......#...#...#.......
+......#...#...#.......
+......#...#..###......
+......................
+......................
+......................
+......................
+
+After 4 seconds:
+......................
+......................
+......................
+............#.........
+........##...#.#......
+......#.....#..#......
+.....#..##.##.#.......
+.......##.#....#......
+...........#....#.....
+..............#.......
+....#......#...#......
+.....#.....##.........
+...............#......
+...............#......
+......................
+......................
+
+

After 3 seconds, the message appeared briefly: HI. Of course, your message will be much longer and will take many more seconds to appear.

+

What message will eventually appear in the sky?

+
+

Your puzzle answer was FNRGPBHR.

--- Part Two ---

Good thing you didn't have to wait, because that would have taken a long time - much longer than the 3 seconds in the example above.

+

Impressed by your sub-hour communication capabilities, the Elves are curious: exactly how many seconds would they have needed to wait for that message to appear?

+
+

Your puzzle answer was 10511.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/src/advent10/advent10-iterate-zip.hs b/src/advent10/advent10-iterate-zip.hs new file mode 100644 index 0000000..ea26113 --- /dev/null +++ b/src/advent10/advent10-iterate-zip.hs @@ -0,0 +1,117 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Data.List + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import qualified Data.Set as S + +type Coord = (Integer, Integer) -- x, y +type Bounds = (Integer, Integer, Integer, Integer) -- minX, maxX, minY, maxY +data Particle = Particle {_position :: Coord, _velocity :: Coord} deriving (Eq, Show) +type Swarm = [Particle] +type Grid = S.Set Coord + +main :: IO () +main = do + text <- TIO.readFile "data/advent10.txt" + let particles = successfulParse text + let (final, time) = part0 particles + putStrLn $ showParticles final + print time + +part0 :: Swarm -> (Swarm, Int) +part0 particles = (snd $ last $ gridPairs, length gridPairs) + where gridPairs = findEnd particles + +runParticles :: Swarm -> [Swarm] +runParticles = iterate updateAll + +findEnd :: Swarm -> [(Swarm, Swarm)] +findEnd particles = takeWhile firstLarger gridPairs + where grids = runParticles particles + gridPairs = zip grids (drop 1 grids) + firstLarger (g1, g2) = (boundsArea g1) > (boundsArea g2) + + + +boundsArea :: Swarm -> Integer +boundsArea particles = (maxX - minX) * (maxY - minY) + where (minX, maxX, minY, maxY) = findBounds particles + +findBounds :: Swarm -> Bounds +findBounds particles = + ( minX -- small x edge + , maxX -- large x edge + , minY -- small x edge + , maxY -- large y edge + ) + where maxX = maximum $ map (fst . _position) particles + minX = minimum $ map (fst . _position) particles + maxY = maximum $ map (snd . _position) particles + minY = minimum $ map (snd . _position) particles + + +update :: Particle -> Particle +update particle = particle {_position = (x + vx, y + vy)} + where (x, y) = _position particle + (vx, vy) = _velocity particle + + +updateAll :: Swarm -> Swarm +updateAll = map update + +showParticles :: Swarm -> String +showParticles particles = intercalate "\n" rows + where (minX, maxX, minY, maxY) = findBounds particles + grid = S.fromList $ map _position particles + rows = [showRow y minX maxX grid | y <- [minY..maxY] ] + +showCell :: Integer -> Integer -> Grid -> Char +showCell x y grid + | (x, y) `S.member` grid = '*' + | otherwise = ' ' + +showRow :: Integer -> Integer -> Integer -> Grid -> String +showRow y minX maxX grid = [showCell x y grid | x <- [minX..maxX] ] + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +lexeme = L.lexeme sc +integer = lexeme L.decimal +symb = L.symbol sc +signedInteger = L.signed sc integer + +posPrefix = symb "position=<" +velPrefix = symb "velocity=<" +suffix = symb ">" +commaP = symb "," + +particleFileP = many particleP + +particleP = particlify <$> positionP <*> velocityP + where particlify x v = Particle x v + +positionP = posPrefix *> pairP <* suffix +velocityP = velPrefix *> pairP <* suffix + +pairP = (,) <$> signedInteger <* commaP <*> signedInteger + +successfulParse :: Text -> Swarm +successfulParse input = + case parse particleFileP "input" input of + Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right particles -> particles \ No newline at end of file diff --git a/src/advent10/advent10.hs b/src/advent10/advent10.hs new file mode 100644 index 0000000..59d7c47 --- /dev/null +++ b/src/advent10/advent10.hs @@ -0,0 +1,118 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Data.List + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import qualified Data.Map.Strict as M +import qualified Data.Set as S + +type Coord = (Integer, Integer) -- x, y +type Bounds = (Integer, Integer, Integer, Integer) -- minX, maxX, minY, maxY +data Particle = Particle {_position :: Coord, _velocity :: Coord} deriving (Eq, Show) +type Grid = S.Set Coord + +main :: IO () +main = do + text <- TIO.readFile "data/advent10.txt" + let particles = successfulParse text + -- putStrLn $ part1 particles + let (time, view) = part2 0 particles + putStrLn view + print time + + +part1 particles + | area' > area = showParticles particles + | otherwise = part1 particles' + where particles' = updateAll particles + area = boundsArea particles + area' = boundsArea particles' + +part2 time particles + | area' > area = (time, showParticles particles) + | otherwise = part2 (time+1) particles' + where particles' = updateAll particles + area = boundsArea particles + area' = boundsArea particles' + +boundsArea :: [Particle] -> Integer +boundsArea particles = (maxX - minX) * (maxY - minY) + where (minX, maxX, minY, maxY) = findBounds particles + +findBounds :: [Particle] -> Bounds +findBounds particles = + ( minX -- small x edge + , maxX -- large x edge + , minY -- small x edge + , maxY -- large y edge + ) + where maxX = maximum $ map (fst . _position) particles + minX = minimum $ map (fst . _position) particles + maxY = maximum $ map (snd . _position) particles + minY = minimum $ map (snd . _position) particles + + +update :: Particle -> Particle +update particle = particle {_position = (x + vx, y + vy)} + where (x, y) = _position particle + (vx, vy) = _velocity particle + + +updateAll :: [Particle] -> [Particle] +updateAll = map update + +showParticles :: [Particle] -> String +showParticles particles = intercalate "\n" rows + where (minX, maxX, minY, maxY) = findBounds particles + grid = S.fromList $ map _position particles + rows = [showRow y minX maxX grid | y <- [minY..maxY] ] + +showCell :: Integer -> Integer -> Grid -> Char +showCell x y grid + | (x, y) `S.member` grid = '*' + | otherwise = ' ' + +showRow :: Integer -> Integer -> Integer -> Grid -> String +showRow y minX maxX grid = [showCell x y grid | x <- [minX..maxX] ] + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +lexeme = L.lexeme sc +integer = lexeme L.decimal +symb = L.symbol sc +signedInteger = L.signed sc integer + +posPrefix = symb "position=<" +velPrefix = symb "velocity=<" +suffix = symb ">" +commaP = symb "," + +particleFileP = many particleP + +particleP = particlify <$> positionP <*> velocityP + where particlify x v = Particle x v + +positionP = posPrefix *> pairP <* suffix +velocityP = velPrefix *> pairP <* suffix + +pairP = (,) <$> signedInteger <* commaP <*> signedInteger + +successfulParse :: Text -> [Particle] +successfulParse input = + case parse particleFileP "input" input of + Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right particles -> particles \ No newline at end of file -- 2.34.1