2023/day06.py

27 lines
644 B
Python
Raw Normal View History

2023-12-20 15:00:10 +00:00
import fileinput
from math import ceil, floor, sqrt
def race(time, dist):
2023-12-25 11:20:24 +00:00
discr = sqrt(time * time - 4 * dist)
x1 = (time - discr) / 2
x2 = (time + discr) / 2
2023-12-20 15:00:10 +00:00
x1, x2 = floor(x1) + 1, ceil(x2) - 1
return x2 - x1 + 1
2023-12-25 11:20:24 +00:00
def main():
input = list(fileinput.input())
times = [int(x) for x in input[0][11:].split()]
distances = [int(x) for x in input[1][11:].split()]
2023-12-20 15:00:10 +00:00
2023-12-25 11:20:24 +00:00
part1 = 1
for time, dist in zip(times, distances):
part1 *= race(time, dist)
2023-12-20 15:00:10 +00:00
2023-12-25 11:20:24 +00:00
time = int(input[0][11:].replace(" ", ""))
dist = int(input[1][11:].replace(" ", ""))
part2 = race(time, dist)
2023-12-20 15:00:10 +00:00
2023-12-25 11:20:24 +00:00
print(part1, part2)
main()