-
Notifications
You must be signed in to change notification settings - Fork 0
/
9-2.py
48 lines (31 loc) · 851 Bytes
/
9-2.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
#!/usr/bin/python3.5
import sys
import re
from pprint import pprint
#from operator import itemgetter
xbyy = re.compile('\((\d+)x(\d+)\)')
def decomp(cur, totlen):
m = xbyy.search(cur)
if m:
print('c: ', len(cur), cur)
print('len: ', totlen)
times = int(m.group(2))
nchars = int(m.group(1))
start = m.end()
end = start + nchars
print ('decompressing %d by %d starting at %d:%d'%(nchars, times, start, end))
return int(m.start()) + times * decomp(cur[start:end], 0) + decomp(cur[end:], totlen)
else:
return len(cur) + totlen
s = []
while True:
try:
line = sys.stdin.readline().rstrip()
d = decomp(line, 0)
s.append(d)
print(d)
except KeyboardInterrupt:
break
if not line:
break
print(sum(s))