Skip to content

Instantly share code, notes, and snippets.

@szepnapot
Last active February 18, 2019 21:54
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 szepnapot/aacdfcfbec48f737a763895d7e73ed49 to your computer and use it in GitHub Desktop.
Save szepnapot/aacdfcfbec48f737a763895d7e73ed49 to your computer and use it in GitHub Desktop.
Recover a secret string from random triplets
from itertools import chain
def get_behind_chars(c, pool):
behind = []
for item in pool:
if c in item:
chars = item[item.index(c):]
[behind.append(_) for _ in chars if _ != c]
return list(set(behind))
def get_pos(word, triplets):
levels = []
for level in range(len(triplets[0]) + 1):
if not levels:
levels.append(get_behind_chars(word, triplets))
else:
levels.append([c for char in levels[level - 1] for c in get_behind_chars(char, triplets)])
return len(set(chain(*levels)))
def recoverSecret(triplets):
flat = set(c for triplet in triplets for c in triplet)
words = {word: get_pos(word, triplets) for word in flat}
order = sorted(words.items(), key=lambda kv: kv[1])
return ''.join([tup[0] for tup in order][::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment