Skip to content

Commit

Permalink
2024/13 non negative
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 13, 2024
1 parent 39d068a commit fcd96ae
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 2024/Day13/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_.
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_.
4 changes: 2 additions & 2 deletions 2024/Day13/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fcd96ae

Please sign in to comment.