From 5e531b0aec81ea22cfd62f67b8a658c0c4b85cc1 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 17 Dec 2020 15:19:15 +0100 Subject: [PATCH] Add 2015 day 14 --- haskellAoC/inputs/2015/14 | 9 ++++++++ haskellAoC/src/Y2015/Day14.hs | 43 +++++++++++++++++++++++++++++++++++ haskellAoC/src/Y2015/Days.hs | 2 ++ 3 files changed, 54 insertions(+) create mode 100644 haskellAoC/inputs/2015/14 create mode 100644 haskellAoC/src/Y2015/Day14.hs diff --git a/haskellAoC/inputs/2015/14 b/haskellAoC/inputs/2015/14 new file mode 100644 index 0000000..6cf5489 --- /dev/null +++ b/haskellAoC/inputs/2015/14 @@ -0,0 +1,9 @@ +Dancer can fly 27 km/s for 5 seconds, but then must rest for 132 seconds. +Cupid can fly 22 km/s for 2 seconds, but then must rest for 41 seconds. +Rudolph can fly 11 km/s for 5 seconds, but then must rest for 48 seconds. +Donner can fly 28 km/s for 5 seconds, but then must rest for 134 seconds. +Dasher can fly 4 km/s for 16 seconds, but then must rest for 55 seconds. +Blitzen can fly 14 km/s for 3 seconds, but then must rest for 38 seconds. +Prancer can fly 3 km/s for 21 seconds, but then must rest for 40 seconds. +Comet can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. +Vixen can fly 18 km/s for 5 seconds, but then must rest for 84 seconds. diff --git a/haskellAoC/src/Y2015/Day14.hs b/haskellAoC/src/Y2015/Day14.hs new file mode 100644 index 0000000..4961279 --- /dev/null +++ b/haskellAoC/src/Y2015/Day14.hs @@ -0,0 +1,43 @@ +module Y2015.Day14 (y15day14) where + +import Data.Function (on) +import Data.List + +data Reindeer = Reindeer { + name :: String + , speed :: Int + , flyDuration :: Int + , restDuration :: Int + } deriving (Show) + +time :: Int +time = 2503 + +parseLine :: String -> Reindeer +parseLine input = Reindeer n s fd rd + where input' = words input + n = input' !! 0 + s = read $ input' !! 3 + fd = read $ input' !! 6 + rd = read $ input' !! 13 + +reindeerDistanceAt :: Int -> Reindeer -> Int +reindeerDistanceAt ts (Reindeer _ s d r) = comp * d * s + (min remn d) * s + where cycleTime = d + r + (comp, remn) = ts `quotRem` cycleTime + +reindeersInFrontAt :: [Reindeer] -> Int -> [String] +reindeersInFrontAt rs t = map (name . fst) $ filter ((== leadDistance) . snd) rsWithDist + where leadDistance = snd $ maximumBy (compare `on` snd) $ rsWithDist + rsWithDist = map (\r -> (r, reindeerDistanceAt t r)) rs + +getMaxPoints :: Int -> [Reindeer] -> Int +getMaxPoints duration rs = maximum $ map length $ group $ sort $ + concatMap (reindeersInFrontAt rs) $ [1..duration] + +y15day14 :: [String] -> (String, String) +y15day14 input = (part1, part2) + where part1 = show $ maximum $ map (reindeerDistanceAt time) $ reindeers + part2 = show $ getMaxPoints time reindeers + + reindeers = map parseLine input diff --git a/haskellAoC/src/Y2015/Days.hs b/haskellAoC/src/Y2015/Days.hs index a2ab3e6..c645dab 100644 --- a/haskellAoC/src/Y2015/Days.hs +++ b/haskellAoC/src/Y2015/Days.hs @@ -13,6 +13,7 @@ import Y2015.Day10 import Y2015.Day11 import Y2015.Day12 import Y2015.Day13 +import Y2015.Day14 year2015 :: String -> [String] -> (String, String) year2015 "01" = y15day01 @@ -28,3 +29,4 @@ year2015 "10" = y15day10 year2015 "11" = y15day11 year2015 "12" = y15day12 year2015 "13" = y15day13 +year2015 "14" = y15day14