-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
67 lines (44 loc) · 1.28 KB
/
solver.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
import inspect
import os
import re
import math
cwd = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
with open(os.path.join(cwd, "example.txt"), "r") as f:
lines = f.read().splitlines()
ops = [int(op) for op in lines[0].translate(str.maketrans("LR", "01"))]
maps = {
key: [re.sub(r"[()]", "", path) for path in values.split(", ")]
for key, values in [line.split(" = ") for line in lines[2:]]
}
def find_path_count(maps, current, pattern):
res = 0
while not pattern(current):
for op in ops:
res += 1
current = maps[current][op]
if pattern(current):
break
return res
# part 1
print(find_path_count(maps, "AAA", lambda p: p == "ZZZ"))
# part 2
currents = tuple(node for node in maps if node.endswith("A"))
counts = [
find_path_count(maps, current, lambda p: p.endswith("Z")) for current in currents
]
def find_lcm(lst):
if len(lst) == 1:
return lst[0]
lcm = lst[0]
for curr in lst[1:]:
num = max(lcm, curr)
den = min(lcm, curr)
rem = num % den
while rem != 0:
num = den
den = rem
rem = num % den
# den ends up being gcd
lcm = int((lcm * curr) / den)
return lcm
print(math.lcm(*counts))