Day eleven

This commit is contained in:
2024-12-11 21:15:35 +01:00
parent c255b82f2d
commit 8802d0c001
9 changed files with 229 additions and 0 deletions

28
day-11-haskell/src/Lib.hs Normal file
View File

@@ -0,0 +1,28 @@
module Lib (task1, task2) where
import Control.Monad (join)
import Flow
task1 :: String -> Int
task1 input = parse input |> recurse blink 25 |> length
task2 :: String -> Int
task2 input = parse input |> recurse blink 75 |> length
parse :: String -> [Int]
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
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]
else [number * 2024]