58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
import tqdm
|
|
import re
|
|
|
|
input = "input.txt"
|
|
|
|
times = []
|
|
distances = []
|
|
|
|
with open(input, "r") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line.startswith("Time"):
|
|
times = map(int, re.split("\\s+", line)[1:])
|
|
else:
|
|
distances = map(int, re.split("\\s+", line)[1:])
|
|
|
|
races = []
|
|
|
|
for time, distance in zip(times, distances):
|
|
races.append((time, distance))
|
|
|
|
part1 = 1
|
|
for time, record_distance in races:
|
|
record_beaten_times = 0
|
|
for time_held in range(time):
|
|
time_left = time - time_held
|
|
speed = time_held
|
|
|
|
distance = speed * time_left
|
|
|
|
if distance > record_distance:
|
|
record_beaten_times += 1
|
|
part1 *= record_beaten_times
|
|
|
|
print("part1", part1)
|
|
|
|
time = 0
|
|
record_distance = 0
|
|
|
|
with open(input, "r") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line.startswith("Time"):
|
|
time = int("".join(re.split("\\s+", line)[1:]))
|
|
else:
|
|
record_distance = int("".join(re.split("\\s+", line)[1:]))
|
|
|
|
print(time)
|
|
part2 = 0
|
|
for time_held in tqdm.tqdm(range(time)):
|
|
time_left = time - time_held
|
|
speed = time_held
|
|
|
|
distance = speed * time_left
|
|
|
|
if distance > record_distance:
|
|
part2 += 1
|
|
print("part2", part2)
|