This commit is contained in:
dave 2021-12-13 21:43:08 -08:00
parent 6fdcd6687f
commit 103ee891b2
4 changed files with 209 additions and 0 deletions

41
14/a.py Normal file
View File

@ -0,0 +1,41 @@
from collections import defaultdict
rules = {}
# with open("sample.txt") as f:
with open("input.txt") as f:
start_poly = f.readline().strip()
f.readline()
for line in f.readlines():
l, r = line.strip().split(" -> ")
rules[l] = r
def mutate(poly):
new_poly = []
for i in range(0, len(poly)):
a = poly[i]
if i == len(poly) - 1:
new_poly.append(a)
break
b = poly[i + 1]
pair = a + b
new_poly.append(a)
new_poly.append(rules[pair])
return ''.join(new_poly)
for i in range(0, 10):
start_poly = mutate(start_poly)
counts = defaultdict(lambda: 0)
for c in start_poly:
counts[c] += 1
counts = sorted(counts.values())
print(counts[-1] - counts[0])

48
14/b.py Normal file
View File

@ -0,0 +1,48 @@
from collections import defaultdict
rules = {}
# with open("sample.txt") as f:
with open("input.txt") as f:
sp = list(f.readline().strip())
f.readline()
for line in f.readlines():
l, r = line.strip().split(" -> ")
rules[l] = r
start_poly = defaultdict(lambda: 0)
for i in range(0, len(sp) - 1):
start_poly[sp[i] + sp[i + 1]] += 1
def mutate(poly):
new_poly = defaultdict(lambda: 0)
for pair, count in poly.items():
new_c = rules[pair]
np1 = pair[0] + new_c
np2 = new_c + pair[1]
new_poly[np1] += count
new_poly[np2] += count
return new_poly
def get_counts(d):
counts = defaultdict(lambda: 0)
for chars, count in d.items():
counts[chars[0]] += count # only count 1st letter, the second was duplicated in a later pair
# except the last char in the original sequence
counts[sp[-1]] += 1
return dict(counts)
for i in range(0, 40):
start_poly = mutate(start_poly)
counts = sorted(get_counts(start_poly).values())
print(counts[-1] - counts[0])

102
14/input.txt Normal file
View File

@ -0,0 +1,102 @@
BNBBNCFHHKOSCHBKKSHN
CH -> S
KK -> V
FS -> V
CN -> P
VC -> N
CB -> V
VK -> H
CF -> N
PO -> O
KC -> S
HC -> P
PP -> B
KO -> B
BK -> P
BH -> N
CC -> N
PC -> O
FK -> N
KF -> F
FH -> S
SS -> V
ON -> K
OV -> K
NK -> H
BO -> C
VP -> O
CS -> V
KS -> K
SK -> B
OP -> S
PK -> S
HF -> P
SV -> P
SB -> C
BC -> C
FP -> H
FC -> P
PB -> N
NV -> F
VO -> F
VH -> P
BB -> N
SF -> F
NB -> K
KB -> S
VV -> S
NP -> N
SO -> O
PN -> B
BP -> H
BV -> V
OB -> C
HV -> N
PF -> B
SP -> N
HN -> N
CV -> H
BN -> V
PS -> V
CO -> S
BS -> N
VB -> H
PV -> P
NN -> P
HS -> C
OS -> P
FB -> S
HO -> C
KH -> H
HB -> K
VF -> S
CK -> K
FF -> H
FN -> P
OK -> F
SC -> B
HH -> N
OH -> O
VS -> N
FO -> N
OC -> H
NF -> F
PH -> S
HK -> K
NH -> H
FV -> S
OF -> V
NC -> O
HP -> O
KP -> B
BF -> N
NO -> S
CP -> C
NS -> N
VN -> K
KV -> N
OO -> V
SN -> O
KN -> C
SH -> F

18
14/sample.txt Normal file
View File

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C