-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcirclegram.py
80 lines (60 loc) · 2.47 KB
/
circlegram.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
#!/usr/bin/python3
# codeword.py
# Solve the the codeword puzzle
#
#
# John Clarke, [email protected]
# V0.1 2021-03-30
import re, os, time
# This problem has three collections of letters:
# each pair of collection share one ketter, and all theee share one.
# common shared letter is unknown and the challenge is to find that letter such that
# all three sets of letters can form a word.
# those three works share a common theme.
# Set a puzzle up
def setPuzzle():
puzzle = tuple()
puzzle += (('r', 'e', 'u', 'h', 'p', 'b'),)
puzzle += (('b', 'g', 'm', 'a', 'r', 'e'),)
puzzle += (('e', 'i', 't', 's', 'w', 'r'),)
return puzzle
class CircleGramToSolve:
def __init__(self, starting_puzzle: tuple, base_dictionary: dict):
self.starting_puzzle = starting_grid
self.base_dictionary = base_dictionary
self.verbose = True
class Result:
def __init__(self, solution_letter, three_words):
self.soltuion_letter = solution_letter
self.three_words = three_words
def show():
print("Common Letter: "+self.solution_letter)
print("Three words: "+self.three_words
def showResult(r: result):
CircleGramToSolve.Result.show(r)
def solve():
results = list()
# There are 26 possibilities for the unknown letter, just go through them and
# get anagram lists for each of the three letter combinations. If any combination
# yields zero words, then this solution letter is invalid. If the letter yields
# words for each of the three, create a potential result for each combination of
# words (with the shared letter)
# Now cull those where the words don't have a common theme.
return results
if __name__ == "__main__":
# Get the thing you need to solve it: the puzzle and the dictionary
puzze = setPuzzle()
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(dir_path + "/ukenglish.txt", "r", encoding="latin-1") as myfile:
my_dictionary = myfile.read().splitlines()
# Set up the class and solve
cgts = CircleGram(puzzle, my_dictionary)
results = cgts.solve()
# Output results
if (len(results) == 0) :
print("FAILED TO SOLVE PUZZLE")
else :
print("Solutions found: %d" % len(results))
for r in results:
print("RESULT:")
cgts.showResult(r)