summaryrefslogtreecommitdiffstats
path: root/aoc/mixins.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-19 09:30:43 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-19 09:30:43 +0100
commitf3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2 (patch)
tree535a7048c7547e1eaef6f97dcfd8b2cad598ca50 /aoc/mixins.py
parent06cb539f69f0b501afaa9ef5b6d89863e1c9d111 (diff)
download2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.gz
2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.bz2
2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.zip
Day 18
Diffstat (limited to 'aoc/mixins.py')
-rw-r--r--aoc/mixins.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/aoc/mixins.py b/aoc/mixins.py
index faeb3eb..5986b6e 100644
--- a/aoc/mixins.py
+++ b/aoc/mixins.py
@@ -55,3 +55,23 @@ class AStarMixin(Generic[Node]):
55 55
56 if neighbour not in open_nodes: 56 if neighbour not in open_nodes:
57 open_nodes.add(neighbour) 57 open_nodes.add(neighbour)
58
59
60class BreathFirstSearchMixin(Generic[Node]):
61 @staticmethod
62 def bfs(
63 start: Node, neighbours: Callable[[Node], Iterator[Node]]
64 ) -> Iterator[Node]:
65 queue = [start]
66 searched = set()
67
68 while len(queue):
69 item = queue.pop(0)
70
71 for neighbour in neighbours(item):
72 if neighbour not in searched and neighbour not in set(queue):
73 queue.append(neighbour)
74
75 searched.add(item)
76
77 yield item