Skip to content

Instantly share code, notes, and snippets.

@Onikoroshi
Created February 17, 2018 19:41
Show Gist options
  • Save Onikoroshi/ff9b0c84209e2abbe0aeff889ef3bc3c to your computer and use it in GitHub Desktop.
Save Onikoroshi/ff9b0c84209e2abbe0aeff889ef3bc3c to your computer and use it in GitHub Desktop.
distribute prioritization
destinations = [
{ priority: 1, destination_id: "A", trucks: 2, still_needed: 2 },
{ priority: 2, destination_id: "B", trucks: 2, still_needed: 2 },
{ priority: 3, destination_id: "C", trucks: 1, still_needed: 1 }
]
def next_destination
# reset if everyone has been fully supplied
if destinations.select{|dest| dest[:still_needed] == 0}.count == destinations.count
destinations.map{|dest| dest[:still_needed] = dest[:trucks]}
end
destinations_sorted_by_still_needed.first[:destination_id]
end
def destinations_sorted_by_still_needed
destinations.sort{|a, b| a[:still_needed] <=> b[:still_needed] == 0 ? a[:priority] <=> b[:priority] : a[:still_needed] <=> b[:still_needed]}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment