Skip to content

Instantly share code, notes, and snippets.

@bertptrs
Last active June 13, 2019 09:02
Show Gist options
  • Save bertptrs/3ec95457ee50d95e1a6e28237e68ed09 to your computer and use it in GitHub Desktop.
Save bertptrs/3ec95457ee50d95e1a6e28237e68ed09 to your computer and use it in GitHub Desktop.
Pythonic proof that a certain Einstein-like puzzle was impossible
import itertools
import sys
PEOPLE = ['Pieter', 'Maaike', 'Kim', 'Tim', 'Patrieck']
AGES = ['Bever', 'Welp', 'Scout', 'Explorer', 'Rover']
BADGES = ['Das', 'Knopen', 'EHBO', 'Boomhut', 'Vissen']
def adjacent(group, *args):
# Exchange to make the table non-circular
# for i in range(len(group) - 1):
for i in range(len(group)):
if group[i] in args and \
group[(i + 1) % len(group)] in args:
return True
return False
def index_of(group, k):
for i, v in enumerate(group):
if v == k:
return i
sys.exit('Did not find ' + k + ' in ' + str(group))
for order in itertools.permutations(PEOPLE):
if order[1] != 'Tim':
continue
if order[0] in ['Pieter', 'Patrieck']:
continue
# check if together
if not adjacent(order, 'Pieter', 'Maaike'):
continue
for badges in itertools.permutations(BADGES):
if badges[2] != 'Knopen':
continue
boom_index = index_of(badges, 'Boomhut')
if order[boom_index][0] == 'P':
continue
for ages in itertools.permutations(AGES):
welp_index = index_of(ages, 'Welp')
if order[welp_index][0] == 'P':
continue
rover_index = index_of(ages, 'Rover')
if adjacent(badges, badges[rover_index], 'Vissen'):
continue
print('Solution:', order, badges, ages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment