Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear solution with dictionary comprehension solution in Clear category for Compass, Map and Spyglass by Ulukai85
def navigation(seaside):
# Get dictionary with indices of the items
keys = {seaside[i][j] : (i, j) for i in range(len(seaside)) \
for j in range(len(seaside[0])) if seaside[i][j]}
x, y = keys['Y']
# Pick distance for each item (for diagonals the distance is the longer one
# of the horizontal and vertical distances) and sum them up
return sum(max(abs(v[0] - x), abs(v[1] - y)) for v in keys.values())
if __name__ == '__main__':
print("Example:")
print(navigation([['Y', 0, 0, 0, 'C'],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
['M', 0, 0, 0, 'S']]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert navigation([['Y', 0, 0, 0, 'C'],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
['M', 0, 0, 0, 'S']]) == 11
assert navigation([[ 0, 0, 'C'],
[ 0, 'S', 0],
['M','Y', 0]]) == 4
assert navigation([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 'M', 0, 'S', 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 'C', 0, 0, 0],
[ 0, 'Y',0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]]) == 9
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 4, 2020