-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
70 lines (61 loc) · 1.88 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
import re
from collections import defaultdict
from collections import Counter
m= lambda s: re.findall(r'\d+',s)
with open('data/test/14.test') as f:
testlines = [ line.strip() for line in f]
with open('data/my_input/14.in') as f:
lines = [ line.strip() for line in f]
def part1(v,loop):
d=dict()
l=list()
input=v[0]
for i,j in enumerate(v[2:]):
a,b,c = j.split()
d[a]=c
for i in range(loop):
newinput=""
size=len(input)
for j in range(1,size):
pair=input[j-1]+input[j]
if pair in d:
newinput+=input[j-1]+d[pair]
else:
newinput+=input[j-1]
newinput+=input[size-1]
input=newinput
return max([v for k,v in Counter(input).items()])-min([v for k,v in Counter(input).items()])
def part2(v,loop):
d=dict()
comptdict=defaultdict(int)
input=v[0]
for i,j in enumerate(v[2:]):
a,b,c = j.split()
d[a]=c
size=len(input)
l=list()
for k in range(1,size):
l.append(input[k-1]+input[k])
for _,j in enumerate(l):
comptdict[j]+=1
for _ in range(loop-1):
newcomptdict=defaultdict(int)
for k,v in comptdict.items():
pair1=k[0]+d[k]
pair2=d[k]+k[1]
newcomptdict[pair1]+=v
newcomptdict[pair2]+=v
comptdict=newcomptdict
countletter=defaultdict(int)
for k,v in comptdict.items():
countletter[k[0]]+=v
countletter[d[k]]+=v
for _,j in enumerate(input):
countletter[j]+=1
for _,j in enumerate(input[1:-1]):
countletter[j]-=1
return max([v for k,v in countletter.items()])-min([v for k,v in countletter.items()])
print("part1 test output",part1(testlines,10))
print("part1 my output",part1(lines,10))
print("part1 test output",part2(testlines,40))
print("part1 my output",part2(lines,40))