Created
December 11, 2024 18:48
-
-
Save skatenerd/6a820031d8de505daf53c11d16f071e5 to your computer and use it in GitHub Desktop.
2024 Day Eleven
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DayEleven (partOne, partTwo, turnStone) where | |
import qualified Data.Map as DM | |
splitDownMiddle :: Int -> [Int] | |
splitDownMiddle toSplit = [leftSide, rightSide] | |
where stringRepresentation = show toSplit | |
newSize = length stringRepresentation `div` 2 | |
leftSide = read $ take newSize stringRepresentation | |
rightSide = read $ drop newSize stringRepresentation | |
turnStone :: Int -> [Int] | |
turnStone 0 = [1] | |
turnStone n | |
| even $ length $ show n = splitDownMiddle n | |
| otherwise = [2024 * n] | |
partOne :: [Int] -> [Int] | |
partOne inputStones = iterate takeTurn inputStones !! 25 | |
where takeTurn = concatMap turnStone | |
fastVersion :: Int -> Int -> Int | |
fastVersion = calculateCached | |
where cache = DM.fromList [((base, distance), sum (map (calculateCached (distance - 1)) (turnStone base))) | base <- [0..100], distance <- [0..75]] | |
calculateCached :: Int -> Int -> Int | |
calculateCached 0 _ = 1 | |
calculateCached newDistance stone = DM.findWithDefault fallback (stone, newDistance) cache | |
where fallback = sum $ map (calculateCached (newDistance - 1)) $ turnStone stone | |
partTwo :: Int -> [Int] -> Int | |
partTwo howFar inputStones = sum $ map (fastVersion howFar) inputStones | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment