ILP Part 103 — 600

This is the one hundred and third part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we’re going to solve 600. We have a squared board of nine numbers. In each cell we have one digit. We can extend each cell with one digit in the front and/or one digit at the end. Our goal is to extend numbers in a way that each row and each column sums to one hundred.

The code:

We start with creating the board in lines 1-5. Next, some helper variables in 7-8.

We then make binary flags indicating whether we extend the number or not. That’s in lines 10-18.

Next, we declare variables for the actual extensions: prefix and suffix (lines 20-27).

Now comes the main part. We calculate the real number in each cell. We check all the conditions: if we extended the number both ways, or just added the prefix, or just added the suffix, or didn’t extend at all. We then do some simple math and get the value. Lines 29-46.

Now it’s easy. We just add the condition that rows (48-51) and columns (53-56) must be equal to one hundred.

Finally, we print the solution (58-68).

Output: