Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solver for "Passengers in a Railroad Compartment" puzzle.
"""Passengers in a Railroad Compartment
Six passengers sharing a train compartment are from Moscow, Leningrad, Tula,
Kiev, Kharkov, and Odessa.
1. A and the man from Moscow are physicians
2. E and the Leningrader are teachers.
3. The man from Tula and C are engineers.
4. B and F are WWII veterans, but the man from Tula has never served in the
armed forces.
5. The man from Kharkov is older than A.
6. The man from Odessa is older than C.
7. At Kiev, B and the man from Moscow got off.
8. At Vinnitsa, C and the man from Kharkov got off.
Match initials, professions, and cities. Also, are the facts both sufficient
and necessary to solve the problem?
from Boris A Kordemksy, The Moscow Puzzles: 359 Mathematical Recreations, p111.
"""
from itertools import permutations
def solve():
"""Search through enumerated solution space."""
perms = list(permutations([1, 2, 3, 4, 5, 6]))
a, b, c, d, e, f = (1, 2, 3, 4, 5, 6)
return ((phys1, phys2, teach1, teach2, eng1, eng2,
Moscow, Leningrad, Tula, Kiev, Kharkov, Odessa)
for phys1, phys2, teach1, teach2, eng1, eng2 in perms
if a is phys1 or a is phys2
if e is teach1 or e is teach2
if c is eng1 or c is eng2
for Moscow, Leningrad, Tula, Kiev, Kharkov, Odessa in perms
if Moscow is phys1 or Moscow is phys2
if Leningrad is teach1 or Leningrad is teach2
if Tula is eng1 or Tula is eng2
if a is not Moscow
if e is not Leningrad
if c is not Tula
if b is not Tula
if f is not Tula
if Kharkov is not a
if Odessa is not c
if Moscow is not b
if Kharkov is not c)
if __name__ == '__main__':
print list(solve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.