-
Notifications
You must be signed in to change notification settings - Fork 0
[There is no Spoon] Solution for There is no Spoon puzzle #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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 | ||
|
|
||
| def initialize | ||
| @width = gets.to_i # the number of cells on the X axis | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract the |
||
| @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 . | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave the |
||
| 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]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A shorter version of |
||
| end | ||
| end | ||
|
|
||
| def find_right_node(node) | ||
| nodes.select { |n| n if n[1] == node[1] && n[0] > node[0] }.min | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way to use you should just write |
||
| end | ||
|
|
||
| def find_bottom_node(node) | ||
| nodes.select { |n| n if n[0] == node[0] && n[1] > node[1] }.min | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as in |
||
| end | ||
|
|
||
| def add_coordinates(result) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not rely on |
||
| if result.nil? | ||
| ' -1 -1' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]}" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 += add_coordinates(find_right_node(node)) | ||
| result += add_coordinates(find_bottom_node(node)) | ||
| puts result | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take the |
||
| end | ||
| end | ||
| end | ||
|
|
||
| obj = Spoon.new | ||
| obj.start | ||
There was a problem hiding this comment.
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