-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19p1.py
51 lines (40 loc) · 1 KB
/
day19p1.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
from day19.rules import rules
import re
rulesDone = {35: "b", 43: "a"}
rulesParse = rules
# Sample rules
# {
# ...
# 117: [[35,80],[43,10]],
# 17: [[43,112],[35,16]],
# 48: [[35,35]],
# 34: [[63,63]],
# 52: [[115,63]],
# 0: [[42,11]],
# ...
# }
def allRulesDone(lst):
for x in lst:
if x not in rulesDone.keys():
return False
return True
while rulesParse:
for x, rule in rulesParse.items():
if len(rule) == 1:
if allRulesDone(rule[0]):
rulesDone[x] = "".join([rulesDone[y] for y in rule[0]])
else:
continue
elif len(rule) == 2:
if allRulesDone(rule[0]) and allRulesDone(rule[1]):
rulesDone[x] = "(" + "".join([rulesDone[y] for y in rule[0]]) + "|" + "".join([rulesDone[y] for y in rule[1]]) + ")"
for y in rulesDone.keys():
rulesParse.pop(y, 0)
day = 19
file = "day%d/input.txt" % day
input = [line.strip() for line in open(file, "r").readlines()]
counter = 0
for x in input:
if re.fullmatch(rulesDone[0], x):
counter+=1
print(counter)