hello-dosdl/compress-targets.py

35 lines
822 B
Python

#!/usr/bin/env python
import json
import sys
alphabet = 'abcdefghijklmnopqrstuvwxyz*'
srcpath = sys.argv[1]
rarestword = sys.argv[2]
targetpath = sys.argv[3]
with open(srcpath, 'r') as f:
all_words = json.load(f)
words = []
for word in all_words:
words.append(word)
if word == rarestword: break
# 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:
f.write(f'targets_len equ {len(array)}\n')
f.write('targets:\n')
for packed in array:
f.write(f'\tdb {", ".join(str(byte) for byte in packed)}\n')