Added 2021 (python)
This commit is contained in:
102
2021/14/input
Normal file
102
2021/14/input
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
|
69
2021/14/prog.py
Normal file
69
2021/14/prog.py
Normal file
@ -0,0 +1,69 @@
|
||||
VERBOSE = True
|
||||
def verbose(s = "", *args, **kwargs):
|
||||
if VERBOSE:
|
||||
print(s, *args, **kwargs)
|
||||
|
||||
def get_input(sample = False, part = 1):
|
||||
with open(f'sample_p{part}' if sample else 'input', 'r') as f:
|
||||
tmp = f.readlines()
|
||||
ret = {"start": tmp[0].strip(), "alphabet": {}, "rules": {}}
|
||||
|
||||
for line in tmp[2:]:
|
||||
line = line.strip().split(" -> ")
|
||||
l, r = line[0], line[1]
|
||||
for c in l:
|
||||
ret["alphabet"][c] = 0
|
||||
ret["alphabet"][r] = 0
|
||||
ret["rules"][l] = r
|
||||
return ret
|
||||
|
||||
|
||||
def result(inp, part = 1, steps = 10, ret_len = False, ret_state = False):
|
||||
res = 0;
|
||||
alph = inp["alphabet"]
|
||||
rules = inp["rules"]
|
||||
state = {k: 0 for k in rules.keys()}
|
||||
|
||||
s = inp["start"]
|
||||
for c in s:
|
||||
alph[c] += 1
|
||||
|
||||
for i, c in enumerate(s[:-1]):
|
||||
state[s[i] + s[i+1]] += 1
|
||||
|
||||
|
||||
# for k, v in state.items():
|
||||
# if v > 0:
|
||||
# verbose(k)
|
||||
|
||||
for _ in range(steps):
|
||||
verbose(f"step {_}")
|
||||
if _ == 3:
|
||||
verbose(sum(alph.values()))
|
||||
for k, v in state.items():
|
||||
if v > 0:
|
||||
verbose(k, v)
|
||||
new_state = {k: 0 for k in rules.keys()}
|
||||
for k, v in state.items():
|
||||
if v > 0:
|
||||
r = rules[k]
|
||||
new_state[k[0] + r] += v
|
||||
new_state[r + k[1]] += v
|
||||
verbose(f"\tadding {r}")
|
||||
alph[r] += v
|
||||
state = new_state.copy()
|
||||
|
||||
verbose(f"{len(alph) = }")
|
||||
|
||||
res = max(alph.values())- min(alph.values())
|
||||
if ret_state:
|
||||
return {k: v for k, v in state.items() if v > 0}
|
||||
if ret_len:
|
||||
return sum(alph.values())
|
||||
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
VERBOSE = False
|
||||
print(result(get_input(), part = 2, steps = 40))
|
18
2021/14/sample_p1
Normal file
18
2021/14/sample_p1
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
|
32
2021/14/test.py
Normal file
32
2021/14/test.py
Normal file
@ -0,0 +1,32 @@
|
||||
from prog import *
|
||||
import unittest
|
||||
|
||||
def res(s):
|
||||
ret = {}
|
||||
for i in range(len(s[:-1])):
|
||||
sub = s[i] + s[i+1]
|
||||
if sub in ret:
|
||||
ret[sub] += 1
|
||||
else:
|
||||
ret[sub] = 1
|
||||
return ret
|
||||
|
||||
class Tests(unittest.TestCase):
|
||||
def test_state(self):
|
||||
self.assertEqual(result(get_input(sample = True), steps = 1, ret_state = True), res("NCNBCHB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 2, ret_state = True), res("NBCCNBBBCBHCB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 3, ret_state = True), res("NBBBCNCCNBBNBNBBCHBHHBCHB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 4, ret_state = True), res("NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB"))
|
||||
|
||||
def test_len(self):
|
||||
self.assertEqual(result(get_input(sample = True), steps = 1, ret_len = True), len("NCNBCHB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 2, ret_len = True), len("NBCCNBBBCBHCB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 3, ret_len = True), len("NBBBCNCCNBBNBNBBCHBHHBCHB"))
|
||||
self.assertEqual(result(get_input(sample = True), steps = 4, ret_len = True), len("NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB"))
|
||||
|
||||
def test_part1(self):
|
||||
self.assertEqual(result(get_input(sample = True)), 1588)
|
||||
|
||||
def test_part2(self):
|
||||
self.assertEqual(result(get_input(sample = True), part = 2, steps = 40), 2188189693529)
|
||||
|
Reference in New Issue
Block a user