-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.py
29 lines (25 loc) · 938 Bytes
/
day19.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
with open("inputs/day19.txt") as f:
inp = f.read().split("\n\n")
workflows = {}
for flow in inp[0].splitlines():
name, rules = flow[:-1].split('{')
workflows[name] = rules.split(',')
def handle_rule(part, rule):
if (ix := rule.find(':')) > -1:
if (((op := rule.find('>')) > -1 and part[rule[:op]] > int(rule[op+1:ix]))
or ((op := rule.find('<')) > -1 and part[rule[:op]] < int(rule[op+1:ix]))):
return rule[ix+1:]
else: return rule
def exec_wkflow(part, flow):
for rule in flow:
if res := handle_rule(part, rule):
return exec_wkflow(part, workflows[res]) if res in workflows else res == 'A'
total = 0
for part_data in inp[1].splitlines():
part = {}
for rating in part_data[1:-1].split(','):
name, val = rating.split('=')
part[name] = int(val)
if exec_wkflow(part, workflows['in']):
total += sum(part.values())
print(total)