From 193beb51e74cd98f120bc008567b1e0034457fc6 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Wed, 11 Dec 2024 16:06:05 +0530 Subject: [PATCH] day 11 Signed-off-by: Amneesh Singh --- aoc2024.cabal | 7 +++++++ src/Day11.hs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/Day11.hs diff --git a/aoc2024.cabal b/aoc2024.cabal index 4a60924..4eb0ea4 100644 --- a/aoc2024.cabal +++ b/aoc2024.cabal @@ -11,6 +11,7 @@ common common default-extensions: LambdaCase ViewPatterns + TupleSections build-depends: , base >=4.14 && <5 @@ -89,3 +90,9 @@ executable day10 hs-source-dirs: src main-is: Day10.hs build-depends: containers + +executable day11 + import: common + hs-source-dirs: src + main-is: Day11.hs + build-depends: containers diff --git a/src/Day11.hs b/src/Day11.hs new file mode 100644 index 0000000..f6bb8af --- /dev/null +++ b/src/Day11.hs @@ -0,0 +1,30 @@ +module Main where + +import qualified Data.IntMap as IM + +blink :: Int -> [Int] +blink 0 = [1] +blink x + | (l, 0) <- len x `quotRem` 2, + (a, b) <- x `quotRem` (10 ^ l) = + [a, b] + | otherwise = [x * 2024] + where + len :: Int -> Int + len 0 = 0 + len a = 1 + len (a `div` 10) + +blinkAll :: IM.IntMap Int -> IM.IntMap Int +blinkAll xs = IM.fromListWith (+) [(x', c) | (x, c) <- IM.toList xs, x' <- blink x] + +blinkN :: Int -> IM.IntMap Int -> Int +blinkN n = IM.foldr (+) 0 . (!! n) . iterate blinkAll + +main :: IO () +main = + do + raw <- map read . words <$> readFile "./inputs/day11.in" + let counts = IM.fromListWith (+) $ map (,1) raw + + putStr "Part 1: " >> print (blinkN 25 counts) + putStr "Part 2: " >> print (blinkN 75 counts)