mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
day11 first approach
This commit is contained in:
parent
a73a31a6d3
commit
8ac5f2e3cf
3 changed files with 35 additions and 0 deletions
|
|
@ -31,6 +31,7 @@ library
|
|||
Data.Point
|
||||
Day1
|
||||
Day10
|
||||
Day11
|
||||
Day2
|
||||
Day3
|
||||
Day4
|
||||
|
|
|
|||
1
input/day11.txt
Normal file
1
input/day11.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
2 72 8949 0 981038 86311 246 7636740
|
||||
33
src/Day11.hs
Normal file
33
src/Day11.hs
Normal 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
|
||||
Loading…
Reference in a new issue