-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
68 lines (53 loc) · 1.8 KB
/
day14.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from utils import read_data_file
from collections import Counter
from copy import deepcopy
lines = read_data_file(14, False)
template = lines[0].strip()
# part 1 - brute force
d_instructions = {}
for line in lines[2:]:
instruction = line.strip().split(' -> ')
d_instructions[instruction[0]] = instruction[1]
num_steps = 10
for step in range(num_steps):
new_template = ''
for s1, s2 in zip(template[:-1], template[1:]):
new_template += s1 + d_instructions[s1+s2]
template = new_template + template[-1]
# print(f'{step}: {template}')
counter = Counter(template)
max_val = max(Counter(template).values())
min_val = min(Counter(template).values())
print(max_val-min_val)
# part 2 - use conditions of couples
num_steps = 40
d_instructions = {}
template = lines[0].strip()
for line in lines[2:]:
instruction = line.strip().split(' -> ')
d_instructions[instruction[0]] = [instruction[0][0]+instruction[1], instruction[1]+instruction[0][1], 0]
org_d_instructions = deepcopy(d_instructions)
for s1, s2 in zip(template[:-1], template[1:]):
d_instructions[s1+s2][-1] += 1
for step in range(num_steps):
new_d_instructions = deepcopy(org_d_instructions)
for couple1, couple2, times in d_instructions.values():
new_d_instructions[couple1][-1] += times
new_d_instructions[couple2][-1] += times
d_instructions = deepcopy(new_d_instructions)
sums = {}
for key in d_instructions:
val = d_instructions[key][-1]
if key[0] in sums:
sums[key[0]] += val
else:
sums[key[0]] = val
if key[1] in sums:
sums[key[1]] += val
else:
sums[key[1]] = val
sums[template[0]] += 1
sums[template[-1]] += 1
max_val = max([value/2 for value in sums.values()])
min_val = min([value/2 for value in sums.values()])
print(int(max_val-min_val))