Skip to content

Instantly share code, notes, and snippets.

@timotta
Created July 24, 2024 23:39
Show Gist options
  • Save timotta/10e87a5337558702e0cb92196bc9a7e6 to your computer and use it in GitHub Desktop.
Save timotta/10e87a5337558702e0cb92196bc9a7e6 to your computer and use it in GitHub Desktop.
def calculate_distance_one_point(city, position, cache, value=-1):
row, col = position
if (row, col) in cache:
print( f"Already {row} {col}" )
return None
cache.add((row, col))
print("doing", row, col)
if row < 0 or col < 0 or row >= len(city) or col >= len(city[0]):
print( f"Limits {row} {col}" )
return None
cell = city[row][col]
if cell == "X":
return None
if cell == "D":
print(row, col, "D", value, "+1")
return value + 1
if cell == " ":
print(" space ", row, col, "S", value, "+1")
results = []
results.append( calculate_distance_one_point(city, [row - 1, col], cache.copy(), value+1) )
results.append( calculate_distance_one_point(city, [row, col + 1], cache.copy(), value+1) )
results.append( calculate_distance_one_point(city, [row + 1, col], cache.copy(), value+1) )
results.append( calculate_distance_one_point(city, [row, col - 1], cache.copy(), value+1) )
results = [r for r in results if r is not None]
print("results", results)
if not results:
return None
else:
return min(results)
def calculate_distance(city, positions):
results = []
for position in positions:
cache = set()
result = calculate_distance_one_point(city, position, cache)
results.append( -1 if result is None else result )
return results
def test_distance_curve3():
city = [
["D", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", "D", " "],
[" ", " ", "X", "D", " "],
]
positions = [
[1, 1],
]
result = calculate_distance(city, positions)
assert result == [2]
def test_distance_curve2():
city = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", "D", " "],
[" ", " ", "X", "D", " "],
]
positions = [
[1, 1],
]
result = calculate_distance(city, positions)
assert result == [3]
def test_distance_right():
city = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", "D", " "],
[" ", " ", "X", "D", " "],
]
positions = [
[2, 1],
]
result = calculate_distance(city, positions)
assert result == [2]
def test_distance_left():
city = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", "D", " ", " ", " "],
[" ", " ", " ", " ", " "],
]
positions = [
[2, 3],
]
result = calculate_distance(city, positions)
assert result == [2]
def test_distance_top():
city = [
[" ", "D", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
]
positions = [
[3, 1],
]
result = calculate_distance(city, positions)
assert result == [3]
def test_distance_bottom():
city = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", "D", " ", " ", " "],
]
positions = [
[0, 1],
]
result = calculate_distance(city, positions)
assert result == [3]
def test_distance_blocked1():
city = [
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", "X", " "],
[" ", " ", "X", "D", "X"],
]
positions = [
[2, 4],
]
result = calculate_distance(city, positions)
assert result == [-1]
def test_distance_blocked2():
city = [
["D", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", "X", " "],
[" ", " ", "X", "D", "X"],
]
positions = [
[2, 4],
]
result = calculate_distance(city, positions)
assert result == [6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment