From 3432d81941551f92653599fd5d898e2c44666b37 Mon Sep 17 00:00:00 2001 From: felixm Date: Fri, 13 Dec 2024 00:58:31 -0500 Subject: [PATCH] Solve 2024 day 13 --- 2024/d13.py | 33 +++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 2024/d13.py diff --git a/2024/d13.py b/2024/d13.py new file mode 100644 index 0000000..2ea45ab --- /dev/null +++ b/2024/d13.py @@ -0,0 +1,33 @@ +from lib import get_data +from lib import ints + +data = get_data(__file__) +ps = data.split("\n\n") + +c1, c2 = 0, 0 +for p in ps: + lines = p.splitlines() + ax, ay = ints(lines[0]) + bx, by = ints(lines[1]) + px, py = ints(lines[2]) + px2 = px + 10000000000000 + py2 = py + 10000000000000 + + for a in range(0, 101): + for b in range(0, 101): + if a * ax + b * bx == px and a * ay + b * by == py: + c1 += a * 3 + b + + D = ax * by - bx * ay + if abs(D) > 1e-10: + a_ = px2 * by - bx * py2 + b_ = ax * py2 - px2 * ay + if a_ % D == 0 and b_ % D == 0: + a = (px2 * by - bx * py2) // D + b = (ax * py2 - px2 * ay) // D + + if a >= 0 and b >= 0: + c2 += a * 3 + b + +print(c1) +print(c2) diff --git a/README.md b/README.md index 536e504..bb6719a 100644 --- a/README.md +++ b/README.md @@ -303,5 +303,6 @@ and focus. Of course, learning more algorithms and techniques helps. - Day 10: `00:19:47 2976 0 00:20:47 2244 0` - Day 11: `00:05:13 642 0 00:33:07 2673 0` - Day 12: `00:30:30 3540 0 11:41:33 16212 0` -- Day 13: +- Day 13: `00:05:35 225 0 00:55:48 2544 0` +- Day 14: