Skip to content

Commit 1e41e2a

Browse files
committed
Day 14 in Python
1 parent 9971b04 commit 1e41e2a

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

day14.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
from collections import Counter
3+
4+
def grow(template, formula, steps=10):
5+
characterCounter = Counter(template)
6+
7+
pairs = [ a+b for a,b in zip(template, template[1:])]
8+
pairCounter = Counter(pairs)
9+
10+
for step in range(steps):
11+
stepCounter = Counter()
12+
for pair, count in pairCounter.items():
13+
# Count the character being inserted
14+
middleChar = formula[pair]
15+
characterCounter[middleChar] += count
16+
17+
# Determine newly created pairs and count them, too
18+
leftPair = pair[0] + middleChar
19+
rightPair = middleChar + pair[1]
20+
stepCounter[leftPair] += count
21+
stepCounter[rightPair] += count
22+
23+
pairCounter = stepCounter
24+
return characterCounter
25+
26+
def main(args = ()):
27+
fileName = "day14.txt" if len(args) < 1 else args[0]
28+
29+
formula = {}
30+
template = ""
31+
with open(fileName) as lines:
32+
for line in lines:
33+
line = line.strip()
34+
if line and "->" not in line:
35+
template = line
36+
elif "->" in line:
37+
pair, insert = line.split(" -> ")
38+
formula[pair] = insert
39+
40+
for part, steps in enumerate((10, 40)):
41+
counter = grow(template, formula, steps)
42+
mc = counter.most_common()
43+
print(f"Part {part+1}:", mc[0][1] - mc[-1][1])
44+
45+
if __name__ == "__main__":
46+
sys.exit(main(sys.argv[1:]))

day14.txt

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
SVKVKCCBNHNSOSCCOPOC
2+
3+
KK -> B
4+
CS -> P
5+
VV -> O
6+
KO -> S
7+
PO -> N
8+
PH -> K
9+
BV -> O
10+
VH -> V
11+
PF -> P
12+
HB -> B
13+
OB -> V
14+
FC -> F
15+
OS -> H
16+
NB -> P
17+
SH -> S
18+
KV -> K
19+
SO -> C
20+
NP -> B
21+
NV -> F
22+
CP -> O
23+
KS -> N
24+
FP -> B
25+
VN -> V
26+
NC -> S
27+
FH -> N
28+
CB -> V
29+
PV -> B
30+
NH -> B
31+
NF -> H
32+
PC -> B
33+
NO -> N
34+
CN -> P
35+
KF -> B
36+
VF -> S
37+
CC -> K
38+
CF -> N
39+
PS -> S
40+
NK -> N
41+
PB -> H
42+
BP -> O
43+
FK -> N
44+
BO -> S
45+
OH -> C
46+
VB -> S
47+
VP -> F
48+
FO -> V
49+
KB -> C
50+
SK -> H
51+
CO -> H
52+
HV -> H
53+
SV -> B
54+
BF -> O
55+
SS -> K
56+
VK -> S
57+
HS -> B
58+
HF -> P
59+
PK -> F
60+
BS -> O
61+
BB -> O
62+
VC -> P
63+
OP -> F
64+
NS -> P
65+
SB -> C
66+
NN -> K
67+
HC -> S
68+
HH -> B
69+
FN -> P
70+
OO -> V
71+
VO -> N
72+
ON -> P
73+
FV -> K
74+
HK -> S
75+
FS -> V
76+
HO -> V
77+
PN -> B
78+
KH -> B
79+
CH -> C
80+
KP -> S
81+
BH -> O
82+
BK -> B
83+
FB -> H
84+
VS -> S
85+
HP -> O
86+
SP -> P
87+
OV -> F
88+
OF -> H
89+
OC -> V
90+
KN -> H
91+
BC -> F
92+
BN -> F
93+
CK -> K
94+
SN -> P
95+
SF -> K
96+
KC -> C
97+
SC -> C
98+
HN -> V
99+
OK -> O
100+
FF -> V
101+
CV -> V
102+
PP -> V

day14sample.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
NNCB
3+
4+
CH -> B
5+
HH -> N
6+
CB -> H
7+
NH -> C
8+
HB -> C
9+
HC -> B
10+
HN -> C
11+
NN -> C
12+
BH -> H
13+
NC -> B
14+
NB -> B
15+
BN -> B
16+
BB -> N
17+
BC -> B
18+
CC -> N
19+
CN -> C

0 commit comments

Comments
 (0)