brewersfan1976

find_path.rb

Nov 26th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.09 KB | None | 0 0
  1. def findPath(matrix)
  2.     require 'matrix'
  3.     x_length = matrix.length
  4.     y_length = matrix[0].length
  5.     total_length = x_length * y_length
  6.        
  7.     touching = true
  8.     previous_location = Matrix[*matrix].index(1)
  9.     previous_x_location = previous_location[0]
  10.     previous_y_location = previous_location[1]
  11.     x = 1
  12.    
  13.     while (x < total_length)
  14.           x = x + 1
  15.           new_location = Matrix[*matrix].index(x)
  16.           new_x_location = new_location[0]
  17.           new_y_location = new_location[1]
  18.           x_location_difference = (new_location[0] - previous_location[0]).abs
  19.           y_location_difference = (new_location[1] - previous_location[1]).abs
  20.           overall_location_difference = x_location_difference + y_location_difference
  21.        
  22.           if overall_location_difference > 1 then
  23.              touching = false
  24.              break
  25.           end
  26.              
  27.           previous_location = Matrix[*matrix].index(x)
  28.           previous_x_location = new_location[0]
  29.           previous_y_location = new_location[1]
  30.     end
  31.        
  32.     return touching
  33. end
Advertisement
Add Comment
Please, Sign In to add comment