2015 Day 14 in Haskell

This commit is contained in:
germax26 2024-12-03 13:16:07 +11:00
parent abfe80e58f
commit 0bdd0081bb
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM

25
2015/day14/day14.hs Normal file
View File

@ -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