Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Compass, Map and Spyglass by liw_80
def navigation(seaside):
def get_pos(matrix, elemet):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == elemet:
return [i, j]
y_pos = get_pos(seaside, 'Y')
m_pos = get_pos(seaside, 'M')
c_pos = get_pos(seaside, 'C')
s_pos = get_pos(seaside, 'S')
def calc_dist(a, b):
return max(abs(a[0] - b[0]), abs(a[1] - b[1]))
return calc_dist(y_pos, m_pos) + calc_dist(y_pos, c_pos) + calc_dist(y_pos, s_pos)
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!")
March 16, 2020