Sunday Times Teaser 3167 – Binary Primes
by Mark Valentine
Published Sunday June 04 2023 (link)
Grace and Helen play a turn-based card game. Each player is given two cards with a zero printed on them, and unlimited cards with a one printed on them. Before the game starts a 1 is placed on the table. Sitting on the same side of the table, a turn then involves placing a card to the left of the table cards, such that the value shown in binary remains prime or one (allowing leading zeros). The winner is the player who is last able to play a card, earning points equal to the value on the table at that time.
Being expert logicians, they play the best possible cards to maximise their points or minimise their opponent’s points. Grace goes first.
Who wins and with what cards on the table from left to right?
Brilliant!
in line 12
is redundant because cv can never be 0.
in line 23
is also redundant.
and line 16
Hi John,
These (and other) conditions are there to ensure that the subroutine will work
with other inputs, For example, calling it with no cards on the table at the start.
I found this version of returning positive/negative points a bit more intuitive.
I have noticed that I can simplify mine a bit by altering what I store (which I will do shortly).
Without recursion.
First all possible games are determined and then the whole list of games is collapsed to only one entry (working our way back from games with the most moves to games with the fewest moves)
Line 30 must be:
This code prints out all possible outcomes without indicating which is the solution to the teaser. The ‘0’ card count switch is inspired by Brian’s code.
Hi John, why is sorting necessary? I find it difficult to see order in the output.
The contents of games and list(zerone()) are the same.
Hi frits, The purpose of sorting is to place games with the same initial plays next to each other so that the tree of plays is easier to visualise or draw manually.
Frits, you are absolutely right, there is no need to sort because the result of zerone() is already in the desired order. Thanks. Here is the slimmed down version…
Starting from a list of prime numbers. I can’t deduce an upper limit for the highest prime number.
(in my previous program I conclude who wins based on the printed entries).
For interest here is a version based on the bottom up pruning of a binary tree. To show the pruning process, set ‘print_tree’ to True.