summaryrefslogtreecommitdiff
path: root/2015/day14.exs
blob: 0728486b7bc1671f5bcebd4e05033e0f62c7b5a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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}")