44 lines
1.3 KiB
Python
Executable File
44 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3.11
|
|
import re
|
|
from data import Race
|
|
|
|
|
|
def extract(input_file: str) -> Race:
|
|
with open(input_file) as input:
|
|
time = re.match("Time: ((?:\s+\d+)+)", input.readline())
|
|
time = int(re.sub("\s+", "", time.group(1)))
|
|
distance = re.match("Distance: ((?:\s+\d+)+)", input.readline())
|
|
distance = int(re.sub("\s+", "", distance.group(1)))
|
|
return Race(time=time, record=distance)
|
|
|
|
|
|
def find_boundary(race: Race, start: int, end: int, nrange: int, reverse: bool = False) -> int:
|
|
print(race, start, end, nrange, reverse)
|
|
step = nrange if not reverse else -nrange
|
|
if nrange == 1:
|
|
for i in range(start, end + 1, step):
|
|
if race.length_for_button_press(i) > race.record:
|
|
return i
|
|
boundary = start
|
|
for i in range(start, end + 1, step):
|
|
boundary = i
|
|
if race.length_for_button_press(i) > race.record:
|
|
return find_boundary(race, boundary - step, boundary, int(nrange / 10), reverse)
|
|
|
|
|
|
input_file = "input.txt"
|
|
race = extract(input_file)
|
|
|
|
start = end = 0
|
|
step = 1
|
|
while step < race.time:
|
|
step *= 10
|
|
step /= 10
|
|
print(step)
|
|
start = find_boundary(race, 0, race.time, int(step))
|
|
print(start)
|
|
end = find_boundary(race, race.time, start, int(step), True)
|
|
|
|
|
|
print(end - start + 1)
|