68
day14/day14.py
Executable file
68
day14/day14.py
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
from collections import defaultdict, Counter
|
||||||
|
from typing import DefaultDict, Mapping
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
|
def apply_rules(template: str, rules: Mapping[str, str]) -> str:
|
||||||
|
# sliding window babey
|
||||||
|
new = ""
|
||||||
|
for i in range(len(template) - 1):
|
||||||
|
new += template[i] + rules[template[i : i + 2]]
|
||||||
|
return new + template[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def part1(template: str, rules: Mapping[str, str]):
|
||||||
|
for i in range(10):
|
||||||
|
template = apply_rules(template, rules)
|
||||||
|
counts: DefaultDict[str, int] = defaultdict(lambda: 0)
|
||||||
|
for c in template:
|
||||||
|
counts[c] += 1
|
||||||
|
counts_sorted = sorted(counts.items(), key=lambda pair: pair[1])
|
||||||
|
_, least = counts_sorted[0]
|
||||||
|
_, most = counts_sorted[-1]
|
||||||
|
print(most - least)
|
||||||
|
|
||||||
|
|
||||||
|
def part2(template: str, rules: Mapping[str, str]):
|
||||||
|
# Apply rules with counts instead of building a string
|
||||||
|
counts: Counter = Counter()
|
||||||
|
|
||||||
|
for i in range(len(template) - 1):
|
||||||
|
counts[template[i : i + 2]] += 1
|
||||||
|
|
||||||
|
for _ in range(40):
|
||||||
|
new_counts: Counter = Counter()
|
||||||
|
for pair, count in counts.items():
|
||||||
|
# Create the two new pairs, and remove the old one
|
||||||
|
rule = rules[pair]
|
||||||
|
p1 = pair[0] + rule
|
||||||
|
p2 = rule + pair[1]
|
||||||
|
# Subtract from this pair, and then add the new ones
|
||||||
|
new_counts[p1] += counts[pair]
|
||||||
|
new_counts[p2] += counts[pair]
|
||||||
|
counts = new_counts
|
||||||
|
# Count all the letters, only using the first character.
|
||||||
|
# If we count the second character, we double-count.
|
||||||
|
letters: Counter = Counter()
|
||||||
|
for pair in counts:
|
||||||
|
letters[pair[0]] += counts[pair]
|
||||||
|
# We just need to count the last character in the original template - since this never changes.
|
||||||
|
letters[template[-1]] += 1
|
||||||
|
print(max(letters.values()) - min(letters.values()))
|
||||||
|
|
||||||
|
|
||||||
|
template = sys.stdin.readline().strip()
|
||||||
|
# blank
|
||||||
|
sys.stdin.readline()
|
||||||
|
rules = {
|
||||||
|
rule: translate.strip()
|
||||||
|
for rule, translate in map(lambda line: line.split(" -> "), sys.stdin)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
print("Part 1")
|
||||||
|
part1(template, rules)
|
||||||
|
print("Part 2")
|
||||||
|
part2(template, rules)
|
||||||
18
day14/example.txt
Normal file
18
day14/example.txt
Normal 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
|
||||||
102
day14/input.txt
Normal file
102
day14/input.txt
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
SHHBNFBCKNHCNOSHHVFF
|
||||||
|
|
||||||
|
CK -> N
|
||||||
|
VP -> B
|
||||||
|
CF -> S
|
||||||
|
FO -> V
|
||||||
|
VC -> S
|
||||||
|
BV -> V
|
||||||
|
NP -> P
|
||||||
|
SN -> C
|
||||||
|
KN -> V
|
||||||
|
NF -> P
|
||||||
|
SB -> C
|
||||||
|
PC -> B
|
||||||
|
OB -> V
|
||||||
|
NS -> O
|
||||||
|
FH -> S
|
||||||
|
NK -> S
|
||||||
|
HO -> V
|
||||||
|
NV -> O
|
||||||
|
FV -> O
|
||||||
|
FB -> S
|
||||||
|
PS -> S
|
||||||
|
FN -> K
|
||||||
|
HS -> O
|
||||||
|
CB -> K
|
||||||
|
HV -> P
|
||||||
|
NH -> C
|
||||||
|
BO -> B
|
||||||
|
FF -> N
|
||||||
|
PO -> F
|
||||||
|
BB -> N
|
||||||
|
PN -> C
|
||||||
|
BP -> C
|
||||||
|
HN -> K
|
||||||
|
CO -> P
|
||||||
|
BF -> H
|
||||||
|
BC -> S
|
||||||
|
CV -> B
|
||||||
|
VV -> F
|
||||||
|
FS -> B
|
||||||
|
BN -> P
|
||||||
|
VK -> S
|
||||||
|
PV -> V
|
||||||
|
PP -> B
|
||||||
|
PH -> N
|
||||||
|
SS -> O
|
||||||
|
SK -> S
|
||||||
|
NC -> P
|
||||||
|
ON -> F
|
||||||
|
NB -> N
|
||||||
|
CC -> N
|
||||||
|
SF -> H
|
||||||
|
PF -> H
|
||||||
|
OV -> O
|
||||||
|
KH -> C
|
||||||
|
CP -> V
|
||||||
|
PK -> O
|
||||||
|
KC -> K
|
||||||
|
KK -> C
|
||||||
|
KF -> B
|
||||||
|
HP -> C
|
||||||
|
FK -> H
|
||||||
|
BH -> K
|
||||||
|
VN -> H
|
||||||
|
OO -> S
|
||||||
|
SC -> K
|
||||||
|
SP -> B
|
||||||
|
KO -> V
|
||||||
|
KV -> F
|
||||||
|
HK -> N
|
||||||
|
FP -> N
|
||||||
|
NN -> B
|
||||||
|
VS -> O
|
||||||
|
HC -> K
|
||||||
|
BK -> N
|
||||||
|
KS -> K
|
||||||
|
VB -> O
|
||||||
|
OH -> F
|
||||||
|
KB -> F
|
||||||
|
KP -> H
|
||||||
|
HB -> N
|
||||||
|
NO -> N
|
||||||
|
OF -> O
|
||||||
|
BS -> H
|
||||||
|
VO -> H
|
||||||
|
SH -> O
|
||||||
|
SV -> K
|
||||||
|
HF -> C
|
||||||
|
CS -> F
|
||||||
|
FC -> N
|
||||||
|
VH -> H
|
||||||
|
OP -> K
|
||||||
|
OK -> H
|
||||||
|
PB -> K
|
||||||
|
HH -> S
|
||||||
|
OC -> V
|
||||||
|
VF -> B
|
||||||
|
CH -> K
|
||||||
|
CN -> C
|
||||||
|
SO -> P
|
||||||
|
OS -> O
|
||||||
Reference in New Issue
Block a user