-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecode.py
122 lines (106 loc) · 3.99 KB
/
decode.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
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
#!/usr/bin/env python3
# masks derived from mask_finder.py
masks = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 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8, 9, 10, 11, 12, 13, 14]
#[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15],
#[1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 14],
# this one is correct:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
]
def bitstring_to_bytes(s):
return bytes(int(s[i : i + 8], 2) for i in range(0, len(s), 8))
# yet another parity check function, this time uses all masks and returns
# index of bit that needs to be flipped
# totally not necessary for this challenge, but I learned something so that's cool
def parity_check(s):
sum = 0
# make list of the indices with ones in them
columns = []
for i, c in enumerate(s):
if c:
columns.append(i)
# generate syndrome
syndrome = []
for mask in masks:
sum = 0
for x in columns:
sum += mask[x]
syndrome.append(sum % 2)
# error stays -1 if no error
error = -1
# check masks against syndrome
if any(syndrome):
for i in range(0, 17):
for j, mask in enumerate(masks):
if mask[i] != syndrome[j]:
break
if j == len(masks) - 1:
error = i
if error != -1:
break
return error
# read bitstream from float parse
with open('bits.txt', 'r') as f:
bit_stream = f.read()
# break into 17 bit chunks
segments = [bit_stream[i:i+17] for i in range(0, len(bit_stream), 17)]
blocks = []
# string to int
for i, s in enumerate(segments):
blocks.append([])
for b in s:
blocks[i].append(int(b))
# error correction (again, not necessary)
for i, block in enumerate(blocks):
# check for an error
e = parity_check(block)
if e != -1:
# correct error
blocks[i][e] = (blocks[i][e] + 1) % 2
# not sure which bits are data, try multiple
# removed code that tests every bit offset
# now we have error-corrected bit stream with no offset
for i, mask in enumerate(data_bit_masks):
bit_stream = ''
for j, block in enumerate(blocks):
for index in mask:
bit_stream += str(block[index])
with open('playable_output' + str(i), 'wb') as f:
b = bitstring_to_bytes(bit_stream)
f.write(b)