Skip to content

Instantly share code, notes, and snippets.

@Samasaur1
Created October 21, 2023 16:33
Show Gist options
  • Save Samasaur1/72d9d60b9a43fd503d7d37d8b095ff7a to your computer and use it in GitHub Desktop.
Save Samasaur1/72d9d60b9a43fd503d7d37d8b095ff7a to your computer and use it in GitHub Desktop.

Revisions

  1. Samasaur1 created this gist Oct 21, 2023.
    39 changes: 39 additions & 0 deletions solver.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    alphabet = sorted([*"qwertyuiopasdfghjklzxcvbnm"])

    def gen_word(i1, i2, i3, i4):
    return alphabet[i1] + alphabet[i2] + alphabet[i3] + alphabet[i4]

    with open("/usr/share/dict/words", "r") as file:
    word_list = [x[:-1] for x in file.readlines() if len(x) == 5]

    print(len(word_list))
    # print(word_list)

    best_offset = (0,0,0,0)

    # conceptually, we hold the outer ring constant and find all permutations in the inner rings
    # for each one, we do the full 26-letter rotation, generating the words
    for i in range(26):
    # print(f"i = {i}")
    for j in range(26):
    # print(f" j = {j}")
    for k in range(26):
    # print(f" k = {k}")
    words = []
    for outer in range(26):
    w = gen_word(outer, (i + outer) % 26, (j + outer) % 26, (k + outer) % 26)
    words.append(w)
    # print(words)
    valid_words = [x for x in words if x in word_list]
    count = len(valid_words)
    if count > 2:
    print(valid_words)
    # print(count)
    # print(words)
    # print(valid_words)
    (_,_,_,prev_count) = best_offset
    if count > prev_count:
    best_offset = (i,j,k,count)
    print(f" updating best match to {i},{j},{k} (words: {valid_words})")

    print(best_offset)