<jupyter_start><jupyter_text>1 + (2 * 3) + (4 * (5 + 6)) still becomes 51.    2 * 3 + (4 * 5) becomes 46.    5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 1445.    5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.    ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340.<jupyter_code>def first_pass(eq, pos=0, rdepth=0):
    stack = []
    ci = pos
    while ci != len(eq):
        c = eq[ci]
        if c.isnumeric():
            if stack and stack[-1] == '+':
                stack.pop(-1)
                num = stack.pop(-1)
                stack.append(num + int(c))
            else:
                stack.append(int(c))
        elif c == ' ':
            pass
        elif c == '*':
            stack.append('*')
        elif c == '(':
            rstack, ci = first_pass(eq, ci+1, rdepth+1)
            for b in rstack:
                if type(b) == int:
                    if stack and stack[-1] == '+':
                        stack.pop(-1)
                        num = stack.pop(-1)
                        stack.append(b + num)
                    else:
                        stack.append(b)
                else:
                    stack.append(b)
        elif c == ')':
            return second_pass(stack), ci
        elif c == '+':
            stack.append('+')
        ci += 1
#         print('{} stack: {} pos: {}'.format(' '*rdepth, stack, ci))
    return second_pass(stack), pos

def second_pass(stack):
    lstack = []
    for x in stack:
        if type(x) == int:
            if lstack and lstack[-1] == '*':
                lstack.pop(-1)
                num = lstack.pop(-1)
                lstack.append(x * num)
            elif lstack and lstack[-1] == '+': # Why this in second pass?
                lstack.pop(-1)
                num = lstack.pop(-1)
                lstack.append(x + num)
            else:
                lstack.append(x)
        elif x == '*':
            lstack.append('*')
        elif x == '+':
            lstack.append('+')
    return lstack
first_pass("1 + (2 * 3) + (4 * (5 + 6))")
first_pass("2 * 3 + (4 * 5)")
first_pass("5 + (8 * 3 + 9 + 3 * 4 * 3)")
first_pass("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))")
first_pass("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2")
with open('../inputs/18.txt') as f:
    lines = f.readlines()
s = 0
for line in lines:
    res, _ = first_pass(line.strip())
    s += res[0]
print(s)<jupyter_output>276894767062189
{'fraction_non_alphanumeric': 0.17389508467575382, 'fraction_numerical': 0.05410987195373813, 'mean_word_length': 1.295734597156398, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '357396', 'license': 'MIT', 'n_tokens_mistral': 927, 'n_tokens_neox': 819, 'n_words': 224, 'path': 'aoc2020/python/Day 18.ipynb', 'repo_name': 'orrinjelo/AdventOfCode2020', 'size': '2421'}