diff --git a/2023/8.py b/2023/8.py index be669e8..cc99a61 100644 --- a/2023/8.py +++ b/2023/8.py @@ -1,3 +1,5 @@ +from math import lcm + input = open("input.txt", 'r').read().splitlines() instructions = input[0] nodes = {l.split()[0]: (l.split()[2][1:-1], l.split()[3][:-1]) for l in input[2:]} @@ -9,4 +11,18 @@ while node != 'ZZZ': node = nodes[node][0] if instructions[steps % len(instructions)] == 'L' else nodes[node][1] steps += 1 -print(steps) \ No newline at end of file + +nodeGhost = [n for n in nodes if n[-1] == 'A'] +multiples = [] + +for n in nodeGhost: + stepsGhost = 0 + nodeTemp = n + while nodeTemp[-1] != 'Z': + nodeTemp = nodes[nodeTemp][0] if instructions[stepsGhost % len(instructions)] == 'L' else nodes[nodeTemp][1] + stepsGhost += 1 + multiples.append(stepsGhost) + + +print(steps) +print(lcm(*multiples)) \ No newline at end of file