From e1f560a2267ad328990347b4d3b3d8bfdf1035cc Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 2 Dec 2025 10:05:40 +0100 Subject: [PATCH] Day 2 done --- aoc.cabal | 12 ++++++++++++ app/2.hs | 34 ++++++++++++++++++++++++++++++++-- inputs/2 | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 inputs/2 diff --git a/aoc.cabal b/aoc.cabal index 0158193..a631bdc 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -21,6 +21,7 @@ executable 1 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 @@ -30,6 +31,7 @@ executable 2 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 3 @@ -38,6 +40,7 @@ executable 3 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 4 @@ -46,6 +49,7 @@ executable 4 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 5 @@ -54,6 +58,7 @@ executable 5 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 6 @@ -62,6 +67,7 @@ executable 6 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 7 @@ -70,6 +76,7 @@ executable 7 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 8 @@ -78,6 +85,7 @@ executable 8 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 9 @@ -86,6 +94,7 @@ executable 9 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 10 @@ -94,6 +103,7 @@ executable 10 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 11 @@ -102,6 +112,7 @@ executable 11 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 executable 12 @@ -110,5 +121,6 @@ executable 12 -- other-modules: -- other-extensions: build-depends: base ^>=4.21.0.0 + , text hs-source-dirs: app default-language: GHC2021 diff --git a/app/2.hs b/app/2.hs index dd9d70e..0213338 100644 --- a/app/2.hs +++ b/app/2.hs @@ -1,6 +1,36 @@ -{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE LambdaCase, OverloadedStrings #-} module Main where +import Data.Text (Text) +import Data.Text qualified as T +import Data.Text.IO qualified as T +import Data.Functor + +parse :: Text -> [(Int,Int)] +parse = map + ((\(a,b) -> (read (T.unpack a), read (T.unpack $ T.drop 1 b))) . T.breakOn "-") . + T.split (==',') + +solve1 :: [(Int,Int)] -> Int +solve1 = sum . go . (>>= \(a,b) -> [a..b]) + where + go :: [Int] -> [Int] + go = filter + (\a -> let d = T.show a in uncurry (==) $ + T.splitAt (T.length d `div` 2) d) + +solve2 :: [(Int,Int)] -> Int +solve2 = sum . go . (>>= \(a,b) -> [a..b]) + where + go :: [Int] -> [Int] + go = filter + (\a -> let d = T.show a in + or $ [1..T.length d - 1] >>= \i -> + let c = T.chunksOf i d in pure $ + all (== head c) c) + main :: IO () -main = pure () +main = T.readFile "inputs/2" <&> parse >>= \i -> + print (solve1 i) >> + print (solve2 i) diff --git a/inputs/2 b/inputs/2 new file mode 100644 index 0000000..fafb968 --- /dev/null +++ b/inputs/2 @@ -0,0 +1 @@ +2558912-2663749,1-19,72-85,82984-100358,86-113,193276-237687,51-69,779543-880789,13004-15184,2768-3285,4002-4783,7702278-7841488,7025-8936,5858546565-5858614010,5117615-5149981,4919-5802,411-466,126397-148071,726807-764287,7454079517-7454227234,48548-61680,67606500-67729214,9096-10574,9999972289-10000034826,431250-455032,907442-983179,528410-680303,99990245-100008960,266408-302255,146086945-146212652,9231222-9271517,32295166-32343823,32138-36484,4747426142-4747537765,525-652,333117-414840,13413537-13521859,1626-1972,49829276-50002273,69302-80371,8764571787-8764598967,5552410836-5552545325,660-782,859-1056