diff options
Diffstat (limited to '2024/day13/solve2.py')
-rw-r--r-- | 2024/day13/solve2.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/2024/day13/solve2.py b/2024/day13/solve2.py new file mode 100644 index 0000000..f329f62 --- /dev/null +++ b/2024/day13/solve2.py @@ -0,0 +1,26 @@ +def solve_machine(a_x: int, a_y: int, b_x: int, b_y: int, prize_x: int, prize_y: int) -> int:
+ b_press_times = (a_y * prize_x - prize_y * a_x) / (a_y * b_x - b_y * a_x)
+ a_press_times = (prize_x - b_x * b_press_times) / a_x
+ cheapest_solution = a_press_times * 3 + b_press_times
+
+ if a_press_times.is_integer() and b_press_times.is_integer() and a_press_times >= 0 and b_press_times >= 0:
+ return int(cheapest_solution)
+
+ return 0
+
+
+with open("input") as f:
+ all_text = f.read()
+
+result = 0
+add_price_pos = 10000000000000
+for details in all_text.split("\n\n"):
+ lines = details.split("\n")
+ button_a = lines[0][10:].split(", ")
+ button_b = lines[1][10:].split(", ")
+ prize = lines[2][7:].split(", ")
+ result += solve_machine(int(button_a[0][2:]), int(button_a[1][2:]),
+ int(button_b[0][2:]), int(button_b[1][2:]),
+ int(prize[0][2:]) + add_price_pos, int(prize[1][2:]) + add_price_pos)
+
+print(result)
|