Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 22 additions & 30 deletions Training/Medium/spoon.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@
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
attr_accessor :nodes
attr_reader :width, :height

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
def initialize(width, height)
@width = width # the number of cells on the X axis
@height = height # 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
Array.new(height) do |line_index|
line = gets.chomp
line.each_char.with_index do |char, char_index|
next if char == '.'

def find_node(chars, n)
chars.each_char.with_index do |char, i|
next if char == '.'

nodes.push([i, n])
nodes << [char_index, line_index]
end
end
end

def find_right_node(node)
nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min
nodes.select { |n| 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
nodes.select { |n| 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
result ? [result[0], result[1]] : [-1, -1]
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
nodes.map do |node|
result = node
result << add_coordinates(find_right_node(node))
result << add_coordinates(find_bottom_node(node))
result.flatten.join(' ')
end
end
end

obj = Spoon.new
obj.start
obj = Spoon.new(gets.to_i, gets.to_i)
answer = obj.start
puts answer
57 changes: 57 additions & 0 deletions spoon.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need accessors if you're not setting these either outside of the class, or by the use of self


def initialize
@width = gets.to_i # the number of cells on the X axis
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the gets statements outside of the method and give them as arguments to initialize

@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 .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the gets outside of the class

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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A shorter version of push is the << operator

end
end

def find_right_node(node)
nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way to use select is a little different - it expects a true/false statement. Meaning that you don't have to write if inside it.
So instead of

array.select { |n| n if some_condition(n) }

you should just write

array.select { |n| some_condition(n) }

end

def find_bottom_node(node)
nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in find_right_node

end

def add_coordinates(result)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not rely on add_coordinates to add the whitespace. Think of a way to take that logic out of that method, so that the method only returns the pair of coordinates without whitespace

if result.nil?
' -1 -1'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use an array to hold coordinates instead of a string

else
" #{result[0]} #{result[1]}"
end
end

def start
nodes.each do |node|
result = "#{node[0]} #{node[1]}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single node is a pair of [x, y]. In order to obtain a string with a whitespace of the two elements, you can use

result = node.join(' ')

result += add_coordinates(find_right_node(node))
result += add_coordinates(find_bottom_node(node))
puts result
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take the puts outside of the class

end
end
end

obj = Spoon.new
obj.start