advent-of-code/2023/8/Main1.hs
2023-12-11 20:14:45 -08:00

27 lines
755 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' (d:ds) s
| s == "ZZZ" = id
| otherwise = 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