Skip to content

Commit 93be29a

Browse files
committed
Day 14, Part 1
1 parent 255d321 commit 93be29a

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

day_14/__init__.py

Whitespace-only changes.

day_14/__main__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import defaultdict
2+
from typing import TextIO, Dict, Tuple
3+
import sys
4+
from operator import itemgetter
5+
6+
def read_input(textio: TextIO) -> Tuple[str, Dict[str, str]]:
7+
lines = textio.readlines()
8+
template = lines[0].strip()
9+
rules = dict()
10+
11+
for line in lines[2:]:
12+
pair, inserted = line.strip().split(' -> ')
13+
rules[pair] = inserted
14+
15+
return template, rules
16+
17+
def step(template: str, rules: Dict[str, str]) -> str:
18+
out = ""
19+
for i in range(len(template)-1):
20+
pair = template[i:i+2]
21+
out += pair[0]
22+
out += rules[pair]
23+
out += template[-1]
24+
return out
25+
26+
def stats(template: str) -> Tuple[int, int]:
27+
counts = defaultdict(lambda: 0)
28+
for char in template:
29+
counts[char] += 1
30+
31+
return min(counts.items(), key=itemgetter(1))[1], max(counts.items(), key=itemgetter(1))[1]
32+
33+
template, rules = read_input(sys.stdin)
34+
for i in range(10):
35+
template = step(template, rules)
36+
37+
least_common, most_common = stats(template)
38+
print(most_common-least_common)

day_14/input

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

day_14/test

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

0 commit comments

Comments
 (0)