This post contains APL symbols! they may not render correctly on your computer! APL is a very interesting programming language! learn APL here
I recently got into orbits, a game where we are given a 6 by 6 square grid of cells. Some of the cells are empty, and we can place between 1 and 3 dots in these empty cells. Some of the cells have a number that is the sum of dots in the 8 cells adjacent to it. Using these sums we have to deduce the correct distribution of dots. whenever I see a game involving a square grid, especially one which such clear and tight constraints, my mind immediately wonders about representing it in APL, and then goes on to wonder about the specific mathematical properties of the games and algorithmic solving techniques etc.
The purpose of this post is to log all the wonderings and APL snippets so that I can continue having fun with them later.
Generating board
I sat down a few weeks ago and wrote a first pass at recreating the board generation in APL. I have since realized this generator isn’t sufficiently constrained to produce unambiguous boards (we consider a board with more than one possible solution ambiguous). I hadn’t taken the time to determine if ambiguous boards were possible or not. Ambiguous boards are possible if there is an empty cell with no adjacent non-empty cells, or we can create one along the lines of a 3x3 grid with a 9-count in the middle.
I hypothesize that a more general case of the 3x3 grid is a grid where every non-empty cell has a value of n+1 where n is the number of adjacent empty cells, that way the extra 1 has 8 possible placements. There is probably a more general and weaker example of ambiguous boards that I can get to with more than the 20s of thought I’m giving right now.
Here is that original board generator: {(0=⍵)×({(+/,⍵)}⌺3 3)⍵}{?⍵ ⍵⍴ 3} n it creates an nxn grid then rolls a 4 sided die to randomize each cell with 0-3 dots, each cell with 0 dots is then replaced with a count and all the dotted cells are zeroed out to create the game board. This board code can potentially cluster too many dotted cells together and create an ambiguous board.
solving methods
While playing last night I realized that we can turn any board into linear algebra. say we have a board like
3 0 0
0 7 5
2 3 0
Where 0 represents an empty cell. Then, we can represent it as a system of linear equations:
3 a b
c 7 5
2 3 d
a + c = 3
a + b + c + d = 7
a + b + d = 5
c = 2
c + d = 3
c = 2
a + c - c = 3-2
a = 1
c + d - c = 3-2
d = 1
a + b + d - a - c - d = 7 - 1 - 2 - 1
b = 3
which we can then represent as an augmented matrix:
a b c d =
1 0 1 0 3
1 1 1 1 7
1 1 0 1 5
0 0 1 0 2
0 0 1 1 3
We can then convert it to row echelon form to get our answer for each empty cell
1 0 0 0 1
0 1 0 0 3
0 0 0 0 0
0 0 1 0 2
0 0 0 1 1
Where the programming comes in is converting our board matrix to the augmented matrix, solving that matrix, and then back filling the board with the correct answer.
I have trouble with APL sometimes, as much as I admire it, translating something I think of very procedurally to a series of filters can be a real challenge for me. So I haven’t figured it out yet, but a good friend wrote an APL program to do it and made some recommendations which I will study and come back to this with.
further extensions
I think it would be really interesting to try to generalize this game to any maximum or minimum dot quantity, and any number of adjacent cells, or any shape of graph, again I say this without putting more than a second’s thought toward it. I suspect it would end up a graph coloring problem, but, proving that it is, in fact exactly as hard as a graph coloring, would still be a fun exercise that I haven’t had to do since college.
I also thought about some sort of iterative flooding approach that could repeat until a stable state. I think it could be interesting to program and I’d learn something from it. Thinking about it, it might be interesting to use a transport algorithm.
There are other grid based games I have wanted to do this for in the past so this would be good practice for that too.