-
Notifications
You must be signed in to change notification settings - Fork 0
/
genwords.py
60 lines (54 loc) · 1.64 KB
/
genwords.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
import json
with open("accepted.json") as fp:
c = fp.read()
words = json.loads(c)
accepted = set(w for w in words)
with open("answers.json") as fp:
c = fp.read()
words = json.loads(c)
answers = set(w for w in words)
with open("scrabble.json") as fp:
c = fp.read()
words = json.loads(c)
scrabble = set(w for w in words if len(w) == 5)
ALL_WORDS = sorted(set().union(accepted, answers, scrabble))
with open("words.h", "w") as fp:
fp.write("#pragma once\n\n")
fp.write("#include <stdbool.h>\n")
fp.write("#include <stdint.h>\n")
fp.write("\n")
fp.writelines(
[
"typedef struct word {\n",
" const char* w;\n",
" const uint32_t letterMasks[5];\n",
" const uint32_t contains;\n",
" const bool canBeAnswer;\n",
" const bool isScrabbleWord;\n",
"} word;\n\n",
]
)
fp.write("word words[] = {\n")
wc = 0
for word in ALL_WORDS:
if len(word) != 5:
continue
canBeAnswer = word in answers
scrabbleWord = word in scrabble
letters = []
seen = 0
for c in word:
m = 1 << (ord(c) - ord("a"))
seen |= m
letters.append(m)
letterMasks = "{" + ", ".join(str(lm) + "U" for lm in letters) + "}"
cba = "false"
scr = "false"
if canBeAnswer:
cba = "true"
if scrabbleWord:
scr = "true"
fp.write(f' {{"{word}", {letterMasks}, {seen}U, {cba}, {scr}}},\n')
wc += 1
fp.write("};\n\n")
fp.write(f"const int wordCount = {wc};\n")