From b8e5d888bc82740405e8ae6c438473c8084bc3b4 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Mon, 16 Jan 2017 12:11:15 +0200 Subject: [PATCH 1/3] Move files into a relevant folders --- ascii_art.rb => Training/Easy/ascii_art.rb | 0 .../Easy/chuck_norris.rb | 0 .../Easy/defibrillators.rb | 0 .../Easy/horse_racing.rb | 0 mime_type.rb => Training/Easy/mime_type.rb | 0 .../Easy/power_of_thor.rb | 0 .../Easy/temperatures.rb | 0 .../Easy/the_descent.rb | 0 Training/Medium/spoon.rb | 57 +++++++++++++++++++ 9 files changed, 57 insertions(+) rename ascii_art.rb => Training/Easy/ascii_art.rb (100%) rename chuck_norris.rb => Training/Easy/chuck_norris.rb (100%) rename defibrillators.rb => Training/Easy/defibrillators.rb (100%) rename horse_racing.rb => Training/Easy/horse_racing.rb (100%) rename mime_type.rb => Training/Easy/mime_type.rb (100%) rename power_of_thor.rb => Training/Easy/power_of_thor.rb (100%) rename temperatures.rb => Training/Easy/temperatures.rb (100%) rename the_descent.rb => Training/Easy/the_descent.rb (100%) create mode 100644 Training/Medium/spoon.rb 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/spoon.rb b/Training/Medium/spoon.rb new file mode 100644 index 0000000..b4690c3 --- /dev/null +++ b/Training/Medium/spoon.rb @@ -0,0 +1,57 @@ +STDOUT.sync = true # DO NOT REMOVE +# Solution for https://www.codingame.com/ide/puzzle/there-is-no-spoon-episode-1 +class Spoon + attr_accessor :width, :height, :nodes + + def initialize + @width = gets.to_i # the number of cells on the X axis + @height = gets.to_i # the number of cells on the Y axis + @nodes = [] + init_nodes + end + + def init_nodes + n = 0 + height.times do + line = gets.chomp # width characters, each either 0 or . + find_node(line, n) + n += 1 + end + end + + def find_node(chars, n) + chars.each_char.with_index do |char, i| + next if char == '.' + + nodes.push([i, n]) + end + end + + def find_right_node(node) + nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min + end + + def find_bottom_node(node) + nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min + end + + def add_coordinates(result) + if result.nil? + ' -1 -1' + else + " #{result[0]} #{result[1]}" + end + end + + def start + nodes.each do |node| + result = "#{node[0]} #{node[1]}" + result += add_coordinates(find_right_node(node)) + result += add_coordinates(find_bottom_node(node)) + puts result + end + end +end + +obj = Spoon.new +obj.start From 7171fdeda2135fb5a94307b763b24c1cf80259be Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Tue, 17 Jan 2017 16:51:58 +0200 Subject: [PATCH 2/3] Solution for gift task --- Training/Medium/gift.rb | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Training/Medium/gift.rb diff --git a/Training/Medium/gift.rb b/Training/Medium/gift.rb new file mode 100644 index 0000000..f3a1cfb --- /dev/null +++ b/Training/Medium/gift.rb @@ -0,0 +1,42 @@ +# 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 + list = [] + participants.times do + list << gets.to_i + end + list.sort + end + + def not_possible? + if budgets_list.inject(0, :+) < gift_price + puts 'IMPOSSIBLE' + true + else + false + end + end + + def start + return if not_possible? + money_to_pay = gift_price + budgets_list.each_with_index do |budget, index| + contributions_count = budgets_list.size - index + contribution = (money_to_pay / contributions_count).to_i + contribution = budget if budget < contribution + money_to_pay -= contribution + puts contribution + end + end +end + +obj = Gift.new +obj.start From 756300834b9a054a01ffea6085a9634ccf04ec86 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Mon, 23 Jan 2017 16:24:00 +0200 Subject: [PATCH 3/3] Refactor not_possible? method Put print action out of the class and use each_with_object to fill array with contributions --- Training/Medium/gift.rb | 24 +++++++---------- Training/Medium/spoon.rb | 57 ---------------------------------------- 2 files changed, 9 insertions(+), 72 deletions(-) delete mode 100644 Training/Medium/spoon.rb diff --git a/Training/Medium/gift.rb b/Training/Medium/gift.rb index f3a1cfb..bd0a5f8 100644 --- a/Training/Medium/gift.rb +++ b/Training/Medium/gift.rb @@ -9,34 +9,28 @@ def initialize end def init_budgets_list - list = [] - participants.times do - list << gets.to_i - end - list.sort + Array.new(participants) { gets.to_i }.sort end def not_possible? - if budgets_list.inject(0, :+) < gift_price - puts 'IMPOSSIBLE' - true - else - false - end + budgets_list.inject(0, :+) < gift_price end def start - return if not_possible? + if not_possible? + puts 'IMPOSSIBLE' + return + end money_to_pay = gift_price - budgets_list.each_with_index do |budget, index| + 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 - puts contribution + result << contribution end end end obj = Gift.new -obj.start +puts obj.start diff --git a/Training/Medium/spoon.rb b/Training/Medium/spoon.rb deleted file mode 100644 index b4690c3..0000000 --- a/Training/Medium/spoon.rb +++ /dev/null @@ -1,57 +0,0 @@ -STDOUT.sync = true # DO NOT REMOVE -# Solution for https://www.codingame.com/ide/puzzle/there-is-no-spoon-episode-1 -class Spoon - attr_accessor :width, :height, :nodes - - def initialize - @width = gets.to_i # the number of cells on the X axis - @height = gets.to_i # the number of cells on the Y axis - @nodes = [] - init_nodes - end - - def init_nodes - n = 0 - height.times do - line = gets.chomp # width characters, each either 0 or . - find_node(line, n) - n += 1 - end - end - - def find_node(chars, n) - chars.each_char.with_index do |char, i| - next if char == '.' - - nodes.push([i, n]) - end - end - - def find_right_node(node) - nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min - end - - def find_bottom_node(node) - nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min - end - - def add_coordinates(result) - if result.nil? - ' -1 -1' - else - " #{result[0]} #{result[1]}" - end - end - - def start - nodes.each do |node| - result = "#{node[0]} #{node[1]}" - result += add_coordinates(find_right_node(node)) - result += add_coordinates(find_bottom_node(node)) - puts result - end - end -end - -obj = Spoon.new -obj.start