Created
February 17, 2018 19:41
-
-
Save Onikoroshi/ff9b0c84209e2abbe0aeff889ef3bc3c to your computer and use it in GitHub Desktop.
distribute prioritization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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