2023/day6/part2.py
Fedaya 8b2bac0791 Day 6 completed
Day5 part 2 still ongoing
2023-12-06 08:12:32 +01:00

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)