crossword-solving-with-gpt.md

4 min read Original article ↗
import crosswordlib import time import copy def clear_terminal(): print('\033c', end='') def snooze(): time.sleep(0.1) crossword_grid = [ ["1", " ", "2", " ", "#", "3", "4", " ", "5", " ", "6", " ", "7"], [" ", "#", " ", "#", "#", "#", " ", "#", " ", "#", " ", "#", " "], ["8", " ", " ", " ", "#", "9", " ", " ", " ", " ", " ", " ", " "], [" ", "#", " ", "#", "10", "#", " ", "#", " ", "#", " ", "#", " "], ["11", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#", "#", "#"], [" ", "#", " ", "#", " ", "#", " ", "#", " ", "#", "12", "#", "13"], ["14", " ", " ", " ", " ", " ", "#", "15", " ", " ", " ", " ", " "], [" ", "#", " ", "#", " ", "#", "16", "#", " ", "#", " ", "#", " "], ["#", "#", "#", "17", " ", " ", " ", " ", " ", " ", " ", " ", " "], ["18", "#", "19", "#", " ", "#", " ", "#", " ", "#", " ", "#", " "], ["20", " ", " ", " ", " ", " ", " ", " ", "#", "21", " ", " ", " "], [" ", "#", " ", "#", " ", "#", " ", "#", "#", "#", " ", "#", " "], ["22", " ", " ", " ", " ", " ", " ", " ", "#", "23", " ", " ", " "]] crosswordlib.print_crossword(crossword_grid) number_coordinates, across_lengths, down_lengths, original_grid = crosswordlib.process_crossword_grid(crossword_grid) print("Number coordinates:") for number, coords in number_coordinates.items(): print(f"Number {number}: {coords}") print("\nAcross Lengths:") for number, length in across_lengths.items(): print(f"Number {number}: {length}") print("\nDown Lengths:") for number, length in down_lengths.items(): print(f"Number {number}: {length}") def process_crossword_clues(clue_list, original_grid, number_coordinates): modified_grid = copy.deepcopy(original_grid) if len(clue_list) == 0: # Win condition print('⭐') exit() return clue = clue_list[0] words_list, start_position, is_horizontal = clue for word in words_list: sub_modified_grid = copy.deepcopy(modified_grid) if crosswordlib.crossword_set(sub_modified_grid, start_position, is_horizontal, word, number_coordinates): clear_terminal() for row in sub_modified_grid: print(" ".join(row)) snooze() process_crossword_clues(clue_list[1:], sub_modified_grid, number_coordinates) def load_dict(filename): with open(filename, "r") as file: # Read the contents of the file file_contents = file.read().strip() # Split the contents into individual strings based on a delimiter (e.g., newlines) return file_contents.split("\n") w12 = load_dict("dictionary/12.txt") w8 = load_dict("dictionary/8.txt") w6 = load_dict("dictionary/6.txt") w4 = load_dict("dictionary/4.txt") { "1": ["buzz", "hype", "rush"], "8": ["rota", "roll"], "9": ["embezzle", "skimming"], "20": ["far-reaching"], "22": ["muscular", "stalwart"], "23": ["star", "icon"] } { "2": ["myriads", "countless"], "4": ["fierce", "untamed", "savage"], "12": ["fall apart", "break down", "give way"], "16": ["in a rush", "pronto"], "18": ["brey", "cove", "inle"], "19": ["elba", "lido"] } across = False down = True # length 8: 2 down, 9 across, 12 down # length 6: 4 down #print(w8) #exit() # I (A human) composed this solution path manually using the guesses suggested by ChatGPT. A more complete solution would automatically focus on the 'best' clue to work on dynamically. solution_path = [ (["bareback"], 1, down), (["tourniquet"], 5, down), (["egyptology"], 10, down), (["musthave"], 3, across), (["blingbling"], 11, across), # (["farreaching"], 20, across), (["canopy"], 14, across), (["floe"], 21, across), (["square", "piazza"], 15, across), (["rottweiler", "doberman"], 17, across), (["axel", "lutz", "loop"], 6, down), (["ease", "rest", "calm"], 7, down), (["hearhear", "amentothat"], 13, down), (["rota", "roll"], 8, across), (w6, 4, down), # no good guesses for this, so use the full wordlist. (w8, 9, across), (w8, 2, down), # (w8, 12, down), (w6, 4, down), (["brey", "cove", "inle"], 18, down), (["elba", "lido"], 19, down), (w8, 22, across), ] process_crossword_clues(solution_path, original_grid, number_coordinates) print("fail")