solve.py (668B)
1import sys 2sys.path.append("../common") 3import aoc 4 5data = aoc.data 6 7def react(s): 8 i = 0 9 while i < len(s)-1: 10 cs = "".join(s[i:i+2]) 11 csl = cs.lower() 12 if csl[0] == csl[1] and cs[0] != cs[1]: 13 s.pop(i) 14 s.pop(i) 15 if i > 0: 16 i -= 1 17 else: 18 i += 1 19 20 return len(s) 21 22def solve1(args): 23 return react(list(data)) 24 25def solve2(args): 26 react_wo = lambda x : react(list(data.replace(x, "").replace(x.upper(), ""))) 27 alph = "abcdefghijklmnopqrstuvwxyz" 28 return min((react_wo(c) for c in alph)) 29 30aoc.run(solve1, solve2, sols=[11364, 4212])