From 26d6292256273cce3d1fb4f4960b087f94fdb9de Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Mon, 10 Mar 2025 16:36:58 -0500 Subject: [PATCH] solved day13 part 2 --- 2024/day13/main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/2024/day13/main.py b/2024/day13/main.py index a25588f..96c0be2 100644 --- a/2024/day13/main.py +++ b/2024/day13/main.py @@ -18,6 +18,8 @@ for i in range(0, len(data), 4): # Very simple niave solution for part 1 +print('<', '-' * 10, ' Part 1 ', '-' * 10, '>') + total_cost = 0 for machine in machines: ax, ay = machine[0] @@ -40,12 +42,24 @@ for machine in machines: cost = (smallest_a * BUTTON_A_COST) + (smallest_b * BUTTON_B_COST) total_cost += cost - print('Presses: ', smallest_a, smallest_b) - print('Minimum tokens: ', cost) - print('Total cost: ', total_cost) +print('<', '-' * 10, ' Part 2 ', '-' * 10, '>') +# Part 2 (unfortunately lifted this formula from A.P. (https://github.com/CodingAP) ) +total_cost = 0 +for machine in machines: + ax, ay = machine[0] + bx, by = machine[1] + x, y = [n + 10000000000000 for n in machine[2]] + b: float = ((ax * y) - (ay * x)) / ((ax * by) - (ay * bx)) + a: float = (x - (bx * b)) / ax + + if b.is_integer() and a.is_integer(): + cost = (a * BUTTON_A_COST) + (b * BUTTON_B_COST) + total_cost += cost + +print('Total cost: ', int(total_cost))