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
|
from dataclasses import dataclass
@dataclass
class Reindeer:
name: str
speed: int
fly_time: int
rest_time: int
def distance_after(self, seconds):
cycle_time = self.fly_time + self.rest_time
cycles = seconds // cycle_time
remaining_time = seconds % cycle_time
if remaining_time > self.fly_time:
remaining_time = self.fly_time
return cycles * self.speed * self.fly_time + remaining_time * self.speed
def part1(reindeer: list[Reindeer]) -> int:
return max(r.distance_after(2503) for r in reindeer)
def part2(reindeer: list[Reindeer]) -> int:
points = [0] * len(reindeer)
for s in range(1, 2504):
distances = [r.distance_after(s) for r in reindeer]
max_dist = max(distances)
for i, d in enumerate(distances):
if d == max_dist:
points[i] += 1
return max(points)
if __name__ == '__main__':
reindeer = [
Reindeer('Vixen', 8, 8, 53),
Reindeer('Blitzen', 13, 4, 49),
Reindeer('Rudolph', 20, 7, 132),
Reindeer('Cupid', 12, 4, 43),
Reindeer('Donner', 9, 5, 38),
Reindeer('Dasher', 10, 4, 37),
Reindeer('Comet', 3, 37, 76),
Reindeer('Prancer', 9, 12, 97),
Reindeer('Dancer', 37, 1, 36),
]
print(part1(reindeer))
print(part2(reindeer))
|