Skip to content

Instantly share code, notes, and snippets.

@jmccardle
Last active February 15, 2023 20:46
Show Gist options
  • Save jmccardle/be54787a7a8f1ce0e288efaf66205aed to your computer and use it in GitHub Desktop.
Save jmccardle/be54787a7a8f1ce0e288efaf66205aed to your computer and use it in GitHub Desktop.
# knights and knaves
class Meta(type):
def __repr__(cls):
return f'<{cls.__name__}>'
class Knight(metaclass = Meta):
pass
class Knave(metaclass = Meta):
pass
# uppercase letters: declare unsolved identities as globals
A, B, C, D = None, None, None, None
# lowercase letters: statements
a = lambda: C is Knave
b = lambda: A is Knight and C is Knave
c = lambda: B is not C
d = lambda: A is Knave
# test until all 4 statements satisfied
def test():
return [stmt() if person is Knight
else not stmt() # invert logic if person is Knave
for person, stmt
in ( (A, a), (B, b), (C, c), (D, d) )
]
for A in (Knight, Knave):
for B in (Knight, Knave):
for C in (Knight, Knave):
for D in (Knight, Knave):
if all(test()):
print(A, B, C, D)
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment