main
HeNine 11 months ago
parent 35a81a2b7f
commit d9239bbb82

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

@ -0,0 +1,2 @@
Time: 59 70 78 78
Distance: 430 1218 1213 1276

@ -0,0 +1,54 @@
from math import sqrt, floor, ceil
def do_task(input):
lines = input.split("\n")
times = [int(t) for t in lines[0].split()[1:]]
distances = [int(d) for d in lines[1].split()[1:]]
prod = 1
for i in range(len(times)):
a = -1
b = 1 + times[i] - 1
c = -distances[i]
max_h = (-b - sqrt(b**2 - 4 * a * c)) / (2 * a)
min_h = (-b + sqrt(b**2 - 4 * a * c)) / (2 * a) + 1
prod *= ceil(max_h) - floor(min_h)
return prod
# This actually works
def do_task_the_dumb_way(input):
lines = input.split("\n")
times = [int(t) for t in lines[0].split()[1:]]
distances = [int(d) for d in lines[1].split()[1:]]
prod = 1
for i in range(len(times)):
ways = 0
for h in range(times[i]):
d = 0
for t in range(times[i] - h):
d += h
if d > distances[i]:
ways += 1
prod *= ways
return prod
with open("day06/data/input100.txt") as infile:
result = do_task(infile.read())
print(result)
print(result == 288)
with open("day06/data/input101.txt") as infile:
result = do_task(infile.read())
print(result)
print(result == 227850)

@ -0,0 +1,47 @@
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)
Loading…
Cancel
Save