-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
45 lines (36 loc) · 1.2 KB
/
day9.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
import numpy as np
import re
def decompressed_len(t):
m = re.search('\((\d+)x(\d+)\)', t)
if m is None:
return len(t)
else:
i = int(m.group(1))
j = int(m.group(2))
return len(t[:t.index('(')]) + j * decompressed_len(t[t.index(')')+1:t.index(')')+1+i]) + decompressed_len(t[t.index(')')+1+i:])
def decompress(in_file):
# read file
f = open(in_file, 'r')
text = f.read()
text = re.sub('[ \n]', '', text)
counter = 0
decompressed_file_len = 0
while counter < len(text):
l = text[counter]
if l == '(':
m = re.search('(\d+)x(\d+)', text[counter+1:counter+10])
i = m.group(1)
j = m.group(2)
counter += 3 + len(i) + len(j)
i = int(i)
j = int(j)
data = text[counter:counter+i]
decompressed_file_len += j * len(data)
counter += i
else:
decompressed_file_len += 1
counter += 1
print('Length of decompressed file is {}'.format(decompressed_file_len))
print('Length of decompressed file in format 2 is {}'.format(decompressed_len(text)))
if __name__ == "__main__":
decompress('day9_input.txt')