Skip to content

Instantly share code, notes, and snippets.

@cdanis
Last active July 16, 2020 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdanis/ea98c223b52d38d08d9734aa319c8f0f to your computer and use it in GitHub Desktop.
Save cdanis/ea98c223b52d38d08d9734aa319c8f0f to your computer and use it in GitHub Desktop.
Solves the 'password' puzzle in Keep Talking And Nobody Explodes
#!/usr/bin/env python3
"""
Solves the 'password' puzzle in Keep Talking And Nobody Explodes.
https://en.wikipedia.org/wiki/Keep_Talking_and_Nobody_Explodes
$ ktane-passwords ahiwx tpcri
The answer is WRITE ๐Ÿ˜…
$ ktane-passwords oglwqm oxanbe wvnrke
Multiple possible answers! ๐Ÿ˜ฌ ['large', 'world']
$ ktane-passwords oglwqm oxanbe wvnkex
Something is wrong, no matches ๐Ÿ˜ฐ
$ ktane-passwords '*' oxanbe wvnrke xudkbl
The answer is WORLD ๐Ÿ˜…
"""
__author__ = "Chris Danis"
__email__ = "cdanis@gmail.com"
__license__ = "MIT"
import argparse
import itertools
import string
words = [
'about', 'after', 'again', 'below', 'could', 'every', 'first',
'found', 'great', 'house', 'large', 'learn', 'never', 'other',
'place', 'plant', 'point', 'right', 'small', 'sound', 'spell',
'still', 'study', 'their', 'there', 'these', 'thing', 'think',
'three', 'water', 'where', 'which', 'world', 'would', 'write',
]
p = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter)
p.add_argument('letters', nargs='+',
help='The letters from each column, for example: ahiwx tpcri \n'
'You may also use * to allow any letter in that column, \n'
'useful if you missed the first column.')
args = p.parse_args()
for (idx, column) in enumerate(args.letters):
if column == '*':
args.letters[idx] = string.ascii_lowercase
all_letter_combos = [''.join(x) for x in itertools.product(*args.letters)]
candidates = [w for w in words for L in all_letter_combos if w.startswith(L)]
if not candidates:
print("Something is wrong, no matches ๐Ÿ˜ฐ")
elif len(candidates) == 1:
print(f"The answer is {candidates[0].upper()} ๐Ÿ˜…")
else:
print(f"Multiple possible answers! ๐Ÿ˜ฌ {candidates}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment