AOC/2024/day13/part1.py

44 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-12-13 05:09:33 -05:00
import math
import re
text: str
with open("input.txt") as f:
text = f.read()
total: int = 0
exp: str = r"Button A: X([+-]\d+), Y([+-]\d+)\nButton B: X([+-]\d+), Y([+-]\d+)\nPrize: X=(\d+). Y=(\d+)"
def resolv(Ax, Ay, Bx, By, goal_x, goal_y):
for press_b in range(101):
press_a = (goal_x - (press_b * Bx)) / Ax
2024-12-13 05:12:23 -05:00
if press_a * Ay + press_b * By != goal_y:
2024-12-13 05:09:33 -05:00
continue
if (press_a // 1 != press_a):
continue
if (press_a > 100):
continue
return press_a, press_b
def resolv_min(Ax, Ay, Bx, By, goal_x, goal_y):
tmp1 = resolv(Ax, Ay, Bx, By, goal_x, goal_y)
tmp2 = resolv(Bx, By, Ax, Ay, goal_x, goal_y)
if (tmp1 is None):
return tmp2
if (tmp2 is None):
return tmp1
press_a1, press_b1 = tmp1
press_b2, press_a2 = tmp2
if (press_a1 > press_a2):
return press_a2, press_b2
return press_a1, press_b1
for Ax, Ay, Bx, By, goal_x, goal_y in re.findall(exp, text):
Ax, Ay, Bx, By, goal_x, goal_y = int(Ax), int(Ay), int(Bx), int(By), int(goal_x), int(goal_y)
tmp = resolv_min(Ax, Ay, Bx, By, goal_x, goal_y)
if (tmp is None):
continue
press_a, press_b = tmp
total += press_a * 3 + press_b
print(total)