~noelle/aoc-2021

b88fb16ec1eb8322eaf585cca6b23a6479ff331b — Noelle Leigh 2 years ago b29905d
14_1
4 files changed, 151 insertions(+), 0 deletions(-)

A 14/README.md
A 14/example.txt
A 14/input.txt
A 14/puzzle_1.py
A 14/README.md => 14/README.md +3 -0
@@ 0,0 1,3 @@
Day 14: Extended Polymerization

https://adventofcode.com/2021/day/14

A 14/example.txt => 14/example.txt +18 -0
@@ 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

A 14/input.txt => 14/input.txt +102 -0
@@ 0,0 1,102 @@
VCOPVNKPFOOVPVSBKCOF

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

A 14/puzzle_1.py => 14/puzzle_1.py +28 -0
@@ 0,0 1,28 @@
"""
Solution for AoC 2021 14 Puzzle 1

cat input.txt | python puzzle_1.py
"""
import operator
import sys
from functools import reduce
from itertools import pairwise
from typing import Counter

if __name__ == "__main__":
    template = next(sys.stdin).strip()
    # Skip blank line
    next(sys.stdin)
    rules = dict(map(lambda line: line.strip().split(" -> ", maxsplit=1), sys.stdin))

    polymer = template
    for step in range(1, 10 + 1):
        polymer = reduce(
            operator.add,
            map(lambda pair: rules["".join(pair)] + pair[1], pairwise(polymer)),
            polymer[0],
        )

    element_counts = Counter(polymer).most_common()
    value = element_counts[0][1] - element_counts[-1][1]
    sys.stdout.write(str(value))