#!/usr/bin/ruby N = 5 R = 5 C = 10 T = [[5, 5, 5, 5], [0, 5, 5, 5], [0, 0, 5, 5], [0, 0, 0, 5]] #the bay is organized by columns, since that's more likely #what we will be working with. bay = Array.new(C, Array.new(R, nil)) # Unloads all the containers for the current port # returns the number of unnecessary moves def unload!(bay, location) moves = 0 bay.each { |col| if col.include? location col.reverse_each { |x| if x == location x = nil else if x moves += 1 end end } compact!(bay, col) end } return moves end # shifts all the nil entries to the top # appropriately. def compact!(bay, col) end bay[0][0] = 1 bay[0][1] = 2 bay[0][2] = 1 p bay unload!(bay, 1) p bay