From f65178f91c9ebe7c8d3e2dfcea9156eaa5bfa455 Mon Sep 17 00:00:00 2001 From: Miguel Flores Ruiz de Eguino <1889916+miguelfrde@users.noreply.github.com> Date: Mon, 2 Dec 2019 23:13:28 -0800 Subject: [PATCH] [2019] Day 3 finished --- 2019/problem03/Makefile | 7 ++++ 2019/problem03/README.md | 73 +++++++++++++++++++++++++++++++++++++ 2019/problem03/example1.txt | 2 + 2019/problem03/input.txt | 2 + 2019/problem03/part1and2.hs | 52 ++++++++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 2019/problem03/Makefile create mode 100644 2019/problem03/README.md create mode 100644 2019/problem03/example1.txt create mode 100644 2019/problem03/input.txt create mode 100644 2019/problem03/part1and2.hs diff --git a/2019/problem03/Makefile b/2019/problem03/Makefile new file mode 100644 index 0000000..13f5a70 --- /dev/null +++ b/2019/problem03/Makefile @@ -0,0 +1,7 @@ +all: part1and2 + +part1and2: part1and2.hs + ghc -o part1and2 part1and2.hs + +clean: + rm -f part1and2 *.o *.hi diff --git a/2019/problem03/README.md b/2019/problem03/README.md new file mode 100644 index 0000000..df576d1 --- /dev/null +++ b/2019/problem03/README.md @@ -0,0 +1,73 @@ +# Day 3: Crossed Wires + +## Part 1 + +The gravity assist was successful, and you're well on your way to the Venus refuelling station. During the rush back on Earth, the fuel management system wasn't completely installed, so that's next on the priority list. + +Opening the front panel reveals a jumble of wires. Specifically, two wires are connected to a central port and extend outward on a grid. You trace the path each wire takes as it leaves the central port, one wire per line of text (your puzzle input). + +The wires twist and turn, but the two wires occasionally cross paths. To fix the circuit, you need to find the intersection point closest to the central port. Because the wires are on a grid, use the Manhattan distance for this measurement. While the wires do technically cross right at the central port where they both start, this point does not count, nor does a wire count as crossing with itself. + +For example, if the first wire's path is R8,U5,L5,D3, then starting from the central port (o), it goes right 8, up 5, left 5, and finally down 3: + +........... +........... +........... +....+----+. +....|....|. +....|....|. +....|....|. +.........|. +.o-------+. +........... +Then, if the second wire's path is U7,R6,D4,L4, it goes up 7, right 6, down 4, and left 4: + +........... +.+-----+... +.|.....|... +.|..+--X-+. +.|..|..|.|. +.|.-X--+.|. +.|..|....|. +.|.......|. +.o-------+. +........... +These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6. + +Here are a few more examples: + +R75,D30,R83,U83,L12,D49,R71,U7,L72 +U62,R66,U55,R34,D71,R55,D58,R83 = distance 159 +R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51 +U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135 +What is the Manhattan distance from the central port to the closest intersection? + +## Part 2 + +It turns out that this circuit is very timing-sensitive; you actually need to minimize the signal delay. + +To do this, calculate the number of steps each wire takes to reach each intersection; choose the intersection where the sum of both wires' steps is lowest. If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection. + +The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered. Again consider the example from above: + +........... +.+-----+... +.|.....|... +.|..+--X-+. +.|..|..|.|. +.|.-X--+.|. +.|..|....|. +.|.......|. +.o-------+. +........... +In the above example, the intersection closest to the central port is reached after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the second wire for a total of 20+20 = 40 steps. + +However, the top-right intersection is better: the first wire takes only 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30 steps. + +Here are the best steps for the extra examples from above: + +R75,D30,R83,U83,L12,D49,R71,U7,L72 +U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps +R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51 +U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps +What is the fewest combined steps the wires must take to reach an intersection? diff --git a/2019/problem03/example1.txt b/2019/problem03/example1.txt new file mode 100644 index 0000000..620a05e --- /dev/null +++ b/2019/problem03/example1.txt @@ -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/problem03/input.txt b/2019/problem03/input.txt new file mode 100644 index 0000000..36d6f5e --- /dev/null +++ b/2019/problem03/input.txt @@ -0,0 +1,2 @@ +R1000,D940,L143,D182,L877,D709,L253,U248,L301,U434,R841,U715,R701,U92,R284,U115,R223,U702,R969,U184,L992,U47,L183,U474,L437,D769,L71,U96,R14,U503,R144,U432,R948,U96,L118,D696,R684,U539,L47,D851,L943,U606,L109,D884,R157,U946,R75,U702,L414,U347,R98,D517,L963,D177,R467,D142,L845,U427,R357,D528,L836,D222,L328,U504,R237,U99,L192,D147,L544,D466,R765,U845,L267,D217,L138,U182,R226,U466,R785,U989,R55,D822,L101,U292,R78,U962,R918,U218,L619,D324,L467,U885,L658,U890,L764,D747,R369,D930,L264,D916,L696,U698,R143,U537,L922,U131,R141,D97,L76,D883,R75,D657,R859,U503,R399,U33,L510,D318,L455,U128,R146,D645,L147,D651,L388,D338,L998,U321,L982,U150,R123,U834,R913,D200,L455,D479,L38,U860,L471,U945,L946,D365,L377,U816,R988,D597,R181,D253,R744,U472,L345,U495,L187,D443,R924,D536,R847,U430,L145,D827,L152,D831,L886,D597,R699,D751,R638,D580,L488,D566,L717,D220,L965,D587,L638,D880,L475,D165,L899,U388,R326,D568,R940,U550,R788,D76,L189,D641,R629,D383,L272,D840,L441,D709,L424,U158,L831,D576,R96,D401,R425,U525,L378,D907,L645,U609,L336,D232,L259,D280,L523,U938,R190,D9,L284,U941,L254,D657,R572,U443,L850,U508,L742,D661,L977,U910,L190,U626,R140,U762,L673,U741,R317,D518,R111,U28,R598,D403,R465,D684,R79,U725,L556,U302,L367,U306,R632,D550,R89,D292,R561,D84,L923,D109,L865,D880,L387,D24,R99,U934,L41,U29,L225,D12,L818,U696,R652,U327,L69,D773,L618,U803,L433,D467,R840,D281,R161,D400,R266,D67,L205,D94,R551,U332,R938,D759,L437,D515,L480,U774,L373,U478,R963,D863,L735,U138,L580,U72,L770,U968,L594 +L990,D248,L833,U137,L556,U943,R599,U481,R963,U812,L825,U421,R998,D847,R377,D19,R588,D657,R197,D354,L548,U849,R30,D209,L745,U594,L168,U5,L357,D135,R94,D686,R965,U838,R192,U428,L861,U354,R653,U543,L633,D508,R655,U575,R709,D53,L801,D709,L92,U289,L466,D875,R75,D448,R576,D972,L77,U4,L267,D727,L3,D687,R743,D830,L803,D537,L180,U644,L204,U407,R866,U886,R560,D848,R507,U470,R38,D652,R806,D283,L836,D629,R347,D679,R609,D224,L131,D616,L687,U181,R539,D829,L598,D55,L806,U208,R886,U794,L268,D365,L145,U690,R50,D698,L140,D512,L551,U845,R351,U724,R405,D245,L324,U181,L824,U351,R223,D360,L687,D640,L653,U158,R786,D962,R931,D151,R939,D34,R610,U684,L694,D283,R402,D253,R388,D195,R732,U809,R246,D571,L820,U742,L507,U967,L886,D693,L273,U558,L914,D122,R146,U788,R83,U149,R241,U616,R326,U40,L192,D845,L577,U803,L668,D443,R705,D793,R443,D883,L715,U757,R767,D360,L289,D756,R696,D236,L525,U872,L332,U203,L152,D234,R559,U191,R340,U926,L746,D128,R867,D562,L100,U445,L489,D814,R921,D286,L378,D956,L36,D998,R158,D611,L493,U542,R932,U957,R55,D608,R790,D388,R414,U670,R845,D394,L572,D612,R842,U792,R959,U7,L285,U769,L410,D940,L319,D182,R42,D774,R758,D457,R10,U82,L861,D901,L310,D217,R644,U305,R92,U339,R252,U460,R609,D486,R553,D798,R809,U552,L183,D238,R138,D147,L343,D597,L670,U237,L878,U872,R789,U268,L97,D313,R22,U343,R907,D646,L36,D516,L808,U622,L927,D982,L810,D149,R390,U101,L565,U488,L588,U426,L386,U305,R503,U227,R969,U201,L698,D850,R800,D961,R387,U632,R543,D541,R750,D174,R543,D237,R487,D932,R220 diff --git a/2019/problem03/part1and2.hs b/2019/problem03/part1and2.hs new file mode 100644 index 0000000..46e37d6 --- /dev/null +++ b/2019/problem03/part1and2.hs @@ -0,0 +1,52 @@ +import Data.List.Split +import qualified Data.Map as Map +import System.Environment + +type Direction = Char +type Point = (Int, Int) + +center :: Point +center = (0, 0) + +manhattan :: Point -> Point -> Int +manhattan (x, y) (x', y') = abs (x - x') + abs (y - y') + +advance :: Point -> Direction -> Point +advance (x, y) dir = case dir of + 'R' -> (x + 1, y) + 'L' -> (x - 1, y) + 'U' -> (x, y + 1) + 'D' -> (x, y - 1) + +points :: Point -> [Direction] -> [(Point, Int)] +points center = tail . scanl step1 (center, 0) + where step1 (p, s) d = (advance p d, s + 1) + +-- Could be optimized to check intersection of lines rather +-- than keeping track of all points. +intersections :: Point -> [Direction] -> [Direction] -> Map.Map Point Int +intersections center a b = Map.intersectionWith (+) (pointMap a) (pointMap b) + where pointMap = Map.fromList . points center + +readDirection :: String -> [Direction] +readDirection (c:n) = replicate (read n) c + +readDirections :: String -> [Direction] +readDirections = concat . map readDirection . splitOn "," + +solve' :: (Map.Map Point Int -> [Int]) -> String -> Int +solve' f s = minimum . f $ xs + where (a:b:_) = lines s + xs = intersections center (readDirections a) (readDirections b) + +solve :: String -> Int +solve = solve' $ map (manhattan center) . Map.keys + +solve2 :: String -> Int +solve2 = solve' $ Map.elems + +main :: IO () +main = do + [f] <- getArgs + content <- readFile f + putStrLn . show . solve $ content