Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day19_part2(s):
- s_rules, _ = s.split('\n\n')
- rules = {}
- for line in s_rules.splitlines():
- name, s_rules = line[:-1].split('{')
- rules[name] = s_rules.split(',')
- def compute(name, intervals):
- if name == 'R' or any(start >= stop for start, stop in intervals.values()):
- return 0
- if name == 'A':
- return math.prod(stop - start for start, stop in intervals.values())
- total = 0
- for rule in rules[name]:
- if rule.isalpha():
- return total + compute(rule, intervals)
- category, op, s_num, label = re.match(r'^(\w)([<>])(\d+):(\w+)$', rule).groups()
- num = int(s_num)
- start, stop = intervals[category]
- if op == '<':
- total += compute(label, intervals | {category: (start, min(stop, num))})
- intervals[category] = max(start, num), stop
- else:
- total += compute(label, intervals | {category: (max(start, num + 1), stop)})
- intervals[category] = start, min(stop, num + 1)
- return compute('in', {category: (1, 4001) for category in 'xmas'})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement