Add 2015 day 14

This commit is contained in:
Xavier Morel
2020-12-17 15:19:15 +01:00
parent 20be9d5483
commit 5e531b0aec
3 changed files with 54 additions and 0 deletions

View File

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

View File

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

View File

@@ -13,6 +13,7 @@ import Y2015.Day10
import Y2015.Day11 import Y2015.Day11
import Y2015.Day12 import Y2015.Day12
import Y2015.Day13 import Y2015.Day13
import Y2015.Day14
year2015 :: String -> [String] -> (String, String) year2015 :: String -> [String] -> (String, String)
year2015 "01" = y15day01 year2015 "01" = y15day01
@@ -28,3 +29,4 @@ year2015 "10" = y15day10
year2015 "11" = y15day11 year2015 "11" = y15day11
year2015 "12" = y15day12 year2015 "12" = y15day12
year2015 "13" = y15day13 year2015 "13" = y15day13
year2015 "14" = y15day14