A stripped down implementation of Wordle for the bitwise challenge.
In the game of Wordle, the player has 6 chances to guess a 5 letter word. For the Bitwise Challenge, the state of the game must be fit into 64 bits.
# start a new game
python play.py
# resume a game from a given state
python play.py 3d09c
The most important optimization here comes from the fact that all valid guesses must come from the word list. Also, the answer can come from a reduced subset of the valid guess list. This allows us to encode guesses as in index into a list, rather than directly encoding each character. The encoding length of the answer can be smaller than the encoding of each guess.
If 10 bits encode each guess, then only 4 bits are left over to encode the answer. With only 16 unique answers, it's not really very replayable. If we use only 9 bits for each guess, then only 512 words can be valid, which is too restrictive.
This alone is not enough.
After the player has submitted the final guess, there are really only two possibilities. Either they have won or lost. If the final frame does not display their guesses, we don't actually have to store what it was.
We can use two bits to encode whether or not the player has made their final guess, and if they were right or not.
Under this scheme, we only need to encode 5 answers with 11 bits each. The answer can be 7 bits. With two bits to encode the final state that makes a total of 64.
The final bit layout then is 1 (winner) + 1 (game over) + 5 * 11 (guesses) + 7 (answer) = 64 bits
Unfortunately, this state layout only gives 128 unique answers and is restricted to 2047 valid words for guessing. In practice this is "playable" but I often run into words that I expect to be valid which are not.
select_words.py
can be used to regenerate the list of valid guesses and answers in order to increase replayability.
I kind of gloss over the fact that the players buffered input (before inputting a guess) could be considered "part of the state". Interpreting the rules that way makes the challenge totally impossible though (for this game), so I'm ignoring it.
Currently, the feedback given to the player (yellow and green squares) does not follow the exact same algorithm as the original game. In the original, if the guessed word contains a repeated letter, then only one will be colored if there is only one of that letter in the solution. This is slightly better feedback but more complicated to implement. It has no impact on the size of the game state though, so I'm not going to bother.
While it's not possible to store the full history of a game (all 6 guesses), it should be possible to tell the player how many guesses it took for them to get it right by parsing the state a bit differently when the 'over' bit is set.
I would love to give better feedback when the player inputs an invalid word. It may be possible to set the 'win' bit when the 'over' bit is not set to indicate that the last guess was not a valid word.
Python is quite awkward for single bit manipulations. The program could probably be made smaller and easier to read in C or something.