-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
generate_db.py
152 lines (131 loc) · 5.38 KB
/
generate_db.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from collections import defaultdict
import json
import string
import struct
# See here for format info
# https://github.com/mattginsberg/cluer/blob/master/cluer.cpp
import string
digs = string.digits + string.ascii_letters
def int2base(x, base):
if x == 0:
return digs[0]
digits = []
while x:
digits.append(digs[x % base])
x = x // base
digits.reverse()
return ''.join(digits)
def inttob32(n):
return int2base(n, 32)
class GenerateDB(object):
_cluedata = None
_clueblock = None
words = []
# clue_map = defaultdict(list)
words_by_length = defaultdict(list)
bitmaps_by_length = defaultdict(
lambda: defaultdict(lambda: defaultdict(int)))
b64_by_length = {}
def __init__(self, cluedata_filename):
self._cluedata = cluedata_filename
with open(self._cluedata, 'rb') as f:
numwords = struct.unpack('<I', f.read(4))[0]
print("Initializing {0} words".format(numwords))
for _ in range(numwords):
l = struct.unpack('<B', f.read(1))[0]
s = struct.unpack('<{}s'.format(l), f.read(l))[
0].decode('ascii')
self.words.append([s, 0])
self._clueblock = f.tell()
def initialize_bitmaps(self):
self.words.sort(key=lambda w: w[1])
for w in filter(lambda w: w[1], self.words):
self.words_by_length[len(w[0])].append([w[0], w[1]])
for length, wordlist in self.words_by_length.items():
for letter in string.ascii_uppercase:
for idx in range(length):
bitmap = 0
for word_idx in range(len(wordlist)):
if wordlist[word_idx][0][idx] == letter:
bitmap |= (1 << word_idx)
self.bitmaps_by_length[length][letter][idx] = bitmap
self.b64_by_length[str(
length)+letter+str(idx)] = inttob32(bitmap)
def initialize_clue_map_and_scores(self):
with open(self._cluedata, 'rb') as f:
f.seek(self._clueblock)
clues = []
clue_clues = []
numclues = struct.unpack('<I', f.read(4))[0]
print('numclues: ', numclues)
for _ in range(numclues):
l = struct.unpack('<B', f.read(1))[0]
s = struct.unpack('<{}s'.format(l), f.read(l))[0]
clues.append(s)
numtraps = struct.unpack('<I', f.read(4))[0]
traps = []
for _ in range(numtraps):
traps.append(struct.unpack('<I', f.read(4))[0])
clue_clues.append(traps)
print('done w/ words')
n = struct.unpack('<I', f.read(4))[0]
while(True):
num = struct.unpack('<h', f.read(2))[0]
diff = struct.unpack('<h', f.read(2))[0]
yr = struct.unpack('<h', f.read(2))[0]
th = struct.unpack('<b', f.read(1))[0]
pnum = struct.unpack('<b', f.read(1))[0]
cnum = struct.unpack('<I', f.read(4))[0]
# fill = self.words[n][0]
# self.clue_map[fill].append({"num": num,
# "diff": diff,
# "yr": yr,
# "pnum": pnum,
# "th": th,
# "cnum": cnum,
# "text": clues[cnum],
# "traps": [(t, self.words[t >> 1][0]) for t in clue_clues[cnum]]})
# Update scoring
if not th:
if pnum == 8: # nyt
self.words[n][1] += num * 5
else:
self.words[n][1] += num
try:
n = struct.unpack('<I', f.read(4))[0]
except (IndexError, struct.error):
break
def write_wordlist(self):
with open("cluedata.txt", "w") as wordlist:
wordlist.writelines(
[w[0].upper() + ';' + str(w[1]) + '\n' for w in self.words if w[1]])
def write_db(self):
def ddict(d):
if isinstance(d, dict):
for k, v in d.items():
if isinstance(v, dict):
d[k] = ddict(v)
return dict(d)
if isinstance(d, list):
return [ddict(x) for x in d]
return d
with open("_db.json", "w") as dbjson:
json.dump({"words": self.words_by_length,
"bitmaps": self.b64_by_length},
dbjson)
with open("_db.py", "w") as db:
content = ["words_by_length = {}\n".format(ddict(self.words_by_length)),
# "clue_map = {}".format(self.clue_map),
"bitmaps_by_length = {}\n".format(ddict(self.bitmaps_by_length))]
db.writelines(content)
if __name__ == "__main__":
print("Initializing wordlist")
cd = GenerateDB("cluedata")
print("Initializing scores")
cd.initialize_clue_map_and_scores()
print("Writing wordlist")
cd.write_wordlist()
# print("Initializing bitmaps")
# cd.initialize_bitmaps()
# print("Writing db")
# cd.write_db()