-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfiveletterwords.py
110 lines (81 loc) · 3.1 KB
/
fiveletterwords.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import time
start_time = time.time()
filestub = '/Users/mattparker/Dropbox/python/five_letter_words/'
def load_words():
words_txt = '/Users/mattparker/Dropbox/python/five_letter_words/words_alpha.txt'
with open(words_txt) as word_file:
valid_words = list(word_file.read().split())
return valid_words
word_length = 5
word_length2 = word_length*2
word_length4 = word_length2*2
word_length5 = word_length4 + word_length
# number of scanA increases per progress report
stepgap = 1000
# Yes, that is the alphabet. In the default order python makes a list in. Weird.
alphabet = ['f', 'g', 'o', 'q', 't', 'b', 'y', 'h', 'r', 'u', 'j', 'w', 'i', 'p', 's', 'd', 'l', 'e', 'k', 'm', 'n', 'v', 'z', 'c', 'a', 'x']
# I could be clever and write this to be dynamic
# but for now I'll hard code everything assuming five words
number_of_sets = 5
english_words = load_words()
print(f"{len(english_words)} words in total")
fl_words = []
for w in english_words:
if len(w) == word_length:
fl_words.append(w)
print(f"{len(fl_words)} words have {word_length} letters")
word_sets = []
unique_fl_words = []
for w in fl_words:
unique_letters = set(w)
if len(unique_letters) == word_length:
if unique_letters not in word_sets:
word_sets.append(unique_letters)
unique_fl_words.append(w)
number_of_words = len(unique_fl_words)
print(f"{number_of_words} words have a unique set of {word_length} letters")
doubleword_sets = []
doubleword_words = []
scanA = 0
while scanA < number_of_words-1:
scanB = scanA + 1
while scanB < number_of_words:
give_it_a_try = word_sets[scanA] | word_sets[scanB]
if len(give_it_a_try) == word_length2:
doubleword_sets.append(give_it_a_try)
doubleword_words.append([unique_fl_words[scanA], unique_fl_words[scanB]])
scanB += 1
scanA += 1
number_of_doublewords = len(doubleword_sets)
print(f"we found {number_of_doublewords} combos")
counter = 0
success_found = []
scanA = 0
print(f"starting at position {scanA}")
while scanA < number_of_doublewords-1:
if scanA % stepgap == 0:
print(f"Up to {scanA} after {time.time() - start_time} seconds.")
scanB = scanA + 1
while scanB < number_of_doublewords:
give_it_a_try = doubleword_sets[scanA] | doubleword_sets[scanB]
if len(give_it_a_try) == word_length4:
scanC = 0
while scanC < number_of_words:
final_go = give_it_a_try | word_sets[scanC]
if len(final_go) == word_length5:
success = doubleword_words[scanA] + doubleword_words[scanB]
success.append(unique_fl_words[scanC])
success.sort()
if success not in success_found:
success_found.append(success)
print(success)
scanC += 1
counter += 1
scanB += 1
scanA += 1
print(f"Damn, we had {len(success_found)} successful finds!")
print(f"That took {time.time() - start_time} seconds")
print("Here they all are:")
for i in success_found:
print(i)
print("DONE")