Add day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-14 06:52:55 +01:00
parent 03633c432f
commit 739cb2a318
3 changed files with 176 additions and 0 deletions

73
day-14/day-14.py Normal file
View File

@@ -0,0 +1,73 @@
#!/usr/bin/env python3
from pathlib import Path
from collections import Counter, defaultdict
def part_1(input):
result = 0
template = input.pop(0).strip()
input.pop(0)
rules = {k.strip(): v.strip()
for line in input for k, v in [line.strip().split('->')]}
counts = defaultdict(int)
for i in range(len(template) - 1):
counts[template[i: i + 2]] += 1
for _ in range(10):
tmp_cnts = counts.copy()
for k in tmp_cnts:
if k in rules:
c = tmp_cnts[k]
counts[k] -= c
counts[k[0] + rules[k]] += c
counts[rules[k] + k[1]] += c
char_counts = defaultdict(int)
for k in counts:
char_counts[k[0]] += counts[k]
char_counts[k[1]] += counts[k]
char_counts[template[0]] += 1
char_counts[template[-1]] += 1
for k in char_counts:
char_counts[k] //= 2
char_counts = sorted(char_counts.items(),
key=lambda kv: kv[1])
result = char_counts[-1][1] - char_counts[0][1]
print("Part 1 result:", result)
def part_2(input):
result = 0
template = input.pop(0).strip()
input.pop(0)
rules = {k.strip(): v.strip()
for line in input for k, v in [line.strip().split('->')]}
counts = defaultdict(int)
for i in range(len(template) - 1):
counts[template[i: i + 2]] += 1
for _ in range(40):
tmp_cnts = counts.copy()
for k in tmp_cnts:
if k in rules:
c = tmp_cnts[k]
counts[k] -= c
counts[k[0] + rules[k]] += c
counts[rules[k] + k[1]] += c
char_counts = defaultdict(int)
for k in counts:
char_counts[k[0]] += counts[k]
char_counts[k[1]] += counts[k]
char_counts[template[0]] += 1
char_counts[template[-1]] += 1
for k in char_counts:
char_counts[k] //= 2
char_counts = sorted(char_counts.items(),
key=lambda kv: kv[1])
result = char_counts[-1][1] - char_counts[0][1]
print("Part 2 result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input.copy())
part_2(input.copy())

102
day-14/input.txt Normal file
View File

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