-
Notifications
You must be signed in to change notification settings - Fork 1
/
day19.py
75 lines (50 loc) · 1.55 KB
/
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from re import fullmatch, compile
with open("inputs/day19.txt") as file:
data = [line.strip() for line in file]
def do(rule_no):
string = "("
rule = rules[rule_no].split()
for value in rule:
if value.isnumeric(): # Check some other rule
string += do(value) # Returns the partial string for that value
elif value.isalpha(): # A simple letter rule
string += value # Just add to our partial string answer
else:
string += value
return string + ")"
messages = []
rule_part = []
message_part = False
for line in data:
if line == "":
message_part = True
continue
if message_part:
messages.append(line)
else:
rule_part.append(line)
rules = {rule.split(": ")[0]: rule.split(": ")[1].replace('"', "") for rule in rule_part} # Mozno replace " az neskor?
del message_part, rule_part
# Part 1 ===
part1 = 0
pattern_string = do("0")
pattern = compile(pattern_string)
for message in messages:
if fullmatch(pattern, message):
part1 += 1
# Part 2 ===
rules["8"] = "42 +"
#rules["11"] = "42 + 31 +"
# Ugly, but I have no idea how to make it pretty. :-(
rules["11"] = "42 ( 42 ( 42 ( 42 ( 42 ( 42 ( 42 ( 42 ( 42 ( 42 ( 42 31 )? 31 )? 31 )? 31 )? 31 )? 31 )? 31 )? 31 )? 31 )? 31 )? 31"
part2 = 0
pattern_string = do("0")
pattern = compile(pattern_string)
pattern8 = do("8")
pattern42 = do("42")
pattern11 = do("11")
for message in messages:
if fullmatch(pattern, message):
part2 += 1
print("Part 1:", part1)
print("Part 2:", part2)