-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
100 lines (79 loc) · 2.77 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from collections import Counter
def part1():
with open('resources/input14.txt') as f:
lines = f.readlines()
template = lines[0].strip()
insertions = {}
for line in lines:
line = line.strip()
if "->" in line:
split = line.split(' -> ')
insertions[split[0]] = split[1]
for _ in range(0, 10):
template = insert(insertions, template)
counter = Counter(template)
most_common = counter.most_common()
print(most_common[0][1] - most_common.pop()[1])
def insert(insertions, template):
new_template = []
prev_char = ' '
for char in template:
char_pair = prev_char + char
if char_pair in insertions:
new_template.append(insertions[char_pair])
new_template.append(char)
prev_char = char
return new_template
def part2():
with open('resources/input14.txt') as f:
lines = f.readlines()
template = lines[0].strip()
template_pairs = {}
prev_char = None
for char in template:
if prev_char is not None:
char_pair = prev_char + char
if char_pair in template_pairs:
template_pairs[char_pair] +=1
else:
template_pairs[char_pair] = 1
prev_char = char
insertions = {}
for line in lines:
line = line.strip()
if "->" in line:
split = line.split(' -> ')
insertions[split[0]] = (split[0][0] + split[1], split[1] + split[0][1])
for _ in range(0, 40):
template_pairs = insert2(insertions, template_pairs)
print(template_pairs)
counts = calculate_counts(template_pairs, template[0], template[len(template)-1])
sorted_counts = sorted(counts.values())
print(abs(sorted_counts[0] - sorted_counts.pop()))
def insert2(insertions, template_pairs):
new_template_pairs = {}
for entry in template_pairs:
if entry in insertions:
increment(insertions[entry][0], new_template_pairs, template_pairs[entry])
increment(insertions[entry][1], new_template_pairs, template_pairs[entry])
else:
increment(entry, new_template_pairs, 1)
return new_template_pairs
def increment(key, dict, count):
if key in dict:
dict[key] += count
else:
dict[key] = count
def calculate_counts(template_pairs, start, end):
counts = {}
for entry in template_pairs:
for char in entry:
increment(char, counts, template_pairs[entry])
counts[start] += 1
counts[end] += 1
for count in counts:
counts[count] /= 2
return counts
if __name__ == '__main__':
part1()
part2()