You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.4 KiB
Python

import re
from collections import defaultdict
with open("input") as f:
lines = [n.strip() for n in f.readlines()]
MAX_TIME = 2503
# MAX_TIME = 1000
def get_distance(speed, fly_time, rest_time, time_elapsed):
cycle = fly_time + rest_time
count_cycles, rest = divmod(time_elapsed, cycle)
rest = min(rest, fly_time)
distance = count_cycles * fly_time * speed + rest * speed
return distance
reindeers = list()
for line in lines:
name = line.split(" ")[0]
[speed, fly_time, rest_time] = [int(x) for x in re.findall("[0-9]+", line)]
reindeers.append((name, speed, fly_time, rest_time))
# example:
# reindeers = [("Comet", 14, 10, 127), ("Dancer", 16, 11, 162)]
POINTS = defaultdict(int)
for second in range(1, MAX_TIME + 1):
max_distance = None
leaders = []
for reindeer in reindeers:
name, speed, fly_time, rest_time = reindeer
distance = get_distance(speed, fly_time, rest_time, second)
if max_distance is None or distance > max_distance:
max_distance = distance
leaders = [name]
elif distance == max_distance:
leaders.append(name)
for leader in leaders:
POINTS[leader] += 1
print("Standings:")
for name, points in POINTS.items():
print(f" {name.ljust(8)}: {points}")
print()
print("Winner points:")
print(max(POINTS.values()))
# 472: too low