Day 8 part 1 completed, part 2 in progress

This commit is contained in:
Fedaya 2023-12-08 08:05:55 +01:00
parent 9c5947aba2
commit 3d1582f685
4 changed files with 112 additions and 75 deletions

54
day8/data.py Normal file
View File

@ -0,0 +1,54 @@
from __future__ import annotations
from typing import List, Tuple, Optional, Self
import weakref
from dataclasses import dataclass
import re
@dataclass
class Node:
value: str
left: str
right: str
_left_node: Optional[weakref.ReferenceType[Node]] = None
_right_node: Optional[weakref.ReferenceType[Node]] = None
def move_left(self, node_list: List[Node]) -> Node:
if self._left_node is None:
node = weakref.ref(list(filter(lambda n: n.value == self.left, node_list))[0])
self._left_node = node
return self._left_node()
def move_right(self, node_list: List[Node]) -> Node:
if self._right_node is None:
node = weakref.ref(list(filter(lambda n: n.value == self.right,node_list))[0])
self._right_node = node
return self._right_node()
def resolve_left_right_nodes(self, node_list: List[Node]) -> Self:
idx = 0
while idx < len(node_list) and (self._left_node is None or self._right_node is None):
if node_list[idx].value == self.left:
self._left_node = weakref.ref(node_list[idx])
if node_list[idx].value == self.right:
self._right_node = weakref.ref(node_list[idx])
idx += 1
if self._left_node == None or self._right_node == None:
print(f"Invalid Node: {self}")
return self
def is_A_node(self):
return self.value[2] == 'A'
def is_Z_node(self):
return self.value[2] == 'Z'
def parse(input_file:str) -> Tuple[str, List[Node]]:
the_list = []
with open(input_file) as input:
directions = input.readline().replace("\n","")
_ = input.readline()
while match:= re.match("([A-Z]+) = \(([A-Z]+), ([A-Z]+)\)",input.readline()):
node = Node(value=match.group(1), left=match.group(2), right=match.group(3))
the_list.append(node)
return directions, the_list

21
day8/part1.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3.11
from typing import List
from data import Node, parse
def count_steps_AAA_ZZZ(directions:str , node_list: List[Node]) -> int:
count = 0
current_node = list(filter(lambda n:n.value == 'AAA', node_list))[0]
while current_node.value != 'ZZZ':
current_node = current_node.move_left(node_list) if directions[count % len(directions)] == 'L' else current_node.move_right(node_list)
count += 1
return count
if __name__ == '__main__':
directions, node_list = parse('input.txt')
count = count_steps_AAA_ZZZ(directions, node_list)
print(count)

37
day8/part2.py Executable file
View 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))

75
q
View File

@ -1,75 +0,0 @@
commit 028c13fbba8bb94d3cad5d5d5d6509923c6be082 (HEAD -> main)
Author: Fedaya <fedaya@fedaya.net>
Date: Wed Dec 6 22:00:00 2023 +0100
Revert "Debugging of Day5/part2_time_hog.py"
This reverts commit bf7aac3b22c1e777a13674535ea4a869392acb12.
commit bf7aac3b22c1e777a13674535ea4a869392acb12
Author: Étienne Gille <etienne.gille@ville-lespontsdece.fr>
Date: Wed Dec 6 21:58:35 2023 +0100
Debugging of Day5/part2_time_hog.py
It now works in parallel, not in a timely manner, though
commit 8b2bac07914217eb8b57798e4d0f798953e8db9d (origin/main, origin/HEAD)
Author: Fedaya <fedaya@fedaya.net>
Date: Wed Dec 6 08:12:32 2023 +0100
Day 6 completed
Day5 part 2 still ongoing
commit b588eeb7e5cc84aa6c61791ff7612edfc0116bb9
Author: Fedaya <fedaya@fedaya.net>
Date: Tue Dec 5 08:06:53 2023 +0100
First solution to part2
It's kind of a time hog, though
commit e46a2717b4c062a7d1bcf92f018a4c91538a9fcc
Author: Fedaya <fedaya@fedaya.net>
Date: Tue Dec 5 07:18:55 2023 +0100
Day 5 part1 completed
Also, deleted an example_input.txt, modified .gitignore
added a script to get the current day's input
commit 9ee98d375a929dd8b690e50f4b62e8b3460fdd8b
Author: Fedaya <fedaya@fedaya.net>
Date: Mon Dec 4 07:47:34 2023 +0100
Modified .gitignore to not include example_input.txt
commit 142696bf6e92ce3e97de220f03c0b57c2b9c0f2a
Author: Fedaya <fedaya@fedaya.net>
Date: Mon Dec 4 07:44:36 2023 +0100
Day 4 completed
+ refactored day 1 to 3 with black -l 119
commit 97021a58b2d41f64b55e4dcb8257dd859817cd95
Author: Fedaya <fedaya@fedaya.net>
Date: Sun Dec 3 08:41:27 2023 +0100
Part 3 completed
commit fbf1c66a3968f0f300cbb6d1895e71c515278a9e
Author: Fedaya <fedaya@fedaya.net>
Date: Sat Dec 2 12:45:07 2023 +0100
Nouveau Repository
Je perd l'historique, mais ne stocke plus les fichiers input.txt
entre autre choses
commit 3e06d85867ceffb21d59c949bef8049e8b0670ef
Author: Fedaya <fedaya@fedaya.net>
Date: Sat Dec 2 12:42:58 2023 +0100
Initial commit