diff options
Diffstat (limited to '2015')
-rw-r--r-- | 2015/day14.exs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/2015/day14.exs b/2015/day14.exs new file mode 100644 index 0000000..0728486 --- /dev/null +++ b/2015/day14.exs @@ -0,0 +1,48 @@ +reindeer = + File.stream!("inputs/day14.txt") + |> Enum.map(fn line -> + [speed, stamina, recovery] = + Regex.run( + ~r/\w+ can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./, + line + ) + |> Stream.drop(1) + |> Stream.map(&Integer.parse/1) + |> Enum.map(fn {i, _} -> i end) + + {speed, stamina, recovery} + end) + +totalRaceLength = 2503 + +distancesAfterRaceLength = fn raceLength -> + Enum.map(reindeer, fn {speed, stamina, recovery} -> + fullIterationDuration = stamina + recovery + fullIterationCount = div(raceLength, fullIterationDuration) + lastIterationDuration = rem(raceLength, fullIterationDuration) + lastIterationRuntime = min(stamina, lastIterationDuration) + + runtime = fullIterationCount * stamina + lastIterationRuntime + runtime * speed + end) +end + +maxDistance = distancesAfterRaceLength.(totalRaceLength) |> Enum.max() +IO.puts("Max distance: #{maxDistance}") + +maxPoints = + Enum.map(1..totalRaceLength, fn raceLength -> + distances = distancesAfterRaceLength.(raceLength) + maxDistance = Enum.max(distances) + + Enum.map(distances, fn + ^maxDistance -> 1 + _ -> 0 + end) + end) + |> List.zip() + |> Enum.map(&Tuple.to_list/1) + |> Enum.map(&Enum.sum/1) + |> Enum.max() + +IO.puts("Max points: #{maxPoints}") |