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}")