Add solution for day 14
This commit is contained in:
1 parent
ac4b409df4
commit
c80e170137
4 files changed
+234
No files matched your search
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
from collections import Counter
|
||||
from copy import deepcopy
|
||||
|
||||
def step(cons,s):
|
||||
for _ in range(s):
|
||||
ncons = {}
|
||||
for (a,b),v in cons.items():
|
||||
if (a, rules[a+b]) not in ncons: ncons[(a, rules[a+b])] = 0
|
||||
if (rules[a+b], b) not in ncons: ncons[(rules[a+b], b)] = 0
|
||||
ncons[(a, rules[a+b])] += v
|
||||
ncons[(rules[a+b],b)] += v
|
||||
cons = deepcopy(ncons)
|
||||
return cons
|
||||
|
||||
def countp(cons):
|
||||
ex = {}
|
||||
for k in nex.keys():
|
||||
ex[k[0]] = 0
|
||||
ex[k[1]] = 0
|
||||
ex[start[0]] += 1
|
||||
ex[start[-1]] += 1
|
||||
for k,v in nex.items():
|
||||
ex[k[0]] += v
|
||||
ex[k[1]] += v
|
||||
return ex
|
||||
|
||||
if __name__ == '__main__':
|
||||
start, rules = open(sys.argv[1]).read().split('\n\n')
|
||||
rules = [r.split(' -> ') for r in rules.split('\n')]
|
||||
rules = {a[0]:a[1] for a in rules[:-1]}
|
||||
nex = {}
|
||||
for a,b in zip(start[:-1],start[1:]):
|
||||
if (a,b) not in nex: nex[(a,b)] = 0
|
||||
nex[(a,b)] +=1
|
||||
# challenge 1
|
||||
nex = step(nex,10)
|
||||
res = countp(nex)
|
||||
res1 = str(max(res.values())//2-min(res.values())//2)
|
||||
print("challenge 1:" +"\n" + res1 + "\n")
|
||||
|
||||
# challenge 2
|
||||
nex = step(nex,30)
|
||||
res = countp(nex)
|
||||
res2 = str(max(res.values())//2-min(res.values())//2)
|
||||
print("challenge 2:" +"\n" + res2 + "\n")
|
||||
|
||||
Reference in new issue
Block a user