Background
Conway's Soldiers is a version of peg solitaire played on an infinite checkerboard. The board is initially full of pegs below an infinite horizontal line, and empty above it. Following the ordinary peg solitaire rules (move a peg by jumping over another one horizontally or vertically, removing the one that was jumped over), the objective is to move a peg as far above the horizontal line as possible.
Wikipedia page has the solutions for 1 to 4 units above the line: (A and B denote two possible alternatives.)
In ASCII notation (using alternative B):
X
X .
X . .
_X_ __._ __.__ ____.____
O OOO OOOOO OOOOOOOOO
O O OOO OOOO
OOOOO
OO
Conway proved that it is impossible to reach 5 units above the line with finite number of moves. To prove it, he assigned a value to each peg: if a peg is \$n\$ units away from the target position in terms of Manhattan distance, it is assigned the value of \$\varphi^n\$, where
$$ \varphi = \frac{\sqrt5 - 1}{2} $$
(The value is the golden ratio minus 1.)
This value was carefully chosen to ensure that every possible move keeps the total value constant when a move is towards X
, and decreasing when a move is away from it. Also, the final state must have a peg precisely at the target position, giving the value of \$\varphi^0 = 1\$, so the target position is unreachable if the initial configuration has the value sum less than 1.
For the target position at 5 units above the line, the configuration looks like this:
X
.
.
.
_____._____
OOOCBABCOOO
OOOOCBCOOOO
OOOOOCOOOOO
...
The peg at the position A
is given \$\varphi^5\$, the ones at B
are \$\varphi^6\$ each, and so on. Then he showed that the sum for the infinite number of pegs is exactly 1, and therefore the value sum of any finite subset is less than 1, concluding the proof of non-reachability.
Task
Now, let's apply this measure to an arbitrary configuration, not just for the original problem, e.g. the pegs may surround the target position:
OOOOO
O...O
O.X.O
O...O
OOOOO
Given such a configuration, calculate Conway's measure on it and output truthy if the measure is at least 1, falsey otherwise. (Note that the truthy output does not guarantee that the target is actually reachable, while the falsey output does say that the target is too far away from the pegs to reach it.)
The calculated measure should be within 1e-6
margin. A program that produces wrong answers when the computed one falls within \$\pm10^{-6}\$ from the true measure is acceptable. You can use (sqrt(5)-1)/2
or 0.618034
, but not 0.61803
or 0.61804
.
You can choose any three distinct symbols (characters, numbers, or any other kind of values) to indicate a peg, an empty space, and the target position respectively. You can take the grid as a matrix, a list of strings (or lists of symbols), or a single string (or a list of symbols) with a delimiter of your choice. You can assume that the input has exactly one target position, and it is not already occupied by a peg.
Test cases
In the test cases below, O
is a peg, X
is the target position, and .
is a blank.
True
measure = 1 (0.61803 will fail all of the measure=1 cases)
OOX
--------------
measure = 1
OO.X
.O..
.O..
--------------
measure = 1
..X..
.....
.....
OOOOO
..OOO
--------------
measure = 1
....X....
.........
.........
.........
OOOOOOOOO
..OOOO...
.OOOOO...
...OO....
--------------
measure = 4
OOOOO
O...O
O.X.O
O...O
OOOOO
--------------
measure ~ 1.00813
X....OOOO
....OOOO.
...OOOO..
..OOOO...
.OOOO....
False
measure ~ 0.618
OO.X
--------------
measure ~ 0.999975 (0.61804 will fail)
OOOOOOOOOOOOOOOOOOOOOO.X
--------------
measure ~ 0.9868
X....OOO
....OOOO
...OOOO.
..OOOO..
.OOOO...
--------------
measure = 0
.....
.....
..X..
.....
.....
/
-separated string for the grid, such asOO.X/.O../.O..
? \$\endgroup\$