-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18-ram-run.py
More file actions
101 lines (77 loc) · 2.3 KB
/
Copy path18-ram-run.py
File metadata and controls
101 lines (77 loc) · 2.3 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image
MEM_VAL = 1
PATH_VAL = 2
input = np.loadtxt("input.txt", delimiter=",", dtype=int)
def get_neighbors(x, y, memory):
neighbors = []
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if 0 <= x + dx < 71 and 0 <= y + dy < 71:
if memory[x + dx, y + dy] == 0:
neighbors.append((x + dx, y + dy))
return neighbors
def shortest_path(memory, start=(0,0), end=(70,70)):
P = set()
dist = np.ones((71,71)) * np.inf
dist[start] = 0
pred = {}
while True: # Tant qu'il existe un sommet hors de P
a = None
min_dist = np.inf
for i in range(71):
for j in range(71):
if (i, j) not in P and dist[i, j] < min_dist:
min_dist = dist[i, j]
a = (i, j)
if a is None:
break
# print("Sommet ", a)
P.add(a)
if a == end:
break
neighbors = get_neighbors(*a, memory)
# print("Voisins ", neighbors)
for b in neighbors:
if b not in P:
if dist[b] > dist[a] + 1:
dist[b] = dist[a] + 1
pred[b] = a
return dist[end], pred
def partI():
memory = np.zeros((71,71))
for i in range(1024):
x, y = input[i]
memory[x, y] = MEM_VAL
dist, pred = shortest_path(memory)
print("I:", int(dist))
path = (70,70)
while path != (0,0):
memory[path] = PATH_VAL
path = pred[path]
memory[0,0] = PATH_VAL
matplotlib.image.imsave('pathI.png', memory, cmap="viridis")
# plt.imshow(memory, cmap="viridis")
# plt.savefig("pathI.png")
def partII(init=2938): # magic !
memory = np.zeros((71,71))
i = 0
for _ in range(init):
x, y = input[i]
memory[x, y] = MEM_VAL
i += 1
crt = True
while crt:
x, y = input[i]
memory[x, y] = MEM_VAL
print(i, input[i], end=" ")
dist, pred = shortest_path(memory)
print(dist)
if dist == np.inf:
crt = False
print("II:", f"{input[i][0]},{input[i][1]}")
memory[0,0] = PATH_VAL
matplotlib.image.imsave('pathII.png', memory, cmap="viridis")
i += 1
partI()
partII()