-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_19.py
50 lines (39 loc) · 1.48 KB
/
day_19.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
import re
from datetime import datetime
time_start = datetime.now()
with open('day_19.input', 'r') as file:
lines = file.readlines()
# Function to count the number of ways to match the pattern
def count_ways(linen, patterns):
count = [0 for _ in range(len(linen) + 1)]
# We can reach the start of the string in 1 way this is linen[0:0]
count[0] = 1
# From the start of the string
for index in range(len(linen)):
# Check if the pattern is at the start
for pattern in patterns:
if linen[index:index + len(pattern)] == pattern:
# If it matches the amount of ways to reach this index + the size of the match
# is added with the amount of ways to reach the previous step (index)
count[index + len(pattern)] += count[index]
# This will be the amount of ways we can reach the end of the string
return count[len(linen)]
CHARACTER_REGEX = r"[a-z]+"
regex_linen_matcher = r"\b(?:"
linen_patterns = []
for v in re.findall(CHARACTER_REGEX, lines[0]):
regex_linen_matcher += v + r"|"
linen_patterns.append(v)
regex_linen_matcher = regex_linen_matcher[:-1] + r")+\b"
part1 = 0
part2 = 0
for i in range(2, len(lines)):
linen = lines[i][:-1]
match = re.fullmatch(regex_linen_matcher, linen)
if match:
part1 += 1
part2 += count_ways(linen, linen_patterns)
print("Part1", part1)
print("Part2", part2)
print("Total time: ", datetime.now() - time_start)
#Total time: 0:00:01.659790