-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle.py
More file actions
114 lines (99 loc) · 3.86 KB
/
puzzle.py
File metadata and controls
114 lines (99 loc) · 3.86 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import csv
import argparse
from termcolor import colored
class Puzzle:
def __init__(self, args):
self.puzzle = self.parse_puzzle(args.puzzle)
self.words = self.parse_words(args.words)
self.solved = []
def parse_puzzle(self, puzzle_name):
puzzle = []
with open(puzzle_name, 'r') as pfile:
p_reader = csv.reader(pfile)
for p_row in p_reader:
puzzle.append(p_row)
return puzzle
def parse_words(self, list_name):
words = []
with open(list_name, 'r') as cfile:
c_reader = csv.reader(cfile)
for c_row in c_reader:
words.append(str(c_row[0]).replace(' ', ''))
return words
def output_cli(self):
for ri, row in enumerate(self.puzzle):
for chi, ch in enumerate(row[0]):
if (ri, chi) in self.solved:
print(colored(f"{ch}", "red"),end=" ")
else:
print(colored(f"{ch}", "blue"),end=" ")
print()
def find_word(self):
for word in self.words:
if self.find_horizontal(word):
continue
if self.find_vertical(word):
continue
if self.find_diagonal(word):
continue
def find_horizontal(self, word):
for ri, row in enumerate(self.puzzle):
if word in str(row):
for i in range(0, len(word)):
self.solved.append((ri, str(row).find(word) - 2 + i))
return True
row_r = str(row)[::-1]
if word in row_r:
for i in range(0, len(word)):
self.solved.append((ri, len(row_r) - str(row_r).find(word) - 3 - i))
return True
return False
def find_vertical(self, word):
for char in range(len(self.puzzle[0][0])):
temp = []
for col in range(len(self.puzzle)):
temp.append(self.puzzle[col][0][char])
temp = ''.join(temp)
temp_r = temp[::-1]
if word in str(temp):
for i in range(0, len(word)):
self.solved.append((str(temp).find(word) + i, char))
return True
if word in str(temp_r):
for i in range(0, len(word)):
self.solved.append((len(temp_r) - str(temp_r).find(word) - 1 - i, char))
return True
return False
def find_diagonal(self, word):
for a in range(0, len(self.puzzle[0][0])):
temp = [[] for i in range(8)]
ranges = [[] for i in range(8)]
i = 0
while ((a - i) >= 0) and (i < len(self.puzzle)):
coords = [[i, a-i],[24-i, a-i], [24-i, 24-(a-i)], [i, 24-(a-i)]]
for cx, c in enumerate(coords):
temp[cx].append(self.puzzle[c[0]][0][c[1]])
ranges[cx].append((c[0], c[1]))
ranges[cx+4].append((c[1], c[0]))
i+=1
for ti in range(4):
temp[ti] = ''.join(temp[ti])
temp[ti+4] = temp[ti][::-1]
for tx, t in enumerate(temp):
if word in str(t):
for i in range(0, len(word)):
self.solved.append(ranges[tx][str(t).find(word) + i])
return True
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--puzzle', help='wordsearch_solver-main\puzzles\ws-1-list.csv')
parser.add_argument('--words', help='wordsearch_solver-main\puzzles\ws-1-puzzle.csv')
args = parser.parse_args()
p = Puzzle(args)
print("\nPROBLEM:")
p.output_cli()
print("\nSOLUTION:")
p.find_word()
p.output_cli()
#set src folder as directory first using cd +src folder path