2023 day 8: touchup Haskell solutions
This commit is contained in:
parent
504e5631fa
commit
e3a9d35260
|
@ -16,11 +16,11 @@ countTimes directions m = countTimes' directions "AAA" 0
|
||||||
getDataMap :: [String] -> DataMap
|
getDataMap :: [String] -> DataMap
|
||||||
getDataMap = HMS.fromList . map parseLine
|
getDataMap = HMS.fromList . map parseLine
|
||||||
where
|
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 :: IO ()
|
||||||
main = do
|
main = do
|
||||||
allLines <- lines <$> readFile "data.txt"
|
allLines <- lines <$> readFile "data.txt"
|
||||||
let directions = cycle $ head allLines
|
let directions = cycle . head $ allLines
|
||||||
dataMap = getDataMap $ drop 2 allLines
|
print . countTimes directions . getDataMap . drop 2 $ allLines
|
||||||
print $ countTimes directions dataMap
|
|
||||||
|
|
|
@ -15,17 +15,20 @@ countTimes directions m start = countTimes' directions start 0
|
||||||
| otherwise = countTimes' ds (getDirection d $ m HMS.! ss) . succ
|
| otherwise = countTimes' ds (getDirection d $ m HMS.! ss) . succ
|
||||||
|
|
||||||
solve :: String -> DataMap -> Int
|
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 :: [String] -> DataMap
|
||||||
getDataMap = HMS.fromList . map parseLine
|
getDataMap = HMS.fromList . map parseLine
|
||||||
where
|
where
|
||||||
revTake3 = reverse . take 3
|
get3At x = reverse . take 3 . drop x
|
||||||
parseLine l = (revTake3 l, (revTake3 $ drop 7 l, revTake3 $ drop 12 l))
|
parseLine l = (get3At 0 l, (get3At 7 l, get3At 12 l))
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
allLines <- lines <$> readFile "data.txt"
|
allLines <- lines <$> readFile "data.txt"
|
||||||
let directions = cycle $ head allLines
|
let directions = cycle . head $ allLines
|
||||||
dataMap = getDataMap $ drop 2 allLines
|
print . solve directions . getDataMap . drop 2 $ allLines
|
||||||
print $ solve directions dataMap
|
|
||||||
|
|
Loading…
Reference in a new issue