Day 8
This commit is contained in:
21
day08/Part2.hs
Normal file
21
day08/Part2.hs
Normal 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
|
||||
Reference in New Issue
Block a user