Background
In the game of Nim, players alternate removing "stones" from "piles": on each turn, a player must remove between one and all stones from a single pile. The object of Nim is to take the last stone or, in the misere variant, to force your opponent to do so -- however, it turns out the strategies are nearly identical.
Nim makes a fun bar game. You can use matchsticks or coins for the "stones," and the "piles" are typically arranged in a line. Below is a classic setup with piles of 1, 3, 5, and 7:
If you've never played Nim before, you might try your hand at it before attempting this challenge. Here's a version called "Pearls Before Swine".
Strategy
Optimal strategy in Nim is tricky enough that most lay people lose consistently to an expert, but simple to describe with binary arithmetic.
Doing mental binary XOR operations, however, is tough, so luckily there is an equivalent way to visualize the correct strategy which is easier to implement in real time, even when drunk.
There are only three steps:
- Mentally group the "stones" in each line into subgroups whose sizes are powers of 2, starting with the largest possible size: 8, 4, 2, and 1 are sufficient for most games.
- Try to match each group with a twin in another line, so that every group has a pair.
- If this isn't possible, remove unpaired "stones" from a single line (this will always be possible - see the Wikipedia link for why) so that step 2. becomes possible.
Or, said another way: "Remove some stone(s) from a single pile such that if you then group the piles into powers of 2 all groups may be paired with a group in some other pile." With the caveat that you cannot break up larger powers of 2 into smaller ones -- eg, you cannot group a line with 8 stones into two groups of 4.
For example here's how you'd visualize the board above:
This board is perfectly balanced, so you'd want your opponent to move first.
The Challenge
Given a list of positive integers representing the size of Nim "piles", return a plain text visualization of the Nim board as seen by an expert.
What constitutes a valid visualization is best explained by example, but you must:
- Assign a distinct character to each "power-of-2 subgroup" and its pair (unpaired subgroups do not qualify), and use that character to represent the "stones" in both subgroup and pair.
- Represent any unpaired "stones" (ie, the ones an expert would remove when playing normal -- not misere -- Nim) using a hyphen:
-
.
There will be multiple ways to achieve a valid visualization, and all are valid. Let's work through some test cases:
Test Cases
Input: 1, 3, 5, 7
Possible Output 1:
A
BBA
CCCCD
CCCCBBD
You may optionally include spaces between the characters, as well as blank lines between the rows:
Possible Output 2:
A
B B A
C C C C D
C C C C B B D
Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The order and choice of characters can be whatever you like:
Possible Output 1:
G
E E
E E G
C C C C
C C C C F
B B B B D D
B B B B D D F
H H I - - - - -
A A A A A A A A I
A A A A A A A A H H
Unicode symbols are ok too:
Possible Output 2:
◎
◈ ◈
◈ ◈ ◎
△ △ △ △
△ △ △ △ ◉
◐ ◐ ◐ ◐ ◀ ◀
◐ ◐ ◐ ◐ ◀ ◀ ◉
▽ ▽ ◒ - - - - -
▥ ▥ ▥ ▥ ▥ ▥ ▥ ▥ ◒
▥ ▥ ▥ ▥ ▥ ▥ ▥ ▥ ▽ ▽
Input: 7
From the rules it follows that any "single pile" must be completely removed.
Possible Output 1:
-------
Possible Output 2:
- - - - - - -
Input: 5, 5
Possible Output:
A A A A B
A A A A B
Additional Rules
- This is code golf with standard rules. Shortest code wins.
- Input is flexible, and may be taken in whatever list-ish form is convenient to you.
- Output is flexible too, as the above examples illustrate. Most reasonable variations will be allowed. Ask if you're unsure about something.
["H","EE","EEH","CCCC","CCCCI","DDDDFF","DDDDFFI","AAAAAAAA","AAAAAAAA-","----------"]
\$\endgroup\$AAAABBBB
is actually invalid, andABB
is not -- but it makes the output less readable so I think just making decreasing within a line an explicit rule is best. \$\endgroup\$