This commit is contained in:
2022-11-28 23:51:45 +01:00
parent 61c98ad733
commit e1ab79cc94
4 changed files with 220 additions and 0 deletions

13
2021/14/README.md Normal file
View File

@@ -0,0 +1,13 @@
# 14
This was nice! Similar to the Lanterfish one. The naive way is to have a big string, but that becomes unfeasible quickly.
The solution was to keep track of the number of combinations available and at the end divide by 2 the counted elements. Rounding up so that the first and last element is not neglected as they are not doubled.
<details>
<summary>Solutions</summary>
<ol>
<li>2988</li>
<li>3572761917024</li>
</ol>
</details>

103
2021/14/input.txt Normal file
View File

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

86
2021/14/python/main.py Normal file
View File

@@ -0,0 +1,86 @@
#!/usr/bin/env python
import math
from collections import defaultdict
from os.path import dirname, join
# Day 14
# Common
def read_input(filename):
data = join(dirname(__file__), '..', filename)
with open(data) as f:
return f.read().strip()
test = read_input('test.txt')
data = read_input('input.txt')
class Polymer:
def __init__(self, state: str, insertions: dict[str, str]):
self.state = state
self.insertions = insertions
self.states: defaultdict[str, int] = defaultdict(lambda: 0)
for i in range(len(self) - 1):
pair = self.state[i:i+2]
self.states[pair] += 1
def __len__(self):
return len(self.state)
def step(self):
new = defaultdict(lambda: 0)
for state, count in self.states.items():
if count == 0:
continue
insertion = self.insertions[state]
for key in [state[0] + insertion, insertion + state[1]]:
new[key] += count
self.states = new
def score(self):
elements = defaultdict(lambda: 0)
for state, count in self.states.items():
for element in state:
elements[element] += count
for element, count in elements.items():
elements[element] = math.ceil(elements[element]/2)
return max(elements.values()) - min(elements.values())
def flag(self, steps: int):
for _ in range(steps):
self.step()
print(self.score())
@staticmethod
def parse(data: str):
state, raw = data.split('\n\n')
lines = [
l.split(' -> ')
for l in raw.split('\n')
]
insertions = {
i[0]: i[1]
for i in lines
}
return Polymer(state.strip(), insertions)
# 1
print('1.')
polymer = Polymer.parse(test)
polymer.flag(10)
polymer = Polymer.parse(data)
polymer.flag(10)
# 2
print('\n2.')
polymer = Polymer.parse(test)
polymer.flag(40)
polymer = Polymer.parse(data)
polymer.flag(40)

18
2021/14/test.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