36 lines
749 B
Python
36 lines
749 B
Python
|
# Python!
|
||
|
import re
|
||
|
with open('09.txt') as file:
|
||
|
data = file.read().rstrip('\n')
|
||
|
|
||
|
|
||
|
marker_exp = re.compile('\((\d*)x(\d*)\)')
|
||
|
level = 1
|
||
|
|
||
|
while marker_exp.search(data):
|
||
|
print('Starting level', level, '. . .')
|
||
|
decompressed = ''
|
||
|
compressed = data
|
||
|
|
||
|
while True:
|
||
|
marker = marker_exp.search(compressed)
|
||
|
mark_start = marker.span()[0]
|
||
|
if mark_start:
|
||
|
decompressed += compressed[:mark_start]
|
||
|
|
||
|
length, reps = [int(x) for x in marker.groups()]
|
||
|
sec_start = marker.span()[1]
|
||
|
stop = sec_start + length
|
||
|
decompressed += compressed[sec_start:stop] * reps
|
||
|
remaining = compressed[stop:]
|
||
|
if marker_exp.search(remaining):
|
||
|
compressed = remaining
|
||
|
else:
|
||
|
decompressed += remaining
|
||
|
break
|
||
|
|
||
|
data = decompressed
|
||
|
level += 1
|
||
|
|
||
|
|
||
|
print(len(data))
|