-
Notifications
You must be signed in to change notification settings - Fork 0
/
19-1.py
50 lines (37 loc) · 1.2 KB
/
19-1.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from itertools import groupby
from operator import gt, lt
OPERATORS = {"<": lt, ">": gt}
with open("input.txt") as f:
lines = f.read().splitlines()
workflows_raw, ratings = [
tuple(group) for not_empty, group in groupby(lines, bool) if not_empty
]
workflows = {}
for workflow in workflows_raw:
name, rules_str = workflow[:-1].split("{")
rules = rules_str.split(",")
workflows[name] = ([], rules.pop())
for rule in rules:
condition, target = rule.split(":")
key, comparsion, *value = tuple(condition)
workflows[name][0].append((key, comparsion, int("".join(value)), target))
def check_rules(items, name="in"):
if name == "R":
return False
if name == "A":
return True
rules, fallback = workflows[name]
for key, comparsion, value, target in rules:
if OPERATORS[comparsion](items[key], value):
return check_rules(items, target)
return check_rules(items, fallback)
res = 0
for rating in ratings:
items = {
key: int(value)
for expression in rating[1:-1].split(",")
for key, value in [expression.split("=")]
}
if check_rules(items):
res += sum(items.values())
print(res)