Day 8 part 1 completed, part 2 in progress
This commit is contained in:
37
day8/part2.py
Executable file
37
day8/part2.py
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3.11
|
||||
from typing import List, Tuple
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from threading import current_thread
|
||||
from data import Node, parse
|
||||
|
||||
def count_steps_XXA_to_XXZ(node: Node, directions:str, node_list: List[Node]) -> int:
|
||||
print(f"thread {current_thread().name} counting from {node}")
|
||||
if not node.is_A_node():
|
||||
raise Exception("node must be a XXA node")
|
||||
count = 0
|
||||
current_node = node
|
||||
while not current_node.is_Z_node():
|
||||
current_node = current_node.move_left(node_list) if directions[count % len(directions)] == 'L' else current_node.move_right(node_list)
|
||||
count += 1
|
||||
print(f"thread {current_thread().name} as counted {count} steps to {current_node}")
|
||||
return count
|
||||
|
||||
if __name__ == '__main__':
|
||||
directions, node_list = parse('input.txt')
|
||||
print("solving all nodes")
|
||||
solved_node_list = map(lambda node: node.resolve_left_right_nodes(node_list), node_list)
|
||||
#print(node_list)
|
||||
print("all_nodes_solved")
|
||||
xxA_nodes = list(filter(lambda node: node.is_A_node(), solved_node_list))
|
||||
print(xxA_nodes)
|
||||
results = []
|
||||
#Parallel version
|
||||
#with ThreadPoolExecutor(max_workers=10) as executor:
|
||||
# print("starting ThreadPool")
|
||||
# for xxA_node in xxA_nodes:
|
||||
# results.append(executor.submit(count_steps_XXA_to_XXZ, xxA_node, directions, solved_node_list))
|
||||
#results = map(lambda exec: exec.result(), results)
|
||||
results = [count_steps_XXA_to_XXZ(xxA_node, directions, solved_node_list) for xxA_node in xxA_nodes]
|
||||
print(results)
|
||||
print(sum(results))
|
||||
|
||||
Reference in New Issue
Block a user