aboutsummaryrefslogtreecommitdiffstats
path: root/src/05/solve.py
blob: 263f9fdbc8ad98b1a7d08664b694e2fb52050b1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys
sys.path.append("../common")
import aoc

data = aoc.data

def react(s):
    i = 0
    while i < len(s)-1:
        cs = "".join(s[i:i+2])
        csl = cs.lower()
        if csl[0] == csl[1] and cs[0] != cs[1]:
            s.pop(i)
            s.pop(i)
            if i > 0:
                i -= 1
        else:
            i += 1

    return len(s)

def solve1(args):
    return react(list(data))

def solve2(args):
    react_wo = lambda x : react(list(data.replace(x, "").replace(x.upper(), "")))
    alph = "abcdefghijklmnopqrstuvwxyz"
    return min((react_wo(c) for c in alph))

aoc.run(solve1, solve2, sols=[11364, 4212])