Skip to content

Commit 6b228e3

Browse files
committed
simplified dijkstra.py
1 parent 3f88dea commit 6b228e3

File tree

6 files changed

+160
-163
lines changed

6 files changed

+160
-163
lines changed

2021/day15/dijkstra.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
from queue import PriorityQueue
55

66
class Graph:
7-
def __init__(self, num_of_vertices, get_neighbours):
8-
self.v = num_of_vertices
9-
self.get_neighbours = get_neighbours
10-
self.visited = {}
11-
12-
def dijkstra(graph, start_vertex):
13-
D = {v:float('inf') for v in range(graph.v)}
14-
D[start_vertex] = 0
15-
16-
pq = PriorityQueue()
17-
pq.put((0, start_vertex))
18-
19-
while not pq.empty():
20-
(dist, current_vertex) = pq.get()
21-
graph.visited[current_vertex] = None
22-
23-
for neighbor, distance in graph.get_neighbours(current_vertex):
24-
if neighbor not in graph.visited:
25-
old_cost = D[neighbor]
26-
new_cost = D[current_vertex] + distance
27-
if new_cost < old_cost:
28-
pq.put((new_cost, neighbor))
29-
D[neighbor] = new_cost
30-
return D
317

8+
def dijkstra(self, start_vertex):
9+
visited = {}
10+
D = {start_vertex: 0}
11+
12+
pq = PriorityQueue()
13+
pq.put((0, start_vertex))
14+
15+
while not pq.empty():
16+
(dist, current_vertex) = pq.get()
17+
visited[current_vertex] = None
18+
19+
for neighbor, distance in self.neighbours(current_vertex):
20+
if neighbor not in visited:
21+
old_cost = D.get(neighbor, float('inf'))
22+
new_cost = D[current_vertex] + distance
23+
if new_cost < old_cost:
24+
pq.put((new_cost, neighbor))
25+
D[neighbor] = new_cost
26+
return D
27+
28+
@staticmethod
29+
def neighbours(current_vertex):
30+
raise NotImplementedError()

2021/day15/run.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,36 @@ def load_data(filename):
1111
# Part One
1212

1313
import numpy as np
14-
from dijkstra import Graph, dijkstra
14+
from dijkstra import Graph
1515

1616
a = load_data('input.txt')
1717

18-
h, w = a.shape
19-
2018
def from_0_to_last(a):
2119
h, w = a.shape
2220

23-
def get_neighbours(current):
24-
x, y = current % w, current // w
25-
if x > 0:
26-
yield current-1, a[y,x-1]
27-
if x < w-1:
28-
yield current+1, a[y,x+1]
29-
if y > 0:
30-
yield current-w, a[y-1,x]
31-
if y < h-1:
32-
yield current+w, a[y+1,x]
33-
34-
g = Graph(w * h, get_neighbours)
21+
class Walker(Graph):
3522

36-
D = dijkstra(g, 0)
23+
@staticmethod
24+
def neighbours(current):
25+
x, y = current
26+
if x > 0:
27+
yield (x-1, y), a[y,x-1]
28+
if x < w-1:
29+
yield (x+1, y), a[y,x+1]
30+
if y > 0:
31+
yield (x, y-1), a[y-1,x]
32+
if y < h-1:
33+
yield (x, y+1), a[y+1,x]
3734

38-
return D[w*h-1]
35+
D = Walker().dijkstra((0, 0))
36+
return D[(w-1, h-1)]
3937

4038
print(from_0_to_last(a))
4139

4240
# Part Two
4341

42+
h, w = a.shape
43+
4444
mw, mh = 5, 5
4545

4646
addon = np.tile(np.repeat(np.arange(mw, dtype=int), w), (mh*h, 1)) + \

2022/day12/dijkstra.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
from queue import PriorityQueue
55

66
class Graph:
7-
def __init__(self, num_of_vertices, get_neighbours):
8-
self.v = num_of_vertices
9-
self.get_neighbours = get_neighbours
10-
self.visited = {}
11-
12-
def dijkstra(graph, start_vertex):
13-
D = {v:float('inf') for v in range(graph.v)}
14-
D[start_vertex] = 0
15-
16-
pq = PriorityQueue()
17-
pq.put((0, start_vertex))
18-
19-
while not pq.empty():
20-
(dist, current_vertex) = pq.get()
21-
graph.visited[current_vertex] = None
22-
23-
for neighbor, distance in graph.get_neighbours(current_vertex):
24-
if neighbor not in graph.visited:
25-
old_cost = D[neighbor]
26-
new_cost = D[current_vertex] + distance
27-
if new_cost < old_cost:
28-
pq.put((new_cost, neighbor))
29-
D[neighbor] = new_cost
30-
return D
317

8+
def dijkstra(self, start_vertex):
9+
visited = {}
10+
D = {start_vertex: 0}
11+
12+
pq = PriorityQueue()
13+
pq.put((0, start_vertex))
14+
15+
while not pq.empty():
16+
(dist, current_vertex) = pq.get()
17+
visited[current_vertex] = None
18+
19+
for neighbor, distance in self.neighbours(current_vertex):
20+
if neighbor not in visited:
21+
old_cost = D.get(neighbor, float('inf'))
22+
new_cost = D[current_vertex] + distance
23+
if new_cost < old_cost:
24+
pq.put((new_cost, neighbor))
25+
D[neighbor] = new_cost
26+
return D
27+
28+
@staticmethod
29+
def neighbours(current_vertex):
30+
raise NotImplementedError()

2022/day12/run.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ def load_data(filename):
2121

2222
heightmap, S, E = load_data('input.txt')
2323

24-
from dijkstra import Graph, dijkstra
24+
from dijkstra import Graph
2525

2626
def from_S_to_E(heightmap, S, E):
2727
h, w = heightmap.shape
2828

29-
def get_neighbours(current):
30-
x, y = current % w, current // w
31-
height = heightmap[y,x]
32-
if x > 0 and heightmap[y,x-1] - height <= 1:
33-
yield current-1, 1
34-
if x < w-1 and heightmap[y,x+1] - height <= 1:
35-
yield current+1, 1
36-
if y > 0 and heightmap[y-1,x] - height <= 1:
37-
yield current-w, 1
38-
if y < h-1 and heightmap[y+1,x] - height <= 1:
39-
yield current+w, 1
29+
class from_S_to_E(Graph):
4030

41-
g = Graph(w * h, get_neighbours)
31+
@staticmethod
32+
def neighbours(current):
33+
x, y = current
34+
height = heightmap[y,x]
35+
if x > 0 and heightmap[y,x-1] - height <= 1:
36+
yield (x-1, y), 1
37+
if x < w-1 and heightmap[y,x+1] - height <= 1:
38+
yield (x+1, y), 1
39+
if y > 0 and heightmap[y-1,x] - height <= 1:
40+
yield (x, y-1), 1
41+
if y < h-1 and heightmap[y+1,x] - height <= 1:
42+
yield (x, y+1), 1
4243

43-
D = dijkstra(g, S[0] + w * S[1])
44-
45-
return D[E[0] + w * E[1]]
44+
D = from_S_to_E().dijkstra(S)
45+
return D[E]
4646

4747
print(from_S_to_E(heightmap, S, E))
4848

@@ -51,29 +51,30 @@ def get_neighbours(current):
5151
def from_E_to_a(heightmap, E):
5252
h, w = heightmap.shape
5353

54-
def get_neighbours(current):
55-
x, y = current % w, current // w
56-
height = heightmap[y,x]
57-
if x > 0 and heightmap[y,x-1] - height >= -1:
58-
yield current-1, 1
59-
if x < w-1 and heightmap[y,x+1] - height >= -1:
60-
yield current+1, 1
61-
if y > 0 and heightmap[y-1,x] - height >= -1:
62-
yield current-w, 1
63-
if y < h-1 and heightmap[y+1,x] - height >= -1:
64-
yield current+w, 1
54+
class from_E_to_a(Graph):
6555

66-
g = Graph(w * h, get_neighbours)
56+
@staticmethod
57+
def neighbours(current):
58+
x, y = current
59+
height = heightmap[y,x]
60+
if x > 0 and heightmap[y,x-1] - height >= -1:
61+
yield (x-1, y), 1
62+
if x < w-1 and heightmap[y,x+1] - height >= -1:
63+
yield (x+1, y), 1
64+
if y > 0 and heightmap[y-1,x] - height >= -1:
65+
yield (x, y-1), 1
66+
if y < h-1 and heightmap[y+1,x] - height >= -1:
67+
yield (x, y+1), 1
6768

68-
D = dijkstra(g, E[0] + w * E[1])
69+
D = from_E_to_a().dijkstra(E)
6970

7071
best = None
7172
for y in range(h):
7273
for x in range(w):
73-
if heightmap[y,x] == 0:
74-
if best is None or D[best[1] + w * best[0]] > D[x + w * y]:
75-
best = (y, x)
74+
if heightmap[y,x] == 0 and (x, y) in D:
75+
if best is None or D[best] > D[(x, y)]:
76+
best = (x, y)
7677

77-
return D[best[1] + w * best[0]]
78+
return D[best]
7879

7980
print(from_E_to_a(heightmap, E))

2022/day24/dijkstra.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
from queue import PriorityQueue
55

66
class Graph:
7-
def __init__(self, num_of_vertices, get_neighbours):
8-
self.v = num_of_vertices
9-
self.get_neighbours = get_neighbours
10-
self.visited = {}
11-
12-
def dijkstra(graph, start_vertex):
13-
D = {v:float('inf') for v in range(graph.v)}
14-
D[start_vertex] = 0
15-
16-
pq = PriorityQueue()
17-
pq.put((0, start_vertex))
18-
19-
while not pq.empty():
20-
(dist, current_vertex) = pq.get()
21-
graph.visited[current_vertex] = None
22-
23-
for neighbor, distance in graph.get_neighbours(current_vertex):
24-
if neighbor not in graph.visited:
25-
old_cost = D[neighbor]
26-
new_cost = D[current_vertex] + distance
27-
if new_cost < old_cost:
28-
pq.put((new_cost, neighbor))
29-
D[neighbor] = new_cost
30-
return D
317

8+
def dijkstra(self, start_vertex):
9+
visited = {}
10+
D = {start_vertex: 0}
11+
12+
pq = PriorityQueue()
13+
pq.put((0, start_vertex))
14+
15+
while not pq.empty():
16+
(dist, current_vertex) = pq.get()
17+
visited[current_vertex] = None
18+
19+
for neighbor, distance in self.neighbours(current_vertex):
20+
if neighbor not in visited:
21+
old_cost = D.get(neighbor, float('inf'))
22+
new_cost = D[current_vertex] + distance
23+
if new_cost < old_cost:
24+
pq.put((new_cost, neighbor))
25+
D[neighbor] = new_cost
26+
return D
27+
28+
@staticmethod
29+
def neighbours(current_vertex):
30+
raise NotImplementedError()

0 commit comments

Comments
 (0)