day6.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. from math import sqrt, ceil, floor
  3. with open(os.path.join(os.path.dirname(__file__), "example.txt")) as example:
  4. example_data = example.read().splitlines()
  5. with open(os.path.join(os.path.dirname(__file__), "input.txt")) as example:
  6. input_data = example.read().splitlines()
  7. def solve(times, dists):
  8. def root_eq(b, c):
  9. D = b**2 - 4 * c
  10. x1 = (b - sqrt(D)) / 2
  11. x2 = (b + sqrt(D)) / 2
  12. return x1, x2
  13. total = 1
  14. for time, dist in zip(times, dists):
  15. at_least, less_then = root_eq(time, dist)
  16. solution_number = ceil(less_then) - floor(at_least) - 1
  17. total *= solution_number
  18. print(total)
  19. return total
  20. def parse1(data):
  21. times = [int(n) for n in data[0][5:].split()]
  22. dists = [int(n) for n in data[1][9:].split()]
  23. return times, dists
  24. def parse2(data):
  25. times = [int(data[0][5:].replace(" ", ""))]
  26. dists = [int(data[1][9:].replace(" ", ""))]
  27. return times, dists
  28. solve(*parse1(example_data))
  29. solve(*parse1(input_data))
  30. solve(*parse2(example_data))
  31. solve(*parse2(input_data))