Skip to content

Instantly share code, notes, and snippets.

@minism
Created February 22, 2013 23:09
Show Gist options
  • Save minism/5017307 to your computer and use it in GitHub Desktop.
Save minism/5017307 to your computer and use it in GitHub Desktop.
import random
import pprint
import sys
# 7 Prisoners, 1 designated counter
prisoners = [
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": False, "messaged": False},
{"seen": False, "is_counter": True, "count": 0},
]
# Control and message bits (two switches)
control = random.choice(range(2))
message = random.choice(range(2))
init_control = control
init_message = message
tries = 0
while True:
tries += 1
prisoner = random.choice(prisoners)
if not prisoner['seen']:
prisoner['seen'] = True
control = 0
else:
if prisoner['is_counter']:
if message:
# Someone set this bit, store and clear it
prisoner['count'] += 1
if prisoner['count'] == 6:
print "Found all!"
print "Initial state: %s %s" % (init_control, init_message)
print "Num tries: %s" % tries
print "----------------"
pprint.pprint(prisoners)
sys.exit()
control = 1
message = 0
elif not prisoner['messaged']:
if control and not message:
prisoner['messaged'] = True
message = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment