diff options
Diffstat (limited to 'aoc/mixins.py')
| -rw-r--r-- | aoc/mixins.py | 20 |
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 | |||
| 60 | class 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 | ||
