day11 first approach

This commit is contained in:
JasterV 2025-04-17 01:57:13 +02:00
parent a73a31a6d3
commit 8ac5f2e3cf
3 changed files with 35 additions and 0 deletions

View file

@ -31,6 +31,7 @@ library
Data.Point
Day1
Day10
Day11
Day2
Day3
Day4

1
input/day11.txt Normal file
View file

@ -0,0 +1 @@
2 72 8949 0 981038 86311 246 7636740

33
src/Day11.hs Normal file
View file

@ -0,0 +1,33 @@
module Day11 (partOne, partTwo, nextStone) where
type Stone = Int
partOne :: String -> Int
partOne raw = sum $ map (blinkN 25) $ parseStones raw
partTwo :: String -> Int
partTwo raw = sum $ map (blinkN 75) $ parseStones raw
blinkN :: Int -> Stone -> Int
blinkN 0 _ = 1
blinkN blinks stone = sum $ map (blinkN (blinks - 1)) $ nextStone stone
-- blinkN blinks stone = concatMap (blinkN (blinks - 1)) (nextStone stone)
nextStone :: Stone -> [Stone]
nextStone stone
| stone == 0 = [1]
| even lengthDigits =
[ read $ take (lengthDigits `div` 2) digits,
read $ drop (lengthDigits `div` 2) digits
]
| otherwise = [stone * 2024]
where
digits :: [Char]
digits = show stone
lengthDigits :: Int
lengthDigits = length digits
parseStones :: String -> [Stone]
parseStones = map read . words