day8 part 2 work in progress, still not working

This commit is contained in:
2023-12-09 08:28:09 +01:00
parent 3d1582f685
commit 7b73d8402a
5 changed files with 107 additions and 29 deletions

View File

@@ -1,37 +1,24 @@
#!/usr/bin/env python3.11
from typing import List, Tuple
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
from typing import List
from datetime import datetime, timedelta
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")
def count_steps_XXA_to_XXZ(directions:str, node_list: List[Node]) -> int:
current_nodes = list(filter(lambda node: node.is_A_node(), node_list))
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)
start_time = datetime.now()
while not all(map(lambda n: n.is_Z_node(), current_nodes)):
if directions[count % len(directions)] == 'L':
current_nodes = list(map(lambda n: n.move_left(node_list), current_nodes))
else:
current_nodes = list(map(lambda n: n.move_right(node_list), current_nodes))
if start_time + timedelta(minutes=1) <= datetime.now():
str_nodes = [node.__str__() for node in current_nodes]
print(f"count: {count} / {str_nodes}")
start_time = datetime.now()
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))
print(count_steps_XXA_to_XXZ(directions, node_list))