diff --git a/ascii_art.rb b/Training/Easy/ascii_art.rb similarity index 100% rename from ascii_art.rb rename to Training/Easy/ascii_art.rb diff --git a/chuck_norris.rb b/Training/Easy/chuck_norris.rb similarity index 100% rename from chuck_norris.rb rename to Training/Easy/chuck_norris.rb diff --git a/defibrillators.rb b/Training/Easy/defibrillators.rb similarity index 100% rename from defibrillators.rb rename to Training/Easy/defibrillators.rb diff --git a/horse_racing.rb b/Training/Easy/horse_racing.rb similarity index 100% rename from horse_racing.rb rename to Training/Easy/horse_racing.rb diff --git a/mime_type.rb b/Training/Easy/mime_type.rb similarity index 100% rename from mime_type.rb rename to Training/Easy/mime_type.rb diff --git a/power_of_thor.rb b/Training/Easy/power_of_thor.rb similarity index 100% rename from power_of_thor.rb rename to Training/Easy/power_of_thor.rb diff --git a/temperatures.rb b/Training/Easy/temperatures.rb similarity index 100% rename from temperatures.rb rename to Training/Easy/temperatures.rb diff --git a/the_descent.rb b/Training/Easy/the_descent.rb similarity index 100% rename from the_descent.rb rename to Training/Easy/the_descent.rb diff --git a/Training/Medium/gift.rb b/Training/Medium/gift.rb new file mode 100644 index 0000000..bd0a5f8 --- /dev/null +++ b/Training/Medium/gift.rb @@ -0,0 +1,36 @@ +# Solution for https://www.codingame.com/ide/puzzle/the-gift +class Gift + attr_accessor :participants, :gift_price, :budgets_list + + def initialize + @participants = gets.to_i + @gift_price = gets.to_i + @budgets_list = init_budgets_list + end + + def init_budgets_list + Array.new(participants) { gets.to_i }.sort + end + + def not_possible? + budgets_list.inject(0, :+) < gift_price + end + + def start + if not_possible? + puts 'IMPOSSIBLE' + return + end + money_to_pay = gift_price + budgets_list.each_with_index.each_with_object([]) do |(budget, index), result| + contributions_count = budgets_list.size - index + contribution = (money_to_pay / contributions_count).to_i + contribution = budget if budget < contribution + money_to_pay -= contribution + result << contribution + end + end +end + +obj = Gift.new +puts obj.start