From 345a5bfccb5b62b3c3554586622697a586431342 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Fri, 13 Dec 2024 11:09:33 +0100 Subject: [PATCH] wip 2024/day13/part1 --- 2024/day13/part1.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 2024/day13/part1.py diff --git a/2024/day13/part1.py b/2024/day13/part1.py new file mode 100644 index 0000000..a49f2fd --- /dev/null +++ b/2024/day13/part1.py @@ -0,0 +1,44 @@ +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 + if press_a * Ay + press_b * By > goal_y: + 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) \ No newline at end of file