-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvpack.py
More file actions
213 lines (178 loc) · 6.37 KB
/
advpack.py
File metadata and controls
213 lines (178 loc) · 6.37 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import struct
import json
import re
from itertools import accumulate
from dizzy import dizzy, undizzy, dizzy_squeeze, woozy, unwoozy, unwrap
splash40 = """
Welcome to
Colossal Cave Adventure!
Original development by Willie Crowther.
Major features added by Don Woods.
Conversion to BDS C by J. R. Jaeger.
Unix standardization by Jerry D. Pohl.
QNX 4 port, bug fixes by James Lummel.
Taliforth 6502 port by P. Surry from
code by J. Wiberg, J. Gillogly.
Would you like instructions?
"""
def compact_word(lo: int, hi: int, words: list[str]) -> bytes:
"""
byte 0: code % 1000 (<=147)
byte 1: code // 1000 (<=3)
byte 2: length of str 0 (<=15) (hi bit set on last string)
byte 3+: chars for str 0
...
last string in group has high bit of length set, followed by next header
last header is ff/ff
"""
assert lo < 256 and hi < 4 and all([len(w) < 16 for w in words])
n = len(words)
cws = b''.join([
bytes([len(w) + (0x80 if i == n-1 else 0)]) + w.encode('ascii')
for i,w in enumerate(words)
])
return bytes([lo, hi]) + cws
def compact_cond(idx: int, c: int):
"""cond byte for 1-indexed cave. original used a bit for each hint
but to save space, since hints don't overlap, we enumerate in high nibble"""
hint = {0: 0, 16: 1, 32: 2, 64: 3, 128: 4}[c&0xf0]
extra = {99: 5, 100: 5, 101: 5, 108: 6}
if idx in extra:
assert hint == 0, "overlapping hint!"
hint = extra[idx]
v = (c & 0xf) | (hint << 4)
print(idx, c, v)
return v
def compact_cave(long: bytes, short: bytes, travel: list[tuple[int,int,int,int,int]]):
"""
compact representation for cave data
the tuple looks like (dtyp, dest, verb, cflg, cobj) <= (2, 161, 307, 7, 95)
so in bits dtyp:2, dest:8, verb:9, cflg:3, cobj:7 = 2+8+9+3+7 = 29
which we represent in a uint32 as
+-----------------+-----------------+-----------------+-----------------+
| 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |
+------+-----+----+-----------------+--------------+--+-----------------+
| . . .| cf | dt | dest | cobj | verb |
+------+-----+----+-----------------+--------------+--+-----------------+
byte 0: offset to long (or 0 if none)
byte 1: # travel
byte 2: 4 x # travel uint32
@short: strz
[@long: strz]
"""
dvcs = [
v | (c << 9) | (d << 16) | (dt << 24) | (cf << 26)
for (dt, d, v, cf, c) in travel
]
if long == short:
offset = 0
else:
offset = 2 + 4 * len(dvcs) + len(short)
assert offset < 256
data = struct.pack(f"<BB{len(dvcs)}I", offset, len(dvcs), *dvcs)
data += short
if offset:
data += long
return data
if __name__ == "__main__":
# fetch the extracted data
advent = json.load(open('data/advent.json'))
# custom 40-column splash screen
advent['messages']['65'] = splash40
caves = list(advent['caves'].values())
# was 27176
for c in caves:
s = c['long']
c['long'] = unwrap(c['long'])
c['short'] = unwrap(c['short'])
corpus = (
[c['long'] for c in caves]
+ [c['short'] for c in caves if c['short'] != c['long']]
+ list(advent['messages'].values())
+ sum(advent['items'].values(), [])
)
# make a digram lookup table
source = unwrap('\0'.join(corpus)+'\0')
print('corpus has', len(corpus), 'strings', len(source), 'total characters as strz')
open('scripts/corpus.asc', 'w').write(source)
data, digrams = dizzy(woozy(source))
print(f"dizzy compress {len(source)} source bytes to {len(data)} compressed bytes "
f"with {len(digrams)} digrams. uncompress ok? {unwoozy(undizzy(data, digrams)) == source}")
max_sqz = dict(raw=0, woozy=0, sqz=0)
def sqz(s):
w = woozy(unwrap(s))
z = dizzy_squeeze(w, digrams) + b'\0'
for (t, d) in zip(['raw', 'woozy', 'sqz'], [s, w+b'\0', z]):
max_sqz[t] = max(max_sqz[t], len(d))
return z
"""
print(', '.join(f'${x:02x},${y:02x}' for x,y in d.values()))
print(', '.join(f'${x:02x}' for x in z.split(b'\0')[0]))
print(src.split(b'\0')[0].decode('ascii'))
"""
def pack_index(zs: list[bytes]) -> bytes:
offsets = list(accumulate((len(z) for z in zs), initial=0))[:-1]
idx = struct.pack(f"<{len(offsets)}H", *offsets)
assert len(offsets) == len(zs)
return idx
# digrams
print(f"??? constant ADVDAT")
data = b''.join(digrams.values())
bin = data
header = struct.pack("<H", len(data))
print(f"DIGRAMS {len(data)} bytes")
# words
# first group as (lo, hi): [ words ]
wdict = {}
for (w, lo, hi) in advent['words'].values():
wdict.setdefault((lo, hi), []).append(w)
data = b''.join(
compact_word(lo, hi, ws) for (lo, hi), ws in wdict.items()
) + bytes([0xff, 0xff])
print(f"VOCAB {len(data)} bytes")
bin += data
header += struct.pack("<H", len(data))
# cond
data = bytes([0] + [
compact_cond(i+1, c['cond']) for i,c in enumerate(caves)
])
print(f"COND {len(data)} bytes")
bin += data
header += struct.pack("<H", len(data))
# caves
zs = [
compact_cave(sqz(c['long']), sqz(c['short']), c['travel'])
for c in caves
]
idx = pack_index(zs)
data = b''.join(zs)
print(f"CAVES& {len(idx)} bytes")
bin += idx
print(f"CAVES {len(data)} bytes")
bin += data
header += struct.pack("<HH", len(idx), len(data))
# messages
zs = [sqz(msg) for msg in advent['messages'].values()]
idx = pack_index(zs)
data = b''.join(zs)
print(f"MSGS& {len(idx)} bytes")
bin += idx
print(f"MSGS {len(data)} bytes")
bin += data
header += struct.pack("<HH", len(idx), len(data))
# items
zs = [
b''.join(sqz(s) for s in states)
for states in advent['items'].values()
]
idx = pack_index(zs)
data = b''.join(zs)
print(f"ITEMS& {len(idx)} bytes")
bin += idx
print(f"ITEMS {len(data)} bytes")
bin += data
header += struct.pack("<HH", len(idx), len(data))
bin = struct.pack("<H", len(header)//2) + header + bin
print(f"longest string {max_sqz['raw']}, woozy {max_sqz['woozy']}, packed {max_sqz['sqz']}")
open('data/advent.dat', 'wb').write(bin)
print(f"Wrote {len(bin)} bytes to advent.dat")