Advertisement
Guest User

Help, I'm trapped in an infinite factory!

a guest
Aug 16th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.23 KB | None | 0 0
  1.     -> grid, box_width, box_height {
  2.       total_width = grid[0].size
  3.       total_height = grid.size
  4.    
  5.       conveyors = {}
  6.    
  7.       (0...total_height).map{|y|
  8.         (0...total_width).map{|x|
  9.           w = grid[y][x]
  10.           conveyors[[y,x]] = w if w > ?!
  11.         }
  12.       }  
  13.    
  14.       move =-> y, x, dir, visited=[]{
  15.         directions = Hash.new 0
  16.    
  17.         [*y...y + box_height].map{|y|
  18.           [*x...x + box_width].map{|x|
  19.             w = conveyors[[y,x]]
  20.             directions[w] += 1 if w
  21.           }
  22.         }
  23.    
  24.         horizontal_force = directions[?>] - directions[?<]
  25.         vertical_force = directions[?v] - directions[?^]
  26.    
  27.         if horizontal_force.abs > vertical_force.abs
  28.           dir = :horizontal
  29.         end
  30.         if horizontal_force.abs < vertical_force.abs
  31.           dir = :vertical
  32.         end
  33.    
  34.         visited << [y, x]
  35.    
  36.         case dir
  37.         when :horizontal
  38.           x += horizontal_force <=> 0
  39.         when :vertical
  40.           y += vertical_force <=> 0
  41.         end
  42.    
  43.         return nil if visited[-1] == [y, x]
  44.         return 1 if visited.include? [y, x]
  45.    
  46.         move[y, x, dir, visited]
  47.       }
  48.    
  49.       move[0, 0, :horizontal]
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement