37 lines
1.1 KiB
Python
Executable File
37 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python3.11
|
|
import math
|
|
import re
|
|
from typing import List
|
|
|
|
from data import Race
|
|
|
|
|
|
def extract(input_file: str) -> List[Race]:
|
|
races = []
|
|
with open(input_file) as input:
|
|
times = re.match("Time:((?:\s+\d+)+)", input.readline())
|
|
times = list(map(int, re.split("\s+", times.group(1).lstrip())))
|
|
distances = re.match("Distance:((?:\s+\d+)+)", input.readline())
|
|
distances = list(map(int, re.split("\s+", distances.group(1).lstrip())))
|
|
if len(times) == len(distances):
|
|
for idx in range(len(times)):
|
|
r = Race(time=times[idx], record=distances[idx])
|
|
races.append(r)
|
|
else:
|
|
raise Exception("Times and Distances are not the same length")
|
|
return races
|
|
|
|
|
|
input_file = "input.txt"
|
|
races = extract(input_file)
|
|
record_beatings = [0 for r in races]
|
|
|
|
|
|
for idx, race in enumerate(races):
|
|
lengths = []
|
|
for i in range(race.time):
|
|
lengths.append(race.length_for_button_press(i))
|
|
record_beatings[idx] = len(list(filter(lambda length: length > race.record, lengths)))
|
|
print(record_beatings)
|
|
print(math.prod(record_beatings))
|