brewersfan1976

robot_path.rb

Jan 31st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.77 KB | None | 0 0
  1. def robotPath(instructions, bound)
  2.     x = 0
  3.     start = Array.new
  4.     start = [0,0]
  5.    
  6.     while (x < instructions.length)
  7.           if instructions[x] == "L" and (start[0] - 1).abs <= bound.abs then
  8.              start[0] = start[0] - 1
  9.           end
  10.              
  11.           if instructions[x] == "R" and (start[0] + 1).abs <= bound.abs then
  12.              start[0] = start[0] + 1
  13.           end
  14.              
  15.           if instructions[x] == "U" and (start[1] + 1).abs <= bound.abs then
  16.              start[1] = start[1] + 1
  17.           end
  18.              
  19.           if instructions[x] == "D" and (start[1] - 1).abs <= bound.abs then
  20.              start[1] = start[1] - 1
  21.           end
  22.              
  23.           x = x + 1
  24.     end
  25.              
  26.     return start
  27. end
Advertisement
Add Comment
Please, Sign In to add comment