From 68351f1963d90c2a3226fa11de5de53955a41a20 Mon Sep 17 00:00:00 2001 From: "Fernando J. Iglesias Garcia" Date: Mon, 8 Jul 2024 23:06:09 +0200 Subject: [PATCH] Optimize day 24 part two with sympy reducing to smaller system. --- .../day/24/never_tell_me_the_odds_part_two.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/adventofcode/2023/day/24/never_tell_me_the_odds_part_two.py b/adventofcode/2023/day/24/never_tell_me_the_odds_part_two.py index 3e55f98..0d7f93a 100644 --- a/adventofcode/2023/day/24/never_tell_me_the_odds_part_two.py +++ b/adventofcode/2023/day/24/never_tell_me_the_odds_part_two.py @@ -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])