#!/usr/bin/env python # coding: utf-8 # # The Devil and the Coin Flip Game # # If the Devil ever challenges me to a [fiddle contest](https://en.wikipedia.org/wiki/The_Devil_Went_Down_to_Georgia), I'm going down. But here is a contest where I'd have a better chance: # # > *You're playing a game with the Devil, with your soul at stake. You're sitting at a circular table which has 4 coins, arranged in a diamond, at the 12, 3, 6, and 9 o'clock positions. You are blindfolded, and can never see the coins or the table.* # # > *Your goal is to get all 4 coins showing heads, by telling the devil the position(s) of some coins to flip. We call this a "move" on your part. The Devil must faithfully perform the requested flips, but may first sneakily rotate the table any number of quarter-turns, so that the coins are in different positions. You keep making moves, and the Devil keeps rotating and flipping, until all 4 coins show heads.* # # > *Example: You tell the Devil to flip the 12 o'clock and 6 o'clock positions. The devil might rotate the table a quarter turn clockwiae, and then flip the coins that have moved into the 12 o'clock and 6 o'clock positions (which were formerly at 3 o'clock and 9 o'clock). Or the Devil could have made any other rotation before flipping.* # # > *What is a shortest sequence of moves that is **guaranteed** to win, no matter what the initial state of the coins, and no matter what rotations the Devil applies?* # # # Analysis # # - We're looking for a "shortest sequence of moves" that reaches a goal. That's a [shortest path search problem](https://en.wikipedia.org/wiki/Shortest_path_problem). I've done that before. # - Since the Devil gets to make moves too, you might think that this is a [minimax](https://en.wikipedia.org/wiki/Minimax) problem: that we should choose the move that leads to the shortest path, given that the Devil has the option of making moves that lead to the longest path. # - But minimax only works when you know what moves the opponent is making: he did *that*, so I'll do *this*. In this problem the player is blinfolded; that makes it a [partially observable problem](https://en.wikipedia.org/wiki/Partially_observable_system) (in this case, not observable at all, but we still say "partially"). # - In such problems, we don't know for sure the true state of the world before or after any move. So we should represent what *is* known: *the set of states that we believe to be possible*. We call this a *belief state*. At the start of the game, each of the four coins could be either heads or tails, so that's 24 = 16 possibilities in the initial belief state: # {HHHH, HHHT, HHTH, HHTT, HTHH, HTHT, HTTH, HTTT, # THHH, THHT, THTH, THTT, TTHH, TTHT, TTTH, TTTT} # - So we have a single-agent shortest path search in the space of belief states (not the space of physical states of the coins). We search for a path from the inital belief state to the goal belief state, which is `{HHHH}`. # - A move updates the belief state as follows: for every four-coin sequence in the current belief state, rotate it in every possible way, and then flip the coins specified by the position(s) in the move. Collect all these results together to form the new belief state. The search space is small (just 216 possible belief states), so run time will be fast. # - I'll Keep It Simple, and not worry about rotational symmetry (although we'll come back to that later). # # # # Basic Data Structures and Functions # # What data structures will I be dealing with? # # - `Coins`: a *coin sequence* (four coins, in order, on the table) is represented as a `str` of four characters, such as `'HTTT'`. # - `Belief`: a *belief state* is a `frozenset` of `Coins` (frozen so it can be hashed), like `{'HHHT', 'TTTH'}`. # - `Position`: an integer index into the coin sequence; position `0` selects the `H` in `'HTTT'` # and corresponds to the 12 o'clock position; position 1 corresponds to 3 o'clock, and so on. # - `Move`: a set of positions to flip, such as `{0, 2}`. # - `Strategy`: an ordered list of moves. A # blindfolded player has no feedback, there thus no decision points in the strategy. # In[1]: from collections import deque from itertools import product, combinations, chain import random Coins = ''.join # A coin sequence; a str: 'HHHT'. Belief = frozenset # A set of possible coin sequences: {'HHHT', 'TTTH'}. Position = int # An index into a coin sequence. Move = set # A set of positions to flip: {0, 2} Strategy = list # A list of Moves: [{0, 1, 2, 3}, {0, 2}, ...] # What basic functions do I need to manipulate these data structures? # # - `all_moves()`: returns a list of every possible move a player can make. # - `all_coins()`: returns a belief state consisting of the set of all 16 possible coin sequences: `{'HHHH', 'HHHT', ...}`. # - `rotations(coins)`: returns a belief set of all 4 rotations of the coin sequence. # - `flip(coins, move)`: flips the specified positions within the coin sequence. # (But leave `'HHHH'` alone, because it ends the game.) # - `update(belief, move)`: returns an updated belief state: all the coin sequences that could result from any rotation followed by the specified flips. # # In[2]: def all_moves() -> [Move]: "List of all possible moves." return [set(m) for m in powerset(range(4))] def all_coins() -> Belief: "The belief set consisting of all possible coin sequences." return Belief(map(Coins, product('HT', repeat=4))) def rotations(coins) -> {Coins}: "A set of all possible rotations of a coin sequence." return {coins[r:] + coins[:r] for r in range(4)} def flip(coins, move) -> Coins: "Flip the coins in the positions specified by the move (but leave 'HHHH' alone)." if 'T' not in coins: return coins # Don't flip 'HHHH' coins = list(coins) # Need a mutable sequence for i in move: coins[i] = ('H' if coins[i] == 'T' else 'T') return Coins(coins) def update(belief, move) -> Belief: "Update belief: consider all possible rotations, then flip." return Belief(flip(c, move) for coins in belief for c in rotations(coins)) flatten = chain.from_iterable def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" # https://docs.python.org/3/library/itertools.html#itertools-recipes s = list(iterable) return flatten(combinations(s, r) for r in range(len(s) + 1)) # Let's try out these functions: # In[3]: all_moves() # In[4]: all_coins() # In[5]: rotations('HHHT') # In[6]: flip('HHHT', {0, 2}) # There are 16 coin sequences in the `all_coins` belief state. If we update this belief state by flipping all 4 positions, we should get a new belief state where we have eliminated the possibility of 4 tails, leaving 15 possible coin sequences: # In[7]: update(all_coins(), {0, 1, 2, 3}) # In[8]: list(powerset([1,2,3])) # Everything looks good so far. # # Search for a Winning Strategy # # The generic function `search` does a breadth-first search starting # from a `start` state, looking for a `goal` state, considering possible `actions` at each turn, # and computing the `result` of each action (`result` is a function such that `result(state, action)` returns the new state that results from executing the action in the current state). `search` works by keeping a `queue` of unexplored possibilities, where each entry in the queue is a pair consisting of a *strategy* (sequence of moves) and a *state* that that strategy leads to. We also keep track of a set of `explored` states, so that we don't repeat ourselves. I've defined this function (or one just like it) multiple times before, for use in different search problems. # In[9]: def search(start, goal, actions, result) -> Strategy: "Breadth-first search from start state to goal; return strategy to get to goal." explored = set() queue = deque([(Strategy(), start)]) while queue: (strategy, state) = queue.popleft() if state == goal: return strategy for action in actions: state2 = result(state, action) if state2 not in explored: queue.append((strategy + [action], state2)) explored.add(state2) # Note that `search` doesn't know anything about belief states—it is designed to work on plain-old physical states of the world. But amazingly, we can still use it to search over belief states: it just works, as long as we properly specify the start state, the goal state, and the means of moving between states. # # The `coin_search` function calls `search` to solve our specific problem: # In[10]: def coin_search() -> Strategy: "Use `search` to solve the Coin Flip problem." return search(start=all_coins(), goal={'HHHH'}, actions=all_moves(), result=update) coin_search() # That's a 15-move strategy that is guaranteed to lead to a win. **Stop here** if all you want is the answer to the puzzle. # # ---- # # Or you can continue on ... # # # Verifying the Winning Strategy # # I don't have a proof, but I have some evidence that this strategy works: # - Exploring with paper and pencil, it looks good. # - A colleague did the puzzle and got the same answer. # - It passes the `winning` test below. # # The call `winning(strategy, k)` plays the strategy *k* times against a Devil that chooses starting positions and rotations at random. Note this is dealing with concrete, individual states of the world, like `HTHH`, not belief states. If `winning` returns `False`, then the strategy is *definitely* flawed. If it returns `True`, then the strategy is *probably* good—it won *k* times in a row—but that does not prove it will win every time (and either way `winning` makes no claims about being a *shortest* strategy). # In[11]: def winning(strategy, k=100000) -> bool: "Is this a winning strategy? A probabilistic algorithm." return all(play(strategy) == 'HHHH' for _ in range(k)) def play(strategy, starting_coins=list(all_coins())) -> Coins: "Play strategy for one game against a random Devil; return final state of coins." coins = random.choice(starting_coins) for move in strategy: if 'T' not in coins: return coins coins = random.choice(list(rotations(coins))) coins = flip(coins, move) return coins winning(strategy=coin_search()) # # # # Canonical Coin Sequences # # Consider these coin sequences: `{'HHHT', 'HHTH', 'HTHH', 'THHH'}`. In a sense, these are all the same: they all denote the same sequence of coins with the table rotated to different degrees. Since the devil is free to rotate the table any amount at any time, we could be justified in treating all four of these as equivalent, and collapsing them into one representative member. I will redefine `Coins` so that is stil takes an iterable of `'H'` or `'T'` characters and joins them into a `str`, but I will make it consider all possible rotations of the string and (arbitraily) choose the one that comes first in alphabetical order (which would be `'HHHT'` for the four coin sequences mentioned here). # In[12]: def Coins(coins) -> str: "The canonical representation (after rotation) of the 'H'/'T' sequence." return min(rotations(''.join(coins))) # In[13]: assert Coins('HHHT') == Coins('HHTH') == Coins('HTHH') == Coins('THHH') == 'HHHT' # With `Coins` redefined, the result of `all_coins()` is different: # In[14]: all_coins() # The starting belief set is down from 16 to 6, namely 4 heads, 3 heads, 2 adjacent heads, 2 opposite heads, 1 head, and no heads, respectively. # # Let's make sure we didn't break anything: # In[15]: coin_search() # # Winning Strategies for *N* Coins # # What if there are 3 coins on the table arranged in a triangle? Or 6 coins in a hexagon? To answer that, I'll generalize all the functions that have a "4" in them: `all_moves, all_coins`, `rotations` and `coin_search`. # # Computing `all_coins(N)` is easy; just take the product, and canonicalize each one with the new definition of `Coins`. For `all_moves(N)` I want canonicalized moves: the moves `{0}` and `{1}` should be considered the same, since they both say "flip one coin." To get that, look at the canonicalized set of `all_coins(N)`, and for each one pull out the set of positions that have an `H` in them and flip those positions. (The positions with a `T` should be symmetric, so we don't need them as well.) # In[16]: def all_moves(N=4) -> [Move]: "All canonical moves for a sequence of N coins." return [set(i for i in range(N) if coins[i] == 'H') for coins in sorted(all_coins(N))] def all_coins(N=4) -> Belief: "Return the belief set consisting of all possible coin sequences." return Belief(map(Coins, product('HT', repeat=N))) def rotations(coins) -> {Coins}: "A list of all possible rotations of a coin sequence." return {coins[r:] + coins[:r] for r in range(len(coins))} def coin_search(N=4) -> Strategy: "Use the generic `search` function to solve the Coin Flip problem." return search(start=all_coins(N), goal={'H' * N}, actions=all_moves(N), result=update) # Let's test the new definitions and make sure we haven't broken `update` and `coin_search`: # In[17]: assert all_moves(3) == [{0, 1, 2}, {0, 1}, {0}, set()] assert all_moves(4) == [{0, 1, 2, 3}, {0, 1, 2}, {0, 1}, {0, 2}, {0}, set()] assert all_coins(4) == {'HHHH', 'HHHT', 'HHTT', 'HTHT', 'HTTT', 'TTTT'} assert all_coins(5) == {'HHHHH','HHHHT', 'HHHTT','HHTHT','HHTTT', 'HTHTT', 'HTTTT', 'TTTTT'} assert rotations('HHHHHT') == {'HHHHHT', 'HHHHTH', 'HHHTHH', 'HHTHHH', 'HTHHHH', 'THHHHH'} assert update({'TTTTTTT'}, {3}) == {'HTTTTTT'} assert (update(rotations('HHHHHT'), {0}) == update({'HHTHHH'}, {1}) == update({'THHHHH'}, {2}) == {'HHHHHH', 'HHHHTT', 'HHHTHT', 'HHTHHT'}) assert coin_search(4) == [ {0, 1, 2, 3}, {0, 2}, {0, 1, 2, 3}, {0, 1}, {0, 1, 2, 3}, {0, 2}, {0, 1, 2, 3}, {0, 1, 2}, {0, 1, 2, 3}, {0, 2}, {0, 1, 2, 3}, {0, 1}, {0, 1, 2, 3}, {0, 2}, {0, 1, 2, 3}] # How many distinct canonical coin sequences are there for up to a dozen coins? # In[18]: {N: len(all_coins(N)) for N in range(1, 13)} # On the one hand this is encouraging; there are only 352 canonical coin sequences of length 10, far less than the 4,096 non-canonical squences. On the other hand, it is discouraging; since we are searching over belief states, that would be 2352 belief states, which is nore than a googol. However, we should be able to easily handle up to N=7, because 220 is only a million. # # # Winning Strategies for 1 to 7 Coins # In[19]: {N: coin_search(N) for N in range(1, 8)} # Too bad; there are no winning strategies for N = 3, 5, 6, or 7. # # There *are* winning strategies for N = 1, 2, 4; they have lengths 1, 3, 15, respectively. Hmm. That suggests ... # # # A Conjecture # # > For every *N* that is a power of 2, there will be a shortest winning strategy of length 2*N* - 1. # # > For every *N* that is not a power of 2, there will be no winning strategy. # # # Winning Strategy for 8 Coins # # For N = 8, there are 236 = 69 billion belief states and if the conjecture is true there will be a shortest winning strategy with 255 steps. All the computations up to now have been instantaneous, but this one should take a few minutes. Let's see: # In[20]: get_ipython().run_line_magic('time', 'strategy = coin_search(8)') # In[21]: len(strategy) # **Eureka!** That's evidence in favor of the conjecture. But not proof. And it leaves many questions unanswered: # - Can you show there are no winning strategies for *N* = 9, 10, 11, ...? # - Can you prove there are no winning strategies for any *N* that is not a power of 2? # - Can you find a winning strategy of length 65,535 for *N* = 16 and verify that it works? # - Can you generate a winning strategy for any power of 2 (without proving it is shortest)? # - Can you prove there are no shorter winning strategies for *N* = 16? # - Can you prove the conjecture in general? # - Can you *understand* and *explain* how the strategy works, rather than just listing the moves? # # Visualizing Strategies # # # In[22]: def show(moves, N=4): "For each move, print the move number, move, and belief state." belief = all_coins(N) order = sorted(belief) for (i, move) in enumerate(moves, 1): belief = update(belief, move) print('{:3} | {:8} | {}'.format( i, movestr(move, N), beliefstr(belief, order))) def beliefstr(belief, order) -> str: return join(((coins if coins in belief else ' ' * len(coins)) for coins in order), ' ') def movestr(move, N) -> str: return join((i if i in move else ' ') for i in range(N)) def join(items, sep='') -> str: return sep.join(map(str, items)) # The following tables shows how moves change the belief state: # In[23]: show(coin_search(2), 2) # In[24]: show(coin_search(4)) # We can see that every odd-numbered move flips all four coins to eliminate the possibility of `TTTT`, flipping it to `HHHH`. We can also see that moves 2, 4, and 6 flip two coins and have the effect of eventually eliminating the two "two heads" sequences from the belief state, and then move 8 eliminates the "three heads" and "one heads" sequences, while bringing back the "two heads" possibilities. Repeating moves 2, 4, and 6 in moves 10, 12, and 14 then re-eliminates the "two heads", and move 15 gets the belief state down to `{'HHHH'}`. # # You could call `show(strategy, 8)`, but the results look bad unless you have a very wide (345 characters) screen to view it on. So instead I'll add a `verbose` parameter to `play` and play out some games with a trace of each move: # In[25]: def play(coins, strategy, verbose=False): "Play strategy against a random Devil; return final state of coins." N = len(coins) for i, move in enumerate(strategy, 1): if 'T' not in coins: return coins coins0 = coins coins1 = random.choice(list(rotations(coins))) coins = flip(coins1, move) if verbose: print('{:4d}: {} rot: {} flip: {} => {}'.format( i, coins0, coins1, movestr(move, N), coins)) return coins play('HHTH', coin_search(4), True) # In[26]: play('HTTHTTHT', strategy, True)