hello-dosdl/compress-targets.py

35 lines
890 B
Python
Raw Normal View History

2022-02-22 22:38:06 +00:00
#!/usr/bin/env python
import json
import sys
alphabet = 'abcdefghijklmnopqrstuvwxyz*'
srcpath = sys.argv[1]
targetpath = sys.argv[2]
with open(srcpath, 'r') as f:
all_words = json.load(f)
# 'murky' is the rarest word we'll use
# https://github.com/lynn/hello-wordl/blob/7da40c1f067eb1ec157d4c5b7a9bd8257ed39342/src/Game.tsx#L34
words = []
for word in all_words:
words.append(word)
if word == 'murky': break
2022-02-22 22:38:06 +00:00
# We only care about 5-letter words
words = [word for word in words if len(word) == 5]
array = []
for word in words:
number = 0
for index, letter in enumerate(word):
number += alphabet.index(letter) << (5 * index)
packed = bytes([number & 0xff, (number >> 8) & 0xff, (number >> 16) & 0xff, number >> 24])
array.append(packed)
with open(targetpath, 'w') as f:
2022-02-23 00:31:52 +00:00
f.write('targets:\n')
2022-02-22 22:38:06 +00:00
for packed in array:
f.write(f'\tdb {", ".join(str(byte) for byte in packed)}\n')