-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle_solver_recursive.py
89 lines (68 loc) · 2.53 KB
/
puzzle_solver_recursive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ast
import time
import clue_scraper
import puzzle_structure
def main():
"""
**********************************************************************************************************************
Main code to run on program execution
"""
fill( 'oct0515' )
def fill( puzzle_name ):
"""
**********************************************************************************************************************
"""
# puzzle_structure.read_raw_puzzle( puzzle_name )
puzzle = puzzle_structure.create_puzzle( puzzle_name, 'skeleton' )
# clue_scraper.lookup_all_clues( puzzle_name )
puzzle_structure.sort_answers( puzzle_name )
f = open( 'puzzles/' + puzzle_name + '/' + puzzle_name + '-answers.txt', 'r' )
answers = f.read().splitlines()[3:]
for i in range( len( answers ) ):
answers[i] = answers[i].strip().split( '\t' )
answers[i] = [ int( answers[i][0] ), int( answers[i][1] ), answers[i][2], ast.literal_eval( answers[i][3] ), ast.literal_eval( answers[i][4] ) ]
recursive_solver( puzzle, answers, 0 )
puzzle_structure.print_puzzle( puzzle )
def recursive_solver( puzzle, answers, answer_index ):
"""
**********************************************************************************************************************
"""
time.sleep( .25 )
puzzle_structure.print_puzzle( puzzle )
## exit condition ##
if ( answer_index == len( answers ) ):
return 1
cur_info = answers[ answer_index ]
candidates = cur_info[4]
print( answer_index, candidates )
for candidate in candidates:
if ( is_valid( candidate, puzzle, cur_info[0], cur_info[1], cur_info[2] ) ):
puzzle = fill_answer( candidate, puzzle, cur_info[0], cur_info[1], cur_info[2] )
if recursive_solver( puzzle, answers, answer_index + 1 ):
return 1
puzzle = fill_answer( ' ' * len( candidate ), puzzle, cur_info[0], cur_info[1], cur_info[2] )
def is_valid( answer, puzzle, row, col, direction ):
"""
**********************************************************************************************************************
"""
for i in range( len( answer ) ):
if puzzle[row][col] != answer[i] and puzzle[row][col] != ' ':
return 0
if direction == 'across':
col += 1
else:
row += 1
return 1
def fill_answer( answer, puzzle, row, col, direction ):
"""
**********************************************************************************************************************
"""
for i in range( len( answer ) ):
puzzle[row][col] = answer[i]
if direction == 'across':
col += 1
else:
row += 1
return puzzle
if __name__ == '__main__':
main()