This wasn’t programming, it was linear algebra. Easy to solve. And Long_Integers to the rescue.
@jcmoyer: I looked at your solution, and yes, there is a unique solution, provided the two vectors don’t have the same direction. But that didn’t occur in my test inputs.
I fell into the trap of thinking it was an optimization problem. In the text you find “…the cheapest way to win the prize…” or “…the minimum tokens you would have to spend…”.
The presence of prime numbers led me to explore in the GCD / Chinese Theorem area…
Actually, if the matrix’ determinant is 0 (which never happens in my data), there would be either 0 or an inifinity of real solutions, some of them perhaps with positive integers. Then it would be tough (perhaps)…
Converting everything to floats, using substitution method to solve for the variables, convert them back to integer types, verified the solution was correct by plugging them in (in case the results rounded from float to integer), then adding the results if the solution was valid.
I did it first
(using type Real is digits System.Max_Digits;) but had to take the unaccuracy into account (the number added to the prize value is 10**14), so I had a pair of loops testing solutions around the values converted from floating-point, with an arbitrary tolerance - a dirty trick…
Later, I’ve discarded floating-points and used integer division. If there is a non-zero remainder for … / determinant, the solution is not an integer.
I started down that route, but since I went substitution method, I ended up starting out with some intermediate divisions and I wasn’t sure how I would handle those being non integer but the ultimate result being an integer, so I moved over to floats for safety. Later on I remembered a better form that only had the one division at the end (so I could have used integers after all), but by that point I was getting a little tired and didn’t want to refactor out the floats (I.E. I got lazy at the end).
I tried to solve this using Diophantine Equations, a topic from Number Theory. Basically, solve for the X’s and Y’s using an extended Euclidean Algorithm, then find the common solutions, then determine the cheapest one. That worked great for Part 1.
It doesn’t work for Part 2 because there are simply too many solutions to generate for each variable, let alone compute the intersection. It wasn’t clear to me that the problem is set up to have at monst one solution, so it never occurred to me to try basic linear algebra until I gave up and went to Reddit to look for hints.
I’m keeping my solution to Part 1, because (a) I like it, and (b) I like it a lot more than the linear algebra approach, which doesn’t work in the general case. I dislike puzzles that involve not only misdirection, but also unspoken secret knowledge.
If the two vectors are linearly independent, i.e. one is not a multiple of the other, then there is precisely one solution, which may be fractional. Only when both vectors are in the same direction, and that’s also the direction of the goal, you have to fall back on other strategies, but even then it’s not an unstructured system of diophantine equations. However, that condition doesn’t seem to have appeared in any of the online discussions.
That’s all quite true, which makes me wonder what on earth was wrong with my head this morning. I used to teach that not too long ago… Despite working for hours at solving them simultaneously, it never occurred to me to solve them… simultaneously.
That should have been easy, and I wrecked it. Thanks for re-explaining it. (I’d even read up on it, as I pointed out… and somehow still whiffed.)