2023-12-06 07:47:59 +00:00
|
|
|
import math
|
2023-12-04 18:32:41 +00:00
|
|
|
import sys
|
|
|
|
|
2023-12-06 06:13:53 +00:00
|
|
|
|
|
|
|
def extreme_times_to_beat(time: int, distance: int) -> tuple[int, int]:
|
2023-12-06 07:47:59 +00:00
|
|
|
# formula (T = total time, D = target distance)
|
|
|
|
# - speed(t) = t,
|
|
|
|
# - distance(t) = (T - t) * speed(t)
|
|
|
|
# - distance(t) > D
|
|
|
|
# <=> (T - t) * t > D
|
|
|
|
# <=> -t^2 + T * t - D >= 0
|
|
|
|
|
|
|
|
a, b, c = -1, time, -distance
|
2023-12-06 06:13:53 +00:00
|
|
|
d = b * b - 4 * a * c
|
|
|
|
|
|
|
|
t1 = math.ceil(-(b - math.sqrt(d)) / (2 * a))
|
|
|
|
t2 = math.floor(-(b + math.sqrt(d)) / (2 * a))
|
|
|
|
|
|
|
|
if (time - t1) * t1 == distance:
|
|
|
|
t1 += 1
|
|
|
|
|
|
|
|
if (time - t2) * t2 == distance:
|
|
|
|
t2 -= 1
|
|
|
|
|
|
|
|
return t1, t2
|
|
|
|
|
|
|
|
|
2023-12-06 07:47:59 +00:00
|
|
|
lines = sys.stdin.read().splitlines()
|
|
|
|
|
2023-12-04 18:32:41 +00:00
|
|
|
# part 1
|
2023-12-06 06:13:53 +00:00
|
|
|
times = list(map(int, lines[0].split()[1:]))
|
|
|
|
distances = list(map(int, lines[1].split()[1:]))
|
2023-12-06 07:47:59 +00:00
|
|
|
answer_1 = math.prod(
|
|
|
|
t2 - t1 + 1
|
|
|
|
for t1, t2 in (
|
|
|
|
extreme_times_to_beat(time, distance)
|
|
|
|
for time, distance in zip(times, distances)
|
|
|
|
)
|
2023-12-06 06:13:53 +00:00
|
|
|
)
|
2023-12-04 18:32:41 +00:00
|
|
|
print(f"answer 1 is {answer_1}")
|
|
|
|
|
|
|
|
# part 2
|
2023-12-06 06:13:53 +00:00
|
|
|
time = int(lines[0].split(":")[1].strip().replace(" ", ""))
|
|
|
|
distance = int(lines[1].split(":")[1].strip().replace(" ", ""))
|
|
|
|
t1, t2 = extreme_times_to_beat(time, distance)
|
|
|
|
answer_2 = t2 - t1 + 1
|
2023-12-04 18:32:41 +00:00
|
|
|
print(f"answer 2 is {answer_2}")
|