From fcd96aed9f628f6351f6f2ff7574c1820f0c51bf Mon Sep 17 00:00:00 2001 From: encse Date: Fri, 13 Dec 2024 10:16:11 +0100 Subject: [PATCH] 2024/13 non negative --- 2024/Day13/README.md | 2 +- 2024/Day13/Solution.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/2024/Day13/README.md b/2024/Day13/README.md index eea3f0b3..41aa01e8 100644 --- a/2024/Day13/README.md +++ b/2024/Day13/README.md @@ -5,4 +5,4 @@ Fortunately, it looks like the resort has a new [arcade](https://en.wikipedia.or Read the [full puzzle](https://adventofcode.com/2024/day/13). -We got a math problem today. The movements triggered by buttons A, B and the target position P form a linear equation that we can solve for the two unknowns: the number of button presses. Using math terms, if _i_ and _j_ marks the number of button presses, we are to solve `A * i + B * j = P`. This is simple enough to do using [Cramer's rule](https://en.wikipedia.org/wiki/Cramer%27s_rule), especially with two dimensional vectors. In this case the determinats can be computed with a simple cross product. We should not forget about the special cases: A and B can be parallel (didn't occur in my input), and the solution needs to be integer for _i_ and _j_. \ No newline at end of file +We got a math problem today. The movements triggered by buttons A, B and the target position P form a linear equation that we can solve for the two unknowns: the number of button presses. Using math terms, if _i_ and _j_ marks the number of button presses, we are to solve `A * i + B * j = P`. This is simple enough to do using [Cramer's rule](https://en.wikipedia.org/wiki/Cramer%27s_rule), especially with two dimensional vectors. In this case the determinats can be computed with a simple cross product. We should not forget about the special cases: A and B can be parallel (didn't occur in my input), and the solution needs to be non negative integer for _i_ and _j_. \ No newline at end of file diff --git a/2024/Day13/Solution.cs b/2024/Day13/Solution.cs index accc47b6..1278ba48 100644 --- a/2024/Day13/Solution.cs +++ b/2024/Day13/Solution.cs @@ -21,8 +21,8 @@ long GetPrize(Machine m) { var i = Det(p, b) / Det(a, b); var j = Det(a, p) / Det(a, b); - // return the prize when an _integer_ solution is found - if (a.x * i + b.x * j == p.x && a.y * i + b.y * j == p.y) { + // return the prize when a non negative _integer_ solution is found + if (i >= 0 && j >= 0 && a.x * i + b.x * j == p.x && a.y * i + b.y * j == p.y) { return 3 * i + j; } else { return 0;