Files
advent-of-code-2023/day08/Part2.hs
2023-12-08 14:17:16 +01:00

22 lines
740 B
Haskell

module Part2 where
import Commons
import Data.HashMap.Internal.Strict (keys, (!))
getStarts :: [(String, Instruction)] -> [String]
getStarts [] = []
getStarts (([a, b, 'A'], ILeft): t) = [a, b, 'A']: getStarts t
getStarts (_: t) = getStarts t
getDistanceToEnd :: [Instruction] -> Network -> String -> Int
getDistanceToEnd [] network start = getDistanceToEnd (instructions network) network start
getDistanceToEnd _ _ [_, _, 'Z'] = 0
getDistanceToEnd (ih: t) network start = 1 + getDistanceToEnd t network (nodes network ! (start, ih))
getAllDistances :: Network -> [Int]
getAllDistances network = map (getDistanceToEnd [] network) . getStarts . keys $ nodes network
getSteps :: Network -> Int
getSteps = foldr lcm 1 . getAllDistances