-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
84 lines (76 loc) · 2.96 KB
/
utils.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
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
ROAD_BLOCK = 1
EMPTY = 0
START = 2
def readfile(filename):
filearr = []
with open(filename) as file:
for line in file:
filearr.append(line.strip().split())
filearr2 = []
for line in filearr:
row = []
for val in line:
row.append(int(val))
filearr2.append(row)
return filearr2
def printarr(arr):
for row in arr:
for num in row:
if num == EMPTY:
# Print 0s in the default color (no color change)
print(f"{num}", end=" ")
elif num == ROAD_BLOCK:
# Print 1s in red
print(f"\033[31m{num}\033[0m", end=" ")
elif num == START:
# Print 2s in green
print(f"\033[32m{num}\033[0m", end=" ")
else:
# Print other numbers in blue
print(f"\033[34m{num}\033[0m", end=" ")
print() # Move to the next row
def printarr_with_arrows(arr, path):
arrow_symbols = {
(-2, 0): '↑', # Up
(2, 0): '↓', # Down
(0, -2): '←', # Left
(0, 2): '→', # Right
(-1, -1): '↖', # Up-Left (Diagonal)
(-1, 1): '↗', # Up-Right (Diagonal)
(1, -1): '↙', # Down-Left (Diagonal)
(1, 1): '↘', # Down-Right (Diagonal)
}
for row in range(len(arr)):
for col in range(len(arr[0])):
num = arr[row][col]
if num == EMPTY:
# Print 0s in the default color (no color change)
print(f"{num}", end=" ")
elif num == ROAD_BLOCK:
# Print 1s in red
print(f"\033[31m{num}\033[0m", end=" ")
elif num == START:
# Print 2s in green
print(f"\033[32m{num}\033[0m", end=" ")
else:
# Print arrows in blue if this point is part of the path
if (row, col) in path:
point_index = path.index((row, col))
if point_index < len(path) - 1:
# Get the next point in the path
next_row, next_col = path[point_index + 1]
# Get the last point in the path
last_row, last_col = path[point_index - 1]
# Calculate the direction
direction = ((next_row - row) - (last_row - row), (next_col - col) - (last_col - col))
# Get the corresponding arrow symbol
arrow = arrow_symbols.get(direction, '?')
# Print the arrow in blue
print(f"\033[34m{arrow}\033[0m", end=" ")
else:
# Last point in the path
print(f"\033[34m{'*'}\033[0m", end=" ")
else:
# If it's not part of the path, just print the number
print(f"{num}", end=" ")
print()