day8 part 2 work in progress, still not working
This commit is contained in:
25
day8/part2_cycles.py
Executable file
25
day8/part2_cycles.py
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3.11
|
||||
from typing import List
|
||||
import math
|
||||
from data import Node, parse
|
||||
|
||||
def find_cycle(start_node, directions, node_list) -> List[Node]:
|
||||
node_cycle = []
|
||||
idx = 0
|
||||
current_node = (start_node, idx)
|
||||
while current_node not in node_cycle and not current_node[0].is_Z_node() :
|
||||
node_cycle.append(current_node)
|
||||
idx += 1
|
||||
if directions[(idx - 1) % len(directions)] == 'L':
|
||||
current_node = (current_node[0].move_left(node_list), idx % len(directions))
|
||||
else:
|
||||
current_node = (current_node[0].move_right(node_list), idx % len(directions))
|
||||
node_cycle.append(current_node)
|
||||
return [node for node,_ in node_cycle]
|
||||
|
||||
if __name__ == '__main__':
|
||||
directions, node_list = parse('input.txt')
|
||||
xxA_nodes = list(filter(lambda node: node.is_A_node(), node_list))
|
||||
cycles = list(map(len,map(lambda node: find_cycle(node, directions, node_list), xxA_nodes)))
|
||||
print(cycles)
|
||||
print(math.lcm(*cycles))
|
||||
Reference in New Issue
Block a user