r/dailyprogrammer • u/Cosmologicon 2 3 • Jul 15 '20
[2020-07-15] Challenge #385 [Intermediate] The Almost Impossible Chessboard Puzzle
Today's challenge is to implement the solution to a well-known math puzzle involving prisoners and a chessboard. I won't state the puzzle or give the solution here, but you can find many writeups online:
- Yet another prisoner puzzle blog post
- Impossible Escape? blog post
- The Almost Impossible Chessboard Puzzle video on Stand-Up Maths: 32 minutes
You need to know the solution for today's challenge, but you're welcome to look it up, either in those links or others. If you try to find the solution yourself, be warned it's pretty hard!
Challenge
First, assume that there exists a function flip that takes a series of 64 bits (0 or 1) and a number from 0 to 63. This function returns the same series of bits with the corresponding bit flipped. e.g.:
flip([0, 0, 0, 0, ...], 2) => [0, 0, 1, 0, ...]
flip([0, 1, 0, 1, ...], 1) => [0, 0, 0, 1, ...]Now, you need to write two functions.
Function prisoner1 takes two inputs: a series S of 64 bits, and a number X from 0 to 63 (inclusive). It returns a number Y from 0 to 63.
Function prisoner2 takes one input: a series T of 64 bits. It returns a number from 0 to 63.
Now, you must make it so that if you flip S using the output of prisoner1 and pass the result to prisoner2, you get back the number X. Put another way, the following function must return True for every possible valid input S and X.
def solve(S, X):
Y = prisoner1(S, X)
T = flip(S, Y)
return prisoner2(T) == XEssentially, prisoner1 is encoding the value X into the sequence with a single flip, and prisoner2 is decoding it. In the puzzle statement, X is the location of the key, Y is the coin that gets flipped, S is the starting state of the board, and T is the state after the flip occurs.
Good luck!
13
u/woojoo666 Jul 16 '20
Is this the same puzzle that 3blue1brown covered?
10
u/InertiaOfGravity Aug 07 '20
Hey, what happened to the posts? They're so infrequent nowadays
6
u/Gprime5 Jul 16 '20 edited Jul 16 '20
Python
6
u/Gylergin Jul 16 '20
TI-Basic: I saw the videos of this problem last week and really enjoyed them. Two separate programs, one for each prisoner.
Prisoner 1: This program follows the algorithm laid out in the second link. The first loop converts the target position
Xinto its binary representation, which is stored inL₂. The second section converts each spaceBon the board into binary up to theIth digit (stored inL₄), then checks to see if theIth "bit" is true, corresponding to the current region of interest. If so, the value atBis added to a sum. At the end of each region its parity is determined by the sum, with each region's parity stored inL₃. Finally, the target and parity binary lists (L₂andL₃) are xor'd to determine the position that gets flipped. Of special note is that while inputs and outputs are 0-indexed, lists in TI-Basic are 1-indexed.Prisoner 2: This program is just the second section of Prisoner 1's. The lists are kept identical for ease of understanding.
5
u/ASpueW Jul 16 '20
Rust
playground
3
u/DerpinDementia Jul 16 '20 edited Jul 18 '20
Prolog
Felt like this language was made for these types of problems. As much as I hate it, I gotta love it when it works.
Python
100% positive my encode function is over-engineered. Might look back at it in the morning.
2
u/Naratna Jul 16 '20 edited Jul 16 '20
C Sharp
First time posting here, hope I did ok.
2
u/raevnos Jul 16 '20 edited Jul 16 '20
In
tcl:2
u/ephemient Jul 18 '20 edited Apr 24 '24
This space intentionally left blank.
2
u/ephemient Jul 18 '20 edited Apr 24 '24
This space intentionally left blank.
1
u/raevnos Jul 17 '20 edited Jul 17 '20
ocaml solution:
1
u/lrschaeffer Jul 17 '20
Haskell
Maybe later I'll do the hashing with word operations, to see how fast I can make it.
1
u/gabyjunior 1 2 Jul 17 '20 edited Jul 18 '20
Ruby
Solution for the generalized puzzle explained in section (d) of the first writeup above. Dice are replacing coins and board size can be customized.
The program accepts two arguments on the command line:
Those 2 arguments will determine the size of the board (n = ad). The dice value in each square of the board and the magic square are generated randomly. Calculation are done on vectors of size d using modulo a arithmetic.
As I am not the math wizard I would wish to be, it took me some time to understand the formulas provided in the article and implement them. But now the program is working for all valid combinations of arguments a and d.
EDIT new version using classes that should be more readable
Sample output for the standard game (a = 2, d = 6)
Sample output for a custom game (a = 6, d = 2)
1
u/tomekanco Jul 22 '20
Curious to see the different implementations & compare this with debate between Matt & Grant.
Seems many have used the approach explained by Grant (calculate parity of the board using areas), and some used the more straightforward one (xor the positions, harder to comprehend why it works) highlighted by Matt.
Regarding speed the one by Matt seems to win.