-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.py
46 lines (32 loc) · 1.14 KB
/
part1.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
from collections import Counter
def solution(inp, steps):
init = inp[0]
sequences = {line[0] + line[1]: line[6] for line in inp if '->' in line}
counts = Counter()
for i in range(len(init) - 1):
current = init[i]
next = init[i + 1]
counts[current + next] += 1
for x in range(steps):
new_counts = counts.copy()
for count in counts:
if count in sequences:
new_counts[count[0] + sequences[count]] += counts[count]
new_counts[sequences[count] + count[1]] += counts[count]
new_counts[count] -= counts[count]
counts = new_counts
beginnings = Counter()
ends = Counter()
for count in counts:
beginnings[count[0]] += counts[count]
ends[count[1]] += counts[count]
res = Counter()
for letter in set(beginnings).union(ends):
count = max(beginnings.get(letter, 0), ends.get(letter, 0))
res[letter] = count
ordered = res.most_common()
return ordered[0][1] - ordered[-1][1]
def result(inp):
return solution(inp, 10)
def test(example_inp):
assert result(example_inp) == 1588