diff --git a/2023/8/Main1.hs b/2023/8/Main1.hs index f8ae9c5..d1ff320 100644 --- a/2023/8/Main1.hs +++ b/2023/8/Main1.hs @@ -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 diff --git a/2023/8/Main2.hs b/2023/8/Main2.hs index 27dffde..dcde066 100644 --- a/2023/8/Main2.hs +++ b/2023/8/Main2.hs @@ -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