This commit is contained in:
2023-12-08 14:10:45 +01:00
parent 1326fe7e33
commit dbfd617fb0
5 changed files with 94 additions and 0 deletions

21
day08/Part2.hs Normal file
View File

@@ -0,0 +1,21 @@
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