-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_16a.py
61 lines (47 loc) · 1.49 KB
/
day_16a.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
57
58
59
60
61
#!/usr/bin/env python3
import re
class Rule:
def __init__(self, name, min_val1, max_val1, min_val2, max_val2):
self.name = name
self.min_val1 = int(min_val1)
self.max_val1 = int(max_val1)
self.min_val2 = int(min_val2)
self.max_val2 = int(max_val2)
def valid(self, val):
return (val >= self.min_val1 and val <= self.max_val1) or (
val >= self.min_val2 and val <= self.max_val2
)
def getRules(f):
pattern = re.compile(r"(.*?): (\d+)-(\d+) or (\d+)-(\d+)(.*?)")
rules = dict()
for line in f:
line = line.strip().replace("\r", "").replace("\n", "")
if re.match(pattern, line):
vals = re.match(pattern, line).groups()
rules[vals[0]] = Rule(vals[0], vals[1], vals[2], vals[3], vals[4])
else:
break
return rules
def getTickets(f, rules):
pattern = re.compile(r"(.*?):((.*?))")
tser = 0
for line in f:
if re.match(pattern, line):
continue
line = line.strip().replace("\r", "").replace("\n", "")
if line == "":
continue
else:
vals = line.split(",")
for val in vals:
if not any(rule.valid(int(val)) for rule in rules.values()):
tser += int(val)
return tser
def main():
f = open("../input/day_16_input")
rules = getRules(f)
tser = getTickets(f, rules)
print(tser)
return tser
if __name__ == "__main__":
main()