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
2023-12-11 20:14:45 -08:00
| otherwise = countTimes' ds (getDirection d $ m HMS.! s) . succ
2023-12-07 23:10:02 -08:00
getDataMap :: [String] -> DataMap
getDataMap = HMS.fromList . map parseLine
where
2023-12-11 20:14:45 -08:00
get3At x = take 3 . drop x
parseLine l = (get3At 0 l, (get3At 7 l, get3At 12 l))
2023-12-07 23:10:02 -08:00
main :: IO ()
main = do
allLines <- lines <$> readFile "data.txt"
2023-12-11 20:14:45 -08:00
let directions = cycle . head $ allLines
print . countTimes directions . getDataMap . drop 2 $ allLines