2023 dec 8th
This commit is contained in:
parent
9de1e0ca9d
commit
f46954d5ff
1 changed files with 57 additions and 0 deletions
57
Python/2023/08/main.py
Normal file
57
Python/2023/08/main.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import math
|
||||
|
||||
input = open("input", 'r')
|
||||
|
||||
instructions = input.readline().rstrip()
|
||||
data = {}
|
||||
all_nodes = []
|
||||
|
||||
_ = input.readline()
|
||||
|
||||
for line in input:
|
||||
line = line.rstrip()
|
||||
node, nexts = line.split(" = ")
|
||||
left, right = nexts.split(", ")
|
||||
|
||||
all_nodes.append(node)
|
||||
|
||||
data[node] = {
|
||||
"left": left[1:],
|
||||
"right": right[:-1]
|
||||
}
|
||||
|
||||
|
||||
current = "AAA"
|
||||
inst = 0
|
||||
|
||||
steps = 0
|
||||
|
||||
while current != "ZZZ":
|
||||
go = instructions[inst]
|
||||
steps += 1
|
||||
inst = (inst + 1) % len(instructions)
|
||||
if go == "L":
|
||||
current = data[current]["left"]
|
||||
else:
|
||||
current = data[current]["right"]
|
||||
|
||||
print(steps)
|
||||
|
||||
def fromAToZ(start, ends):
|
||||
current = start
|
||||
inst = 0
|
||||
steps = 0
|
||||
|
||||
while current not in ends:
|
||||
go = instructions[inst]
|
||||
steps += 1
|
||||
inst = (inst + 1) % len(instructions)
|
||||
direct = "left" if go == "L" else "right"
|
||||
current = data[current][direct]
|
||||
|
||||
return steps
|
||||
|
||||
starts = [node for node in all_nodes if node[-1] == "A"]
|
||||
ends = [node for node in all_nodes if node[-1] == "Z"]
|
||||
|
||||
print(math.lcm(*[fromAToZ(start, ends) for start in starts]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue