From 0bdd0081bb4d28428ff608d4b511237bbe29ea7a Mon Sep 17 00:00:00 2001 From: germax26 Date: Tue, 3 Dec 2024 13:16:07 +1100 Subject: [PATCH] 2015 Day 14 in Haskell --- 2015/day14/day14.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2015/day14/day14.hs diff --git a/2015/day14/day14.hs b/2015/day14/day14.hs new file mode 100644 index 0000000..2d9aa70 --- /dev/null +++ b/2015/day14/day14.hs @@ -0,0 +1,25 @@ +import Aoc +import Data.Bifunctor ( bimap ) +import Data.List ( sort, group ) + +parseLine :: String -> (String, (Int, Int, Int)) +parseLine line = + let [name, "can", "fly", speed, "km/s", "for", flyTime, "seconds,", "but", "then", "must", "rest", "for", restTime, "seconds."] = words line in + (name, (read speed, read flyTime, read restTime)) + +atTime :: Int -> (String, (Int, Int, Int)) -> (String, Int) +atTime time (name, (s, f, r)) = (name, (*s) $ uncurry (+) $ bimap (*f) (min f) $ quotRem time (f+r)) + +part1 :: [(String, (Int, Int, Int))] -> Int +part1 = maximum . map (snd . atTime 2503) + +part2 :: [(String, (Int, Int, Int))] -> Int +part2 reindeer = maximum $ map length $ group $ sort $ concatMap winners [1..2503] + where + winners t = + let distances = map (atTime t) reindeer in + let maxDistance = maximum $ map snd distances in + map fst $ filter ((==maxDistance) . snd) distances + +main :: IO () +main = aocMain (map parseLine) part1 part2 \ No newline at end of file