35 lines
854 B
Python
35 lines
854 B
Python
# Python!
|
|
import re
|
|
|
|
class compression_group:
|
|
def __init__(self, match_object):
|
|
self.length, self.reps = [int(x) for x in match_object.groups()]
|
|
self.marker_start, self.body_start = match_object.span()
|
|
self.end = self.body_start + self.length
|
|
|
|
|
|
with open('09.txt') as file:
|
|
compressed = file.read().rstrip('\n')
|
|
|
|
marker_exp = re.compile('\((\d*)x(\d*)\)')
|
|
char_exp = re.compile('[\(\)\dx]')
|
|
|
|
markers = marker_exp.finditer(compressed)
|
|
groups = [compression_group(m) for m in markers]
|
|
|
|
characters = 0
|
|
index = 0
|
|
for i, c in enumerate(compressed):
|
|
if not char_exp.match(c):
|
|
current_groups = []
|
|
multiplier = 1
|
|
for group in groups:
|
|
if group.body_start <= i < group.end:
|
|
multiplier *= group.reps
|
|
current_groups.append(group)
|
|
elif current_groups and i > current_groups[0].end:
|
|
break
|
|
characters += multiplier
|
|
|
|
print(characters)
|