Skip to content

Commit

Permalink
Optimize day 24 part two with sympy reducing to smaller system.
Browse files Browse the repository at this point in the history
  • Loading branch information
iglesias committed Jul 8, 2024
1 parent 275a302 commit 68351f1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions adventofcode/2023/day/24/never_tell_me_the_odds_part_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ def __init__(self, x0, y0, z0, vx, vy, vz):
vx, vy, vz = [int(x) for x in tail.strip().split(', ')]
hailstones.append(Hailstone(x0, y0, z0, vx, vy, vz))

n = len(hailstones)
symbols_str = 'x y z vx vy vz'
num_symbols = len(symbols_str.split())
eqs = []
for i in range(n):
h = hailstones[i]
x, y, z, vx, vy, vz = sympy.symbols('x y z vx vy vz')
eqs.append((h.vy - vy)*(x - h.x0) - (h.vx - vx)*(y - h.y0))
eqs.append((h.vz - vz)*(y - h.y0) - (h.vy - vy)*(z - h.z0))
i = -1
for n in range(3, len(hailstones)):
for i in range(i + 1, n):
h = hailstones[i]
x, y, z, vx, vy, vz = sympy.symbols(symbols_str)
eqs.append((h.vy - vy)*(x - h.x0) - (h.vx - vx)*(y - h.y0))
eqs.append((h.vz - vz)*(y - h.y0) - (h.vy - vy)*(z - h.z0))
i = n - 1
solution = sympy.solve(eqs)
if len(solution) == 1 and len(solution[0].keys()) == num_symbols:
break

solution = sympy.solve(eqs)

assert(len(solution) == 1)
print(solution)
solution = solution[0]
print('Part Two:', solution[x] + solution[y] + solution[z])

0 comments on commit 68351f1

Please sign in to comment.