-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
52 lines (36 loc) · 1.57 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
46
47
48
49
50
51
52
import os
import re
import get_input
search_term = re.compile('\(\d+x\d+\)')
def get_len_compression(compressed):
start = 0
uncompressed_len = 0
marker_match = re.search(search_term, compressed[start:])
while marker_match is not None:
marker_info = marker_match.group().lstrip('(').rstrip(')').split('x')
m_len, m_rep = map(int, marker_info)
m_start, m_end = marker_match.span()
uncompressed_len += m_start + m_len * m_rep
start += m_end + m_len
marker_match = re.search(search_term, compressed[start:])
uncompressed_len += len(compressed) - start
return uncompressed_len
def get_len_compression2(compressed):
start = 0
uncompressed_len = 0
marker_match = re.search(search_term, compressed[start:])
while marker_match is not None:
marker_info = marker_match.group().lstrip('(').rstrip(')').split('x')
m_len, m_rep = map(int, marker_info)
m_start, m_end = marker_match.span()
uncompressed_len += m_start + get_len_compression2(compressed[start + m_end:start + m_end + m_len]) * m_rep
start += m_end + m_len
marker_match = re.search(search_term, compressed[start:])
uncompressed_len += len(compressed) - start
return uncompressed_len
day = int(os.path.basename(__file__).replace('day', '').replace('.py', ''))
data = get_input.get_input(day)
output_len_1 = get_len_compression(data)
output_len_2 = get_len_compression2(data)
print('Decompressed length: {}'.format(output_len_1))
print('Decompressed length using compression v2: {}'.format(output_len_2))