You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB
Python

1 year ago
from math import sqrt, floor, ceil
def do_task(input):
lines = input.split("\n")
time = int("".join(lines[0].split()[1:]))
distance = int("".join(lines[1].split()[1:]))
a = -1
b = 1 + time - 1
c = -distance
max_h = (-b - sqrt(b**2 - 4 * a * c)) / (2 * a)
min_h = (-b + sqrt(b**2 - 4 * a * c)) / (2 * a) + 1
return ceil(max_h) - floor(min_h)
# Kind of slow, but workable
def do_task_the_dumb_way(input):
lines = input.split("\n")
time = int("".join(lines[0].split()[1:]))
distance = int("".join(lines[1].split()[1:]))
ways = 0
for h in range(time):
d = (time - h) * h
# This is how dumb you have to make it to make it run slow
# for t in range(time - h):
# d+=h
if d > distance:
ways += 1
return ways
with open("day06/data/input100.txt") as infile:
result = do_task(infile.read())
print(result)
print(result == 71503)
with open("day06/data/input101.txt") as infile:
result = do_task(infile.read())
print(result)
print(result == 42948149)