diff --git a/day-11-haskell/day11-haskell.cabal b/day-11-haskell/day11-haskell.cabal index faaca03..6b41699 100644 --- a/day-11-haskell/day11-haskell.cabal +++ b/day-11-haskell/day11-haskell.cabal @@ -26,6 +26,7 @@ library ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints build-depends: base >=4.7 && <5 + , containers , flow default-language: Haskell2010 diff --git a/day-11-haskell/package.yaml b/day-11-haskell/package.yaml index 5f0549e..94c40e3 100644 --- a/day-11-haskell/package.yaml +++ b/day-11-haskell/package.yaml @@ -24,6 +24,7 @@ library: source-dirs: src dependencies: - flow + - containers executables: day11-haskell-exe: diff --git a/day-11-haskell/src/Lib.hs b/day-11-haskell/src/Lib.hs index b614670..29eebf4 100644 --- a/day-11-haskell/src/Lib.hs +++ b/day-11-haskell/src/Lib.hs @@ -1,13 +1,14 @@ module Lib (task1, task2) where import Control.Monad (join) +import qualified Data.Map.Strict as Map import Flow task1 :: String -> Int -task1 input = parse input |> recurse blink 25 |> length +task1 input = parse input |> map (\stone -> (stone, 1)) |> recurse blink 25 |> map (\(_, cnt) -> cnt) |> sum task2 :: String -> Int -task2 input = parse input |> recurse blink 75 |> length +task2 input = parse input |> map (\stone -> (stone, 1)) |> recurse blink 75 |> map (\(_, cnt) -> cnt) |> sum parse :: String -> [Int] parse input = words input |> map read @@ -15,14 +16,15 @@ parse input = words input |> map read recurse :: (a -> a) -> Int -> a -> a recurse fun count start = iterate fun start !! count -blink :: [Int] -> [Int] -blink stones = map blinkItem stones |> join +blink :: [(Int, Int)] -> [(Int, Int)] +blink stones = map (\(stone, mult) -> blinkItem stone |> map (\s -> (s, mult))) stones |> join |> Map.fromListWith (+) |> Map.toList blinkItem :: Int -> [Int] blinkItem 0 = [1] -blinkItem number = let digits = show number |> map (read . pure) :: [Int] - in if length digits `mod` 2 == 0 - then case splitAt (length digits `div` 2) digits of - (first, second) -> [map show first |> join |> read, map show second |> join |> read] +blinkItem number = let str = show number + in if length str `mod` 2 == 0 + then case splitAt (length str `div` 2) str of + (first, second) -> [read first, read second] else [number * 2024] +