Skip to content

Commit fcd96ae

Browse files
committed
2024/13 non negative
1 parent 39d068a commit fcd96ae

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

2024/Day13/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Fortunately, it looks like the resort has a new [arcade](https://en.wikipedia.or
55

66
Read the [full puzzle](https://adventofcode.com/2024/day/13).
77

8-
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_.
8+
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_.

2024/Day13/Solution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ long GetPrize(Machine m) {
2121
var i = Det(p, b) / Det(a, b);
2222
var j = Det(a, p) / Det(a, b);
2323

24-
// return the prize when an _integer_ solution is found
25-
if (a.x * i + b.x * j == p.x && a.y * i + b.y * j == p.y) {
24+
// return the prize when a non negative _integer_ solution is found
25+
if (i >= 0 && j >= 0 && a.x * i + b.x * j == p.x && a.y * i + b.y * j == p.y) {
2626
return 3 * i + j;
2727
} else {
2828
return 0;

0 commit comments

Comments
 (0)