Last active
July 16, 2020 23:16
-
-
Save cdanis/ea98c223b52d38d08d9734aa319c8f0f to your computer and use it in GitHub Desktop.
Solves the 'password' puzzle in Keep Talking And Nobody Explodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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__ = "[email protected]" | |
__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