-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_to_walls.py
58 lines (45 loc) · 1.65 KB
/
path_to_walls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from ast import literal_eval as make_tuple
class Graph:
class Node:
def __init__(self, coordinate):
self.coordinate = coordinate
self.neighbors = set()
def add_neighbor(self, neighbor):
self.neighbors.add(neighbor)
def __repr__(self):
return f'{self.coordinate} -> {self.neighbors}'
def __init__(self):
self.nodes = set()
def add_node(self, coordinate, previous_coordinate=None):
node = self._get_node(coordinate)
if previous_coordinate is not None:
node.add_neighbor(previous_coordinate)
def _get_node(self, coordinate):
for node in self.nodes:
if node.coordinate == coordinate:
return node
new_node = self.Node(coordinate)
self.nodes.add(new_node)
return new_node
def find_walls(self):
walls = set()
for node in self.nodes:
c = node.coordinate
possible_neighbors = {(c[0]+1, c[1]), (c[0]-1, c[1]), (c[0], c[1]+1), (c[0], c[1]-1)}
missing_neighbors = possible_neighbors - node.neighbors
walls.update({(c, m) for m in missing_neighbors})
return walls
def read_coordinates(filename):
g = Graph()
with open(filename) as file:
previous_coordinate = None
coordinate = None
for line in file:
coordinate = make_tuple(line)
g.add_node(coordinate, previous_coordinate)
previous_coordinate = coordinate
return g
if __name__ == "__main__":
graph = read_coordinates("examples/coordinates1.txt")
print(graph.nodes)
print(graph.find_walls())