Day eleven, but correct

This commit is contained in:
2024-12-12 00:02:12 +01:00
parent 8802d0c001
commit 4e2ef454b5
3 changed files with 12 additions and 8 deletions

View File

@@ -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]