Refactor code

This commit is contained in:
Daniele Fucini
2024-12-20 18:00:43 +01:00
parent 5c90ad8c8f
commit cff92ce34f
36 changed files with 448 additions and 674 deletions

34
src/Day11.hs Normal file
View File

@@ -0,0 +1,34 @@
module Day11
( day11_1,
day11_2,
)
where
import qualified Data.Map.Strict as M
blinkStone :: (Int, Int) -> [(Int, Int)]
blinkStone (0, n) = [(1, n)]
blinkStone (s, n) =
let ss = show s
nDigit = length ss
in if even nDigit
then zip (map read [take (nDigit `div` 2) ss, drop (nDigit `div` 2) ss]) [n, n]
else [(s * 2024, n)]
blink :: Int -> M.Map Int Int -> M.Map Int Int
blink 0 m = m
blink n m = blink (n - 1) $ M.fromListWith (+) $ concatMap blinkStone $ M.toList m
day11_1 :: IO ()
day11_1 = do
contents <- M.fromListWith (+) . flip zip (repeat 1) . map read . words <$> readFile "input/day11.txt"
putStrLn $
"Day 11, Puzzle 1 solution: "
++ show (M.foldl (+) 0 $ blink 25 contents)
day11_2 :: IO ()
day11_2 = do
contents <- M.fromListWith (+) . flip zip (repeat 1) . map read . words <$> readFile "input/day11.txt"
putStrLn $
"Day 11, Puzzle 2 solution: "
++ show (M.foldl (+) 0 $ blink 75 contents)