summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <j.wernick@eyeo.com>2024-09-27 11:43:21 +0200
committerJustin Wernick <j.wernick@eyeo.com>2024-09-27 11:43:21 +0200
commit6997fbea377ca44c445e112d594eb077dcc8a76a (patch)
tree102407ab64bc16ffa8832185d471e19f2ef6eb47
parent583229b4db9d4a62962dfb70e270b17757c718cf (diff)
Day 14HEADmain
-rw-r--r--2015/day14.exs48
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}")