-
Notifications
You must be signed in to change notification settings - Fork 1
/
14.py
48 lines (32 loc) · 855 Bytes
/
14.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
from collections import Counter
import fileinput
def diff_single_letters(counter):
single = Counter()
for k, v in counter.items():
single[k[0]] += v
single[start[-1]] += 1
return single.most_common()[0][1] - single.most_common()[-1][1]
start = None
rules = {}
for l in fileinput.input():
l = l.strip()
if start == None:
start = list(l)
continue
if l == '':
continue
s = l.split(' -> ')
rules[s[0]] = s[1]
# rules.append((s[0], s[1]))
res = list(start)
c = Counter( (a+b) for a,b in zip(res, res[1:]))
for i in range(40):
cc = Counter()
for pair, cnt in c.items():
middle = rules[pair]
cc[pair[0] + middle] += cnt
cc[middle + pair[1]] += cnt
c = cc
if i == 9:
print(diff_single_letters(c))
print(diff_single_letters(c))