added day 14

This commit is contained in:
Peter Hudec 2021-12-14 15:59:23 +01:00
parent 0707c53b7b
commit d6179dc2e6
4 changed files with 219 additions and 0 deletions

102
14/input01.txt Normal file
View File

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

18
14/input01_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

36
14/solve01.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python
import re
RE_INS = re.compile("(.*)\s+->\s+(.*)$")
def iterate_polymer(polymer, rules):
result = polymer[0]
for i in range(0, len(polymer)-1):
k = polymer[i:i+2]
if k in rules.keys():
result = result + rules[k]+k[1]
else:
result = result+k[1]
return result
polymer = None
rules = dict()
with open("input01.txt","r") as f:
polymer = f. readline().strip()
for line in f:
line = line.strip()
if len(line) == 0:
continue
m = RE_INS.match(line)
if not m:
print(line)
rules[m.group(1)] = m.group(2)
for i in range(10):
polymer = iterate_polymer(polymer, rules)
count = [polymer.count(i) for i in set(polymer)]
print(max(count) - min(count))

63
14/solve02.py Normal file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env python
import re
from collections import defaultdict
RE_INS = re.compile("(.*)\s+->\s+(.*)$")
def tokenize_polymer(polymer):
result = defaultdict(int)
for i in range(0, len(polymer)-1):
k = polymer[i:i+2]
result[k] += 1
return result
def iterate_polymer(polymer, rules):
delta = defaultdict(int)
pairs = list(polymer.keys())
for pair in pairs:
if pair not in rules:
continue
if polymer[pair] == 0:
continue
p1 = pair[0]+rules[pair]
p2 = rules[pair]+pair[1]
delta[p1] += polymer[pair]
delta[p2] += polymer[pair]
delta[pair] -= polymer[pair]
for pair in delta:
polymer[pair] += delta[pair]
return polymer
polymer = None
rules = dict()
polymer_pairs = defaultdict(int)
with open("input01.txt","r") as f:
polymer = f. readline().strip()
for line in f:
line = line.strip()
if len(line) == 0:
continue
m = RE_INS.match(line)
if not m:
print(line)
rules[m.group(1)] = m.group(2)
for i in range(0, len(polymer)-1):
k = polymer[i:i+2]
polymer_pairs[k] += 1
polymer_pairs = tokenize_polymer(polymer)
for i in range(40):
polymer_pairs = iterate_polymer(polymer_pairs, rules)
# print(polymer_pairs)
sums = defaultdict(int)
for i in polymer_pairs.keys():
sums[i[1]] += polymer_pairs[i]
sums[polymer[0]] += 1
p_max = max([x for x in sums.values()])
p_min = min([x for x in sums.values()])
print(p_max - p_min)