2023 day 8: touchup Haskell solutions

This commit is contained in:
eriedaberrie 2023-12-11 20:14:45 -08:00
parent 504e5631fa
commit e3a9d35260
2 changed files with 15 additions and 12 deletions

View file

@ -11,16 +11,16 @@ countTimes directions m = countTimes' directions "AAA" 0
where
countTimes' (d:ds) s
| s == "ZZZ" = id
| otherwise = countTimes' ds (getDirection d $ m HMS.! s) . succ
| 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))
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
dataMap = getDataMap $ drop 2 allLines
print $ countTimes directions dataMap
let directions = cycle . head $ allLines
print . countTimes directions . getDataMap . drop 2 $ allLines

View file

@ -11,21 +11,24 @@ countTimes :: String -> DataMap -> String -> Int
countTimes directions m start = countTimes' directions start 0
where
countTimes' (d:ds) ss@(s:_)
| s == 'Z' = id
| s == 'Z' = id
| otherwise = countTimes' ds (getDirection d $ m HMS.! ss) . succ
solve :: String -> DataMap -> Int
solve directions m = foldl1' lcm $ map (countTimes directions m) $ filter ((== 'A') . head) $ HMS.keys m
solve directions m = foldl1' lcm
. map (countTimes directions m)
. filter ((== 'A') . head)
. HMS.keys
$ m
getDataMap :: [String] -> DataMap
getDataMap = HMS.fromList . map parseLine
where
revTake3 = reverse . take 3
parseLine l = (revTake3 l, (revTake3 $ drop 7 l, revTake3 $ drop 12 l))
get3At x = reverse . 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
dataMap = getDataMap $ drop 2 allLines
print $ solve directions dataMap
let directions = cycle . head $ allLines
print . solve directions . getDataMap . drop 2 $ allLines