Advertisement
hhoppe

Advent of code 2023 day 6

Dec 6th, 2023 (edited)
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. def day6(s, *, part2=False):
  2.   if part2:
  3.     s = s.replace(' ', '')
  4.   ts, ds = (map(int, line.split(':')[1].split()) for line in s.splitlines())
  5.  
  6.   def num_wins(t, d):
  7.     # Quadratic equation -t**2 * x**2 + t * x - d = 0 has roots x = u \pm v with:
  8.     u, v = t / 2, (t**2 - 4 * d) ** 0.5 / 2
  9.     return math.ceil(u + v - 1) - math.floor(u - v + 1) + 1
  10.  
  11.   return math.prod(num_wins(t, d) for t, d in zip(ts, ds))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement