From a877bc5bedea641ee80497971239d8ad86b470ca Mon Sep 17 00:00:00 2001 From: setop Date: Sun, 2 Jan 2022 23:35:19 +0100 Subject: [PATCH] day 14 --- d14/d14_1.py | 22 +++++++++++ d14/d14_2.py | 27 +++++++++++++ d14/dograph.py | 9 +++++ d14/input | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ d14/sample | 18 +++++++++ 5 files changed, 178 insertions(+) create mode 100644 d14/d14_1.py create mode 100644 d14/d14_2.py create mode 100644 d14/dograph.py create mode 100644 d14/input create mode 100644 d14/sample diff --git a/d14/d14_1.py b/d14/d14_1.py new file mode 100644 index 0000000..41e6805 --- /dev/null +++ b/d14/d14_1.py @@ -0,0 +1,22 @@ +import sys +L = sys.stdin.read().splitlines() +seed = L[0] +L = L[2:] +L = [ l.split() for l in L ] +D = { tuple(k):v for [k,_,v] in L } +print(seed, D) +for step in range(10): + C = ["X"] # X is their to avoid empty list when comparing to previous char + for (a,b) in zip(seed, seed[1:]): + if a != C[-1]: + C.append(a) + if (a,b) in D: + C.append(D[(a,b)]) + C.append(b) + seed = "".join(C) + print(step+1, len(seed), seed[1:]) +from collections import Counter +C = Counter(seed) +del C["X"] +print(C, C.values(), max(C.values())-min(C.values())) +# store 40 iterations will be 20*2^39~=21TB ... diff --git a/d14/d14_2.py b/d14/d14_2.py new file mode 100644 index 0000000..fcf2422 --- /dev/null +++ b/d14/d14_2.py @@ -0,0 +1,27 @@ +import sys +L = sys.stdin.read().splitlines() +seed = L[0] +L = L[2:] +L = [ l.split() for l in L ] +D = { tuple(k):v for [k,_,v] in L } +def reprpairs(P): + return " ".join(f"{a}{b}:{v}" for ((a,b),v) in P.items()) +E = { K:0 for (K,_) in D.items() } +for (a,b) in zip(seed, seed[1:]): + E[(a,b)] += 1 +#print(reprpairs(E)) +for step in range(40): + F = { K:0 for (K,_) in D.items() } + for ((a,b),v) in E.items(): + j = D[(a,b)] + F[(a,j)] += v + F[(j,b)] += v + E = F + #print(step+1,reprpairs(E)) +from collections import defaultdict as dfd +S = dfd(int) +for ((a,b),v) in E.items(): + S[a]+=v + S[b]+=v +S ={ k:(a+b) for (k,v) in S.items() for (a,b) in [divmod(v,2)] } +print(S, max(S.values())-min(S.values())) diff --git a/d14/dograph.py b/d14/dograph.py new file mode 100644 index 0000000..e5210a8 --- /dev/null +++ b/d14/dograph.py @@ -0,0 +1,9 @@ +import sys +L = sys.stdin.read().splitlines() +L = L[2:] +L = [ l.split() for l in L ] +D = { tuple(k):v for [k,_,v] in L } +print('digraph G {') +for ((k,l),v) in D.items(): + print(f'{k} -> {v} [label="{l}"];') +print("}") \ No newline at end of file diff --git a/d14/input b/d14/input new file mode 100644 index 0000000..3134b4a --- /dev/null +++ b/d14/input @@ -0,0 +1,102 @@ +SVKVKCCBNHNSOSCCOPOC + +BB -> O +BC -> F +BF -> O +BH -> O +BK -> B +BN -> F +BO -> S +BP -> O +BS -> O +BV -> O +CB -> V +CC -> K +CF -> N +CH -> C +CK -> K +CN -> P +CO -> H +CP -> O +CS -> P +CV -> V +FB -> H +FC -> F +FF -> V +FH -> N +FK -> N +FN -> P +FO -> V +FP -> B +FS -> V +FV -> K +HB -> B +HC -> S +HF -> P +HH -> B +HK -> S +HN -> V +HO -> V +HP -> O +HS -> B +HV -> H +KB -> C +KC -> C +KF -> B +KH -> B +KK -> B +KN -> H +KO -> S +KP -> S +KS -> N +KV -> K +NB -> P +NC -> S +NF -> H +NH -> B +NK -> N +NN -> K +NO -> N +NP -> B +NS -> P +NV -> F +OB -> V +OC -> V +OF -> H +OH -> C +OK -> O +ON -> P +OO -> V +OP -> F +OS -> H +OV -> F +PB -> H +PC -> B +PF -> P +PH -> K +PK -> F +PN -> B +PO -> N +PP -> V +PS -> S +PV -> B +SB -> C +SC -> C +SF -> K +SH -> S +SK -> H +SN -> P +SO -> C +SP -> P +SS -> K +SV -> B +VB -> S +VC -> P +VF -> S +VH -> V +VK -> S +VN -> V +VO -> N +VP -> F +VS -> S +VV -> O \ No newline at end of file diff --git a/d14/sample b/d14/sample new file mode 100644 index 0000000..6c1c3a1 --- /dev/null +++ b/d14/sample @@ -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 \ No newline at end of file