Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First via coords dict solution in Clear category for Compass, Map and Spyglass by Molot
def navigation(seaside):
coords = {'Y':[], 'M':[], 'S':[], 'C':[]}
deltas, res = [], 0
for row in range(len(seaside)):
for col in range(len(seaside[0])):
k = seaside[row][col]
if k in coords.keys():
coords[k] = [row, col]
for x in ['C', 'M', 'S']:
deltas.append(list(map(lambda x: abs(x[0] - x[1]),
(zip(coords['Y'], coords[x])))))
for d in deltas:
res += d[0] if d[0] == d[1] else min(*d) + abs(d[0]-d[1])
return res
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!")
May 29, 2019