Last active
April 19, 2022 08:58
-
-
Save pandwoter/8296c351651bb902a1a9fd45112f789f to your computer and use it in GitHub Desktop.
BARBER SHOP
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
def calculate_time(customers_in_line, barbers_speed) | |
customers_queue = Queue.new | |
customers_in_line.times { |i| customers_queue << i } | |
required_time = 0 | |
barbers_progress = {} | |
barbers_speed.sort.each.with_index do |speed, idx| | |
customers_queue.pop(true) | |
barbers_progress[idx] = { time_tooked: speed, speed: speed } | |
end | |
completed = false | |
while true | |
required_time += 1 | |
barbers_progress = barbers_progress.map do |k, v| | |
if v[:time_tooked] == required_time | |
if customers_queue.empty? | |
completed = true | |
return v[:time_tooked] | |
end | |
cust = customers_queue.pop | |
[k, {**v, time_tooked: v[:time_tooked] + v[:speed]}] | |
else | |
[k, v] | |
end | |
end.to_h | |
end | |
required_time | |
rescue ThreadError | |
return "No need to wait (you're processed immediately)" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment