diff --git a/2024/13/index.html b/2024/13/index.html index e91c8aff..569b057e 100644 --- a/2024/13/index.html +++ b/2024/13/index.html @@ -277,7 +277,7 @@
Next up: the lobby of a resort on a tropical island. The Historians take a moment to admire the hexagonal floor tiles before spreading out.
Fortunately, it looks like the resort has a new arcade! Maybe you can win some prizes from the claw machines?
Read the full puzzle.
-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, 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, 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.
namespace AdventOfCode.Y2024.Day13;
@@ -302,8 +302,8 @@ Claw Contraption
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;