from dis import Instruction
import re
from tabnanny import check
import numpy as np


f = open('input.txt', 'r')
# f = open('test.txt', 'r')
content = f.read()
# lines = content.splitlines()
paragraphs = content.split("\n\n")
sum = 0

flows =  {}

print(paragraphs[0].split("\n")[0].split('{')[1].strip("}"))
for workflow in paragraphs[0].split("\n"):
    sub_workflow = workflow.split('{')
    name = sub_workflow[0]
    instructions = sub_workflow[1].strip("}").split(",")
    # print(instructions)
    inst_list = []
    for a in instructions:
        if_stat = a.split(":")
        logic = []
        for ch in ["<", ">"]:
            if ch in if_stat[0]:
                temp = if_stat[0].split(ch)
                logic = [temp[0], ch, int(temp[1])]
                break
        # inst
        inst_list.append(logic+[if_stat[-1]])
    print(inst_list)


    flows[name] = inst_list

vals = []

for val in paragraphs[1].split("\n"):
    current_val = {}
    for i in val.strip("{}").split(","):
        x = i.split("=")
        print(x)
        current_val[x[0]] = int(x[1])
    vals.append(current_val)

print(vals)


print(flows)

sum = 0

for val in vals:
    key = "in"
    while key != "A" and key != "R":
        print(key)
        for inst in flows[key]:
            # pass
            if len(inst) == 1:
                key = inst[-1]
                break
            else:
                eq0 = val[inst[0]] if inst[1] == "<" else inst[2]
                eq1 = val[inst[0]] if inst[1] == ">" else inst[2]

                if eq0 < eq1:
                    key = inst[-1] 
                    break
                else:
                    continue

    if key == "A":
        sum += val["x"]+val["m"]+val["a"]+val["s"]

print(sum)

f.close()