My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix solution for 2015/19

+16 -79
+16 -79
2015/day19.py
··· 1 1 import fileinput 2 - import itertools 3 2 import re 4 - import sys 5 3 6 4 REPLACEMENTS = [] 7 5 ELECTRONS = [] ··· 18 16 else: 19 17 MOLECULE = line.strip() 20 18 21 - print "Elements:", len(re.findall(r'[A-Z]', MOLECULE)) 22 - print "Rn/Ar:", len(re.findall(r'Rn', MOLECULE)), "* 2" 23 - print "Y:", len(re.findall(r'Y', MOLECULE)) 24 - 25 - # molecule = MOLECULE 26 - 27 - # reps = {m[1][::-1]: m[0][::-1] 28 - # for m in REPLACEMENTS} 29 - 30 - # def rep(x): 31 - # return reps[x.group()] 32 - 33 - # count = 0 34 - # while molecule != 'e': 35 - # molecule = re.sub('|'.join(reps.keys()), rep, molecule, 1) 36 - # count += 1 37 - 38 - # print(count) 39 - 40 - sys.exit() 41 - 42 - 43 - outs = set() 44 - 45 - def molefy(mole): 46 - for old, new in REPLACEMENTS: 47 - for pos in (m.start() for m in re.finditer(old, mole)): 48 - new_string = mole[:pos] + new + mole[pos+len(old):] 49 - yield new_string 50 - 51 - def delete(mole): 52 - for old, new in REPLACEMENTS: 53 - for pos in (m.start() for m in re.finditer(new, mole)): 54 - new_string = mole[:pos] + old + mole[pos+len(new):] 55 - yield new_string 56 19 57 20 def replace(old, new, s): 58 - for pos in (m.start() for m in re.finditer(new, s)): 59 - yield "{}{}{}".format(s[:pos], old, s[pos+len(new):]) 21 + for pos in (m.start() for m in re.finditer(old, s)): 22 + yield "{}{}{}".format(s[:pos], new, s[pos+len(old):]) 60 23 61 24 62 - LOWEST = 100000 25 + single_replacements = set() 63 26 64 - def submol(m, i=1,): 65 - if m in ELECTRONS: 66 - LOWEST = min(LOWEST, i) 27 + for old, new in REPLACEMENTS: 28 + if old in MOLECULE: 29 + for new_mol in replace(old, new, MOLECULE): 30 + single_replacements.add(new_mol) 67 31 68 - for r in REPLACEMENTS: 69 - if r[1] in m: 70 - for x in replace(r[0], r[1], m): 71 - submol(x, i+1) 32 + print "Distinct molecules after one replacement:", len(single_replacements) 72 33 73 34 74 - submol(MOLECULE) 75 - print LOWEST 76 - 77 - sys.exit() 35 + elements = len(re.findall(r'[A-Z]', MOLECULE)) 36 + radon = len(re.findall(r'Rn', MOLECULE)) 37 + yttrium = len(re.findall(r'Y', MOLECULE)) 78 38 79 - iterations = 1 80 - mols = set() 81 - mols.add(MOLECULE) 82 - new_mols = set() 83 - # seen = set() 39 + # print "Elements:", elements 40 + # print "Rn/Ar:", radon, "* 2" 41 + # print "Y:", yttrium 42 + # print "|E| - (Rn + Ar) - 2*Y - 1 = n" 84 43 85 - while True: 86 - iterations += 1 87 - for m in mols: 88 - for r in REPLACEMENTS: 89 - if r[1] in m: 90 - for x in replace(r[0], r[1], m): 91 - if x in ELECTRONS: 92 - print iterations 93 - sys.exit() 94 - 95 - # if x not in seen: 96 - new_mols.add(x) 97 - # seen.add(x) 98 - 99 - if len(new_mols) == 0: 100 - sys.exit("BLALALLA") 101 - 102 - mols = new_mols 103 - new_mols = set() 104 - 105 - 106 - 107 - # print len(outs) 44 + print "Fewest steps from e to medicine:", elements - (2 * radon) - (2 * yttrium) - 1