14!
This commit is contained in:
55
14/14.py
Normal file
55
14/14.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
|
def part_x(template: str, pairs: list[tuple[str, str, str]], iterations=10) -> int:
|
||||||
|
duos = defaultdict(int)
|
||||||
|
|
||||||
|
for i in range(len(template) - 1):
|
||||||
|
duos[template[i : i + 2]] += 1
|
||||||
|
|
||||||
|
for i in range(iterations):
|
||||||
|
next_duos = defaultdict(int)
|
||||||
|
|
||||||
|
for duo, n in duos.items():
|
||||||
|
inserted = pairs[duo]
|
||||||
|
next_duos[duo[0] + inserted] += n
|
||||||
|
next_duos[inserted + duo[1]] += n
|
||||||
|
|
||||||
|
duos = next_duos
|
||||||
|
|
||||||
|
print(duos)
|
||||||
|
letter_counts = count_occurrences(duos, template)
|
||||||
|
return max(letter_counts.values()) - min(letter_counts.values())
|
||||||
|
|
||||||
|
|
||||||
|
def count_occurrences(duos: dict[str, int], template: str):
|
||||||
|
letter_counts = defaultdict(int)
|
||||||
|
|
||||||
|
letter_counts[template[0]] = 1
|
||||||
|
letter_counts[template[-1]] = 1
|
||||||
|
|
||||||
|
for duo, count in duos.items():
|
||||||
|
letter_counts[duo[0]] += count
|
||||||
|
letter_counts[duo[1]] += count
|
||||||
|
|
||||||
|
letter_counts = dict(map(lambda kv: (kv[0], kv[1] // 2), letter_counts.items()))
|
||||||
|
|
||||||
|
return letter_counts
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
template = sys.stdin.readline().strip()
|
||||||
|
sys.stdin.readline()
|
||||||
|
|
||||||
|
pairs = {}
|
||||||
|
|
||||||
|
for line in sys.stdin.readlines():
|
||||||
|
A = line[0:2]
|
||||||
|
C = line[6]
|
||||||
|
pairs[A] = C
|
||||||
|
|
||||||
|
print("Part One:", part_x(template, pairs))
|
||||||
|
print("Part Two:", part_x(template, pairs, 40))
|
||||||
18
14/ex14.in
Normal file
18
14/ex14.in
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
14/input14.in
Normal file
102
14/input14.in
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
ONSVVHNCFVBHKVPCHCPV
|
||||||
|
|
||||||
|
VO -> C
|
||||||
|
VV -> S
|
||||||
|
HK -> H
|
||||||
|
FC -> C
|
||||||
|
VB -> V
|
||||||
|
NO -> H
|
||||||
|
BN -> B
|
||||||
|
FP -> K
|
||||||
|
CS -> C
|
||||||
|
HC -> S
|
||||||
|
FS -> K
|
||||||
|
KH -> V
|
||||||
|
CH -> H
|
||||||
|
BP -> K
|
||||||
|
OF -> K
|
||||||
|
SS -> F
|
||||||
|
SP -> C
|
||||||
|
PN -> O
|
||||||
|
CK -> K
|
||||||
|
KS -> H
|
||||||
|
HO -> K
|
||||||
|
FV -> F
|
||||||
|
SN -> P
|
||||||
|
HN -> O
|
||||||
|
KK -> H
|
||||||
|
KP -> O
|
||||||
|
CN -> N
|
||||||
|
BO -> C
|
||||||
|
CC -> H
|
||||||
|
PB -> F
|
||||||
|
PV -> K
|
||||||
|
BV -> K
|
||||||
|
PP -> H
|
||||||
|
KB -> F
|
||||||
|
NC -> F
|
||||||
|
PC -> V
|
||||||
|
FN -> N
|
||||||
|
NH -> B
|
||||||
|
CF -> V
|
||||||
|
PO -> F
|
||||||
|
KC -> S
|
||||||
|
VP -> P
|
||||||
|
HH -> N
|
||||||
|
OB -> O
|
||||||
|
KN -> O
|
||||||
|
PS -> N
|
||||||
|
SF -> V
|
||||||
|
VK -> F
|
||||||
|
CO -> N
|
||||||
|
KF -> B
|
||||||
|
VC -> C
|
||||||
|
SH -> S
|
||||||
|
HV -> V
|
||||||
|
FK -> O
|
||||||
|
NV -> N
|
||||||
|
SC -> O
|
||||||
|
BK -> F
|
||||||
|
BB -> K
|
||||||
|
HF -> K
|
||||||
|
OC -> O
|
||||||
|
KO -> V
|
||||||
|
OS -> P
|
||||||
|
FF -> O
|
||||||
|
PH -> F
|
||||||
|
FB -> O
|
||||||
|
NN -> C
|
||||||
|
NK -> C
|
||||||
|
HP -> B
|
||||||
|
PF -> H
|
||||||
|
PK -> C
|
||||||
|
NP -> O
|
||||||
|
NS -> V
|
||||||
|
CV -> O
|
||||||
|
VH -> C
|
||||||
|
OP -> N
|
||||||
|
SO -> O
|
||||||
|
SK -> H
|
||||||
|
SV -> O
|
||||||
|
NF -> H
|
||||||
|
BS -> K
|
||||||
|
BH -> O
|
||||||
|
VN -> S
|
||||||
|
HB -> O
|
||||||
|
OH -> K
|
||||||
|
CB -> B
|
||||||
|
BC -> S
|
||||||
|
OV -> F
|
||||||
|
BF -> P
|
||||||
|
OO -> F
|
||||||
|
HS -> H
|
||||||
|
ON -> P
|
||||||
|
NB -> F
|
||||||
|
CP -> S
|
||||||
|
SB -> V
|
||||||
|
VF -> C
|
||||||
|
OK -> O
|
||||||
|
FH -> H
|
||||||
|
KV -> S
|
||||||
|
FO -> C
|
||||||
|
VS -> B
|
||||||
Reference in New Issue
Block a user