advent-of-code/2023/8/Main1.hs

27 lines
755 B
Haskell
Raw Normal View History

2023-12-07 23:10:02 -08:00
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
parseLine l = (take 3 l, (take 3 $ drop 7 l, take 3 $ drop 12 l))
main :: IO ()
main = do
allLines <- lines <$> readFile "data.txt"
let directions = cycle $ head allLines
dataMap = getDataMap $ drop 2 allLines
print $ countTimes directions dataMap