-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
76 lines (62 loc) · 1.96 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
import adventutils
def part1(file):
print("Part 1")
runpolymer(10, file)
def part2(file):
print("Part 2")
runpolymer(40, file)
def templateandpairs(contents):
template = contents[0]
pairs = dict()
for index in range(2, len(contents)):
pair, result = contents[index].split(" -> ")
pairs[pair] = result
return template, pairs
def recursivepolymer(template, pairs, depth):
counts = dict()
for index in range(len(template) - 1):
key = template[index] + template[index + 1]
if key in counts.keys():
counts[key] += 1
else:
counts[key] = 1
for _ in range(depth):
itercounts = dict()
for pair, count in counts.items():
firstkey = pair[0] + pairs[pair]
secondkey = pairs[pair] + pair[1]
if firstkey in itercounts.keys():
itercounts[firstkey] += count
else:
itercounts[firstkey] = count
if secondkey in itercounts.keys():
itercounts[secondkey] += count
else:
itercounts[secondkey] = count
counts = itercounts
finalcounts = dict()
for pair, count in counts.items():
if pair[0] in finalcounts:
finalcounts[pair[0]] += count
else:
finalcounts[pair[0]] = count
finalcounts[template[-1]] += 1
return finalcounts
def minmax(charcounts):
min = 1000000000000000
max = -1
for count in charcounts.values():
if count < min:
min = count
if count > max:
max = count
return min, max
def runpolymer(iterations, file):
contents = adventutils.file_contents(file)
template, pairs = templateandpairs(contents)
counts = recursivepolymer(template, pairs, iterations)
minimum, maximum = minmax(counts)
print(maximum - minimum)
if __name__ == "__main__":
part1("./data/day14part1.txt")
part2("./data/day14part1.txt")