2023 day 6 part 2 solved

kwout 11 months ago
parent 31bdd46e72
commit 7d866c841b
  1. 16
      2023/6.py

@ -1,8 +1,12 @@
from math import pow, ceil, floor
lines = [l.split() for l in open("input.txt", 'r').read().splitlines()]
races = [(int(lines[0][i]), int(lines[1][i])) for i in range(1,len(lines[0]))]
lines = open("input.txt", 'r').read().splitlines()
cols = [l.split() for l in lines]
races = [(int(cols[0][i]), int(cols[1][i])) for i in range(1,len(cols[0]))]
#[(time, record)]
realRace = (int(lines[0].split(": ")[1].replace(" ","")), int(lines[1].split(": ")[1].replace(" ","")))
print(realRace)
#d = t * v
#charge + t = time
@ -13,12 +17,16 @@ races = [(int(lines[0][i]), int(lines[1][i])) for i in range(1,len(lines[0]))]
summa = 1
for race in races:
def findWays(race):
a = -1
b = race[0]
c = -race[1]
rangeStart = floor((-b + pow((pow(b,2)-(4*a*c)),0.5)) / (2*a)) + 1
rangeEnd = ceil((-b - pow((pow(b,2)-(4*a*c)),0.5)) / (2*a)) - 1
summa *= (rangeEnd-rangeStart+1)
return rangeEnd-rangeStart+1
for r in races:
summa *= findWays(r)
print(summa)
print(findWays(realRace))
Loading…
Cancel
Save