Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def findPath(matrix)
- require 'matrix'
- x_length = matrix.length
- y_length = matrix[0].length
- total_length = x_length * y_length
- touching = true
- previous_location = Matrix[*matrix].index(1)
- previous_x_location = previous_location[0]
- previous_y_location = previous_location[1]
- x = 1
- while (x < total_length)
- x = x + 1
- new_location = Matrix[*matrix].index(x)
- new_x_location = new_location[0]
- new_y_location = new_location[1]
- x_location_difference = (new_location[0] - previous_location[0]).abs
- y_location_difference = (new_location[1] - previous_location[1]).abs
- overall_location_difference = x_location_difference + y_location_difference
- if overall_location_difference > 1 then
- touching = false
- break
- end
- previous_location = Matrix[*matrix].index(x)
- previous_x_location = new_location[0]
- previous_y_location = new_location[1]
- end
- return touching
- end
Advertisement
Add Comment
Please, Sign In to add comment