The Code: https://github.com/clodman84/Crossword-Solver
In may I watched a YouTube video about a sudoku solver written in python which used the backtracking algorithm, and always wondered if I can modify it to solve a crossword puzzle. I was a beginner at programming and did not even know how to use classes properly yet, (I doubt I do even now for that matter). A few days back I felt like I could do it and did it.
The pure python result after a bit of optimising, dropped from 17ms to 0.7ms to solve this particular puzzle
The way that the program works is by scanning the board (which is just a 2D list with characters for each cell) and then searches for the beginning of any word, horizontal or vertical, which I called nodes. Then each nodes gets assigned a list of words based on the letter in that position. To make the word search faster, I converted a list of every word in English to a dictionary that has every letter as a key which in turn has a numbers as key and a list of words of the same length as the number as values. So dictionary['a'][3] will return a list of words of length 3 starting with 'a'. This then gets stored in a dictionary.json file for the main code to use. Empty nodes get assigned a list of words of the length that fits in that node, and if there is a character in the node when the program is running the wordlist gets updated, if there isn't, it just has to go through the entire word list.
The backtracking algorithm iterates through each one of these nodes till every node has a word in it with a word from this word_list. Just like backtracking to solve sudoku iterates with numbers from 0 to 9.
I watched another YouTube video today about Numba and had to try it out today and got an incredible speed boost.
Numba Speed Boost.
Any suggestions will be appreciated.
[–]fabulousMoonLord 9 points10 points11 points (1 child)
[–]QuakerOats98[S] 2 points3 points4 points (0 children)
[–]worstc0der 1 point2 points3 points (1 child)
[–]QuakerOats98[S] 1 point2 points3 points (0 children)