diff --git a/app/Day9.hs b/app/Day9.hs new file mode 100644 index 0000000..6f61681 --- /dev/null +++ b/app/Day9.hs @@ -0,0 +1,55 @@ +module Main where + +import Data.List (uncons) +import Data.Maybe (mapMaybe) +import Data.Set (Set) +import Data.Set qualified as S +import System.Environment (getArgs) + +main :: IO () +main = do + input <- maybe (error "Missing argument to input file") (readFile . fst) =<< uncons <$> getArgs + putStrLn $ unwords ["Part 1:", show $ part1 input] + putStrLn $ unwords ["Part 2:", show $ part2 input] + +area :: (Int, Int) -> (Int, Int) -> Int +area (x1, y1) (x2, y2) = x' * y' + where + x' = max x1 x2 - min x1 x2 + 1 + y' = max y1 y2 - min y1 y2 + 1 + +parse :: String -> [(Int, Int)] +parse = mapMaybe (fmap fst . uncons . go) . lines + where + go :: String -> [(Int, Int)] + go s = do + (x, ',' : s1) <- reads s + (y, []) <- reads s1 + pure (x, y) + +combine :: (Ord a) => [a] -> [(a, a)] +combine as = S.toList $ foldl' (go as) S.empty as + where + go :: (Ord a) => [a] -> Set (a, a) -> a -> Set (a, a) + go as' pairs b = foldl' (\pairs' a -> if a /= b then S.insert (a, b) pairs' else pairs') pairs as' + +-- | Idea: Generate pairs of all coordinates and calculate the area of +-- them. Take the maximum. +part1 :: String -> Int +part1 = maximum . map (uncurry area) . combine . parse + +part2 :: String -> Int +part2 = error "Not implemented" + +testInput :: String +testInput = + unlines + [ "7,1", + "11,1", + "11,7", + "9,7", + "9,5", + "2,5", + "2,3", + "7,3" + ] diff --git a/input/day9.txt b/input/day9.txt new file mode 100644 index 0000000..45aaae8 --- /dev/null +++ b/input/day9.txt @@ -0,0 +1,496 @@ +98159,50267 +98159,51472 +97633,51472 +97633,52686 +97719,52686 +97719,53876 +97424,53876 +97424,55073 +97277,55073 +97277,56385 +98002,56385 +98002,57598 +97822,57598 +97822,58791 +97528,58791 +97528,59917 +96930,59917 +96930,61076 +96556,61076 +96556,62269 +96323,62269 +96323,63419 +95931,63419 +95931,64536 +95443,64536 +95443,65681 +95057,65681 +95057,67050 +95262,67050 +95262,68060 +94492,68060 +94492,69354 +94417,69354 +94417,70160 +93242,70160 +93242,71374 +92969,71374 +92969,72458 +92417,72458 +92417,73530 +91842,73530 +91842,74584 +91236,74584 +91236,75677 +90688,75677 +90688,76452 +89655,76452 +89655,77661 +89274,77661 +89274,78788 +88751,78788 +88751,79775 +88028,79775 +88028,80259 +86691,80259 +86691,81514 +86299,81514 +86299,82673 +85762,82673 +85762,83462 +84812,83462 +84812,84332 +83952,84332 +83952,85006 +82907,85006 +82907,86150 +82297,86150 +82297,86243 +80765,86243 +80765,87303 +80072,87303 +80072,88050 +79116,88050 +79116,88801 +78161,88801 +78161,89485 +77158,89485 +77158,89814 +75923,89814 +75923,91013 +75247,91013 +75247,91922 +74365,91922 +74365,92123 +73074,92123 +73074,92597 +71950,92597 +71950,93521 +71048,93521 +71048,93946 +69896,93946 +69896,94548 +68824,94548 +68824,95077 +67716,95077 +67716,94673 +66263,94673 +66263,95787 +65366,95787 +65366,95448 +63982,95448 +63982,96441 +63009,96441 +63009,96286 +61709,96286 +61709,97203 +60677,97203 +60677,97451 +59476,97451 +59476,97381 +58220,97381 +58220,97948 +57072,97948 +57072,97911 +55831,97911 +55831,97621 +54575,97621 +54575,97382 +53343,97382 +53343,97773 +52156,97773 +52156,98453 +50958,98453 +50958,97637 +49734,97637 +49734,97715 +48525,97715 +48525,98213 +47285,98213 +47285,97955 +46079,97955 +46079,98059 +44842,98059 +44842,97419 +43692,97419 +43692,97797 +42405,97797 +42405,96724 +41356,96724 +41356,97156 +40035,97156 +40035,97175 +38775,97175 +38775,96752 +37617,96752 +37617,96153 +36515,96153 +36515,95794 +35351,95794 +35351,95118 +34297,95118 +34297,95129 +32999,95129 +32999,94856 +31792,94856 +31792,94140 +30766,94140 +30766,93159 +29877,93159 +29877,92965 +28627,92965 +28627,92361 +27571,92361 +27571,92258 +26236,92258 +26236,91298 +25378,91298 +25378,90318 +24556,90318 +24556,89731 +23496,89731 +23496,89062 +22488,89062 +22488,88655 +21282,88655 +21282,87756 +20436,87756 +20436,87174 +19342,87174 +19342,86161 +18605,86161 +18605,85765 +17323,85765 +17323,84393 +16939,84393 +16939,83733 +15888,83733 +15888,82936 +14962,82936 +14962,82021 +14158,82021 +14158,81165 +13285,81165 +13285,80224 +12508,80224 +12508,79043 +12044,79043 +12044,78076 +11315,78076 +11315,77171 +10494,77171 +10494,75993 +10077,75993 +10077,75285 +8925,75285 +8925,74240 +8292,74240 +8292,73000 +8012,73000 +8012,72144 +7024,72144 +7024,70844 +6901,70844 +6901,69691 +6505,69691 +6505,68616 +5942,68616 +5942,67632 +5135,67632 +5135,66251 +5358,66251 +5358,65319 +4352,65319 +4352,64024 +4414,64024 +4414,63031 +3481,63031 +3481,61756 +3529,61756 +3529,60589 +3183,60589 +3183,59338 +3242,59338 +3242,58154 +2996,58154 +2996,56939 +2952,56939 +2952,55761 +2665,55761 +2665,54579 +2336,54579 +2336,53408 +1698,53408 +1698,52175 +1795,52175 +1795,50954 +1721,50954 +1721,50248 +94645,50248 +94645,48530 +2464,48530 +2464,47293 +1919,47293 +1919,46101 +2310,46101 +2310,44902 +2503,44902 +2503,43690 +2565,43690 +2565,42405 +2206,42405 +2206,41300 +2971,41300 +2971,40122 +3257,40122 +3257,38840 +3096,38840 +3096,37707 +3590,37707 +3590,36626 +4224,36626 +4224,35360 +4232,35360 +4232,34350 +5033,34350 +5033,32938 +4706,32938 +4706,31864 +5321,31864 +5321,30822 +5988,30822 +5988,29918 +6927,29918 +6927,28676 +7134,28676 +7134,27368 +7255,27368 +7255,26694 +8556,26694 +8556,25286 +8546,25286 +8546,24239 +9178,24239 +9178,23202 +9827,23202 +9827,22561 +11041,22561 +11041,21672 +11870,21672 +11870,20315 +12088,20315 +12088,19561 +13091,19561 +13091,18499 +13716,18499 +13716,17778 +14732,17778 +14732,16747 +15406,16747 +15406,15778 +16156,15778 +16156,14823 +16933,14823 +16933,14092 +17920,14092 +17920,13329 +18872,13329 +18872,12914 +20102,12914 +20102,11662 +20664,11662 +20664,11318 +21925,11318 +21925,10073 +22539,10073 +22539,9823 +23840,9823 +23840,9216 +24893,9216 +24893,8250 +25735,8250 +25735,8013 +27000,8013 +27000,7603 +28153,7603 +28153,6481 +28952,6481 +28952,5889 +30028,5889 +30028,5497 +31195,5497 +31195,5386 +32466,5386 +32466,4832 +33556,4832 +33556,4468 +34719,4468 +34719,3710 +35758,3710 +35758,3740 +37041,3740 +37041,3605 +38262,3605 +38262,3557 +39494,3557 +39494,2580 +40529,2580 +40529,2793 +41809,2793 +41809,2151 +42942,2151 +42942,2316 +44196,2316 +44196,2626 +45448,2626 +45448,2373 +46639,2373 +46639,2514 +47856,2514 +47856,1535 +49041,1535 +49041,1640 +50269,1640 +50269,1976 +51484,1976 +51484,2129 +52694,2129 +52694,2141 +53912,2141 +53912,2418 +55106,2418 +55106,2511 +56316,2511 +56316,3056 +57459,3056 +57459,3243 +58649,3243 +58649,2770 +59980,2770 +59980,3191 +61136,3191 +61136,3961 +62193,3961 +62193,3730 +63518,3730 +63518,4645 +64507,4645 +64507,4791 +65734,4791 +65734,4677 +67072,4677 +67072,5228 +68173,5228 +68173,6426 +68986,6426 +68986,6536 +70263,6536 +70263,6857 +71461,6857 +71461,7573 +72463,7573 +72463,8382 +73403,8382 +73403,9002 +74441,9002 +74441,9191 +75752,9191 +75752,9998 +76683,9998 +76683,11110 +77390,11110 +77390,11262 +78778,11262 +78778,11911 +79822,11911 +79822,12614 +80831,12614 +80831,13639 +81567,13639 +81567,14385 +82538,14385 +82538,15325 +83329,15325 +83329,15937 +84444,15937 +84444,17201 +84891,17201 +84891,18096 +85709,18096 +85709,19014 +86502,19014 +86502,19592 +87717,19592 +87717,20687 +88306,20687 +88306,22064 +88489,22064 +88489,23010 +89241,23010 +89241,23934 +90033,23934 +90033,24664 +91154,24664 +91154,25990 +91310,25990 +91310,27161 +91692,27161 +91692,27891 +92904,27891 +92904,29292 +92816,29292 +92816,30207 +93715,30207 +93715,31211 +94463,31211 +94463,32417 +94738,32417 +94738,33487 +95359,33487 +95359,34662 +95700,34662 +95700,35837 +96033,35837 +96033,36919 +96694,36919 +96694,38202 +96633,38202 +96633,39471 +96544,39471 +96544,40630 +96914,40630 +96914,41819 +97152,41819 +97152,43058 +97057,43058 +97057,44145 +98103,44145 +98103,45437 +97492,45437 +97492,46636 +97663,46636 +97663,47846 +97715,47846 +97715,49054 +97829,49054 +97829,50267