From 6997fbea377ca44c445e112d594eb077dcc8a76a Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 27 Sep 2024 11:43:21 +0200 Subject: Day 14 --- 2015/day14.exs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2015/day14.exs (limited to '2015/day14.exs') 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}") -- cgit v1.2.3