Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- work, _ = open("input").read().split('\n\n')
- workflows = {name: rules[:-1].split(',') for name, rules in (line.split('{') for line in work.splitlines())}
- def find_rules(current, rules):
- if current == 'A':
- regole.append(rules)
- elif current != 'R':
- rul = []
- for rule in workflows[current][:-1]:
- if rule not in 'AR':
- next_workflow = rule.split(':')[1]
- find_rules(next_workflow, rules + rul + [rule.split(':')[0]])
- if rule[1] == '<':
- rul.append(rule.split(':')[0].split('<')[0] + '>=' + rule.split(':')[0].split('<')[1])
- elif rule[1] == '>':
- rul.append(rule.split(':')[0].split('>')[0] + '<=' + rule.split(':')[0].split('>')[1])
- else:
- next_workflow = workflows[current][-1]
- find_rules(next_workflow, rules + rul)
- regole = []
- find_rules('in', [])
- somma = 0
- for r in regole:
- maxs = {x: 4001 for x in 'xmas'}
- mins = {x: 0 for x in 'xmas'}
- for s in r:
- if s[0] in 'xmas' and s[1] == '<':
- maxs[s[0]] = min(maxs[s[0]], int(s[2:])) if s[2] != '=' else min(maxs[s[0]], int(s[3:]) + 1)
- elif s[0] in 'xmas' and s[1] == '>':
- mins[s[0]] = max(mins[s[0]], int(s[2:])) if s[2] != '=' else max(mins[s[0]], int(s[3:]) - 1)
- p = 1
- for key in maxs:
- p *= (maxs[key] - mins[key] - 1)
- somma += p
- print(somma)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement