代码
py
import zlib
import base64
field_str = """
IJJJLOO___
IJZZLOO___
IZZLLS____
ITJJSS____
ITTJS_____
ITLLL_____
IJJJ______
IJ________
"""
blockid = {
'_': 0,
'I': 1,
'L': 2,
'O': 3,
'Z': 4,
'T': 5,
'J': 6,
'S': 7,
'X': 8,
'=': 9,
}
def field_from_str(field: str):
res = []
for line in field.split('\n'):
if len(line) != 10:
continue
for i in range(0, len(line), 2):
a, b = blockid[line[i]], blockid[line[i + 1]]
res.append(a << 4 | b)
return bytes(res)
def field_to_base64(field: str):
return base64.b64encode(zlib.compress(field_from_str(field))[2:-4]).decode()
res = field_to_base64(field_str)
print(len(res))
print(res)
print(zlib.decompress(zlib.compress(field_from_str(field_str))[2:-4], -15))
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
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
python 的 raw inflate/deflate
raw inflate 其实就是没有头部和尾部的 inflate,有个很暴力的实现方式就是直接截掉。
py
zlib.compress(data)[2:-4]
1
解压的时候需要加个标志让 python 不报错
py
zlib.decompress(data, -15)
1