advent-of-code/2023/8/Main1.hs
2023-12-12 20:25:25 -08:00

27 lines
743 B
Haskell

import qualified Data.HashMap.Strict as HMS
type DataMap = HMS.HashMap String (String, String)
getDirection :: Char -> (a, a) -> a
getDirection 'L' = fst
getDirection 'R' = snd
countTimes :: String -> DataMap -> Int
countTimes directions m = countTimes' directions "AAA" 0
where
countTimes' _ "ZZZ" = id
countTimes' (d:ds) s = countTimes' ds (getDirection d $ m HMS.! s) . succ
getDataMap :: [String] -> DataMap
getDataMap = HMS.fromList . map parseLine
where
get3At x = take 3 . drop x
parseLine l = (get3At 0 l, (get3At 7 l, get3At 12 l))
main :: IO ()
main = do
allLines <- lines <$> readFile "data.txt"
let directions = cycle . head $ allLines
print . countTimes directions . getDataMap . drop 2 $ allLines