-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.py
56 lines (40 loc) · 1.43 KB
/
part1.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
51
52
53
54
55
56
# Description: Day 19 Part 1 of Advent of Code
import re
def solution(textfile: str) -> int:
""" Returns the sum of all the valid categories for each part """
# Artifact due to change of the template
lines = textfile.splitlines()
lines = [line+"\n" for line in lines]
lines[-1] = lines[-1][:-1]
# End of artifact
accepted = set()
workflows, items = "".join(lines).split("\n\n")
workflows = workflows.split("\n")
dict_workflows = {}
for workflow in workflows:
name, checks = workflow.split("{")
checks = checks[:-1].split(",")
tmp = []
for check in checks:
tmp.append(check.split(":"))
dict_workflows[name] = tmp
items = items.split("\n")
for i, item in enumerate(items):
categories = re.findall(r"\d+", item)
items[i] = list(map(int, categories))
accepted = []
for item in items:
start = "in"
x, m, a, s = item
while start != "A" and start != "R":
workflow = dict_workflows[start]
for check in workflow:
if len(check) == 1:
start = check[0]
break
if eval(check[0], {"x": x, "m": m, "a": a, "s": s}):
start = check[1]
break
if start == "A":
accepted.append(item)
return sum(sum(el) for el in accepted)