From e0dd12908e6e26ae3fb483e3a82ea16f5f4cacaa Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Fri, 20 Jan 2017 16:19:49 +0200 Subject: [PATCH 1/2] Solution for conway sequence --- Training/Medium/conway_sequence.rb | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Training/Medium/conway_sequence.rb diff --git a/Training/Medium/conway_sequence.rb b/Training/Medium/conway_sequence.rb new file mode 100644 index 0000000..ce8bd38 --- /dev/null +++ b/Training/Medium/conway_sequence.rb @@ -0,0 +1,39 @@ +# Solution for https://www.codingame.com/ide/puzzle/conway-sequence +class ConwaySequence + attr_accessor :first_number, :line_to_display, :sequence + + def initialize + @first_number = gets.to_i + @line_to_display = gets.to_i + end + + def init_sequence + sequence = {} + sequence[1] = [first_number] + line_to_display.times.each_with_index do |line, index| + last_line = sequence[index + 1] + sequence[index + 2] = next_line(last_line) + end + sequence + end + + def next_line(last_line) + count = 0 + result = [] + last_line.each_with_index do |num, index| + count += 1 + next if num == last_line[index + 1] + result << [count, num] + count = 0 + end + result.flatten + end + + def start + sequence = init_sequence + sequence[line_to_display].join(' ') + end +end + +obj = ConwaySequence.new +print obj.start From 516047638a51aed6808e96746afb6fd236188b1e Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Tue, 24 Jan 2017 16:00:23 +0200 Subject: [PATCH 2/2] Refactor the code Print outside of the class --- Training/Medium/conway_sequence.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Training/Medium/conway_sequence.rb b/Training/Medium/conway_sequence.rb index ce8bd38..7df95cc 100644 --- a/Training/Medium/conway_sequence.rb +++ b/Training/Medium/conway_sequence.rb @@ -1,39 +1,40 @@ # Solution for https://www.codingame.com/ide/puzzle/conway-sequence class ConwaySequence - attr_accessor :first_number, :line_to_display, :sequence + attr_reader :first_number, :line_to_display, :sequence - def initialize - @first_number = gets.to_i - @line_to_display = gets.to_i + def initialize(first_number, line_to_display) + @first_number = first_number + @line_to_display = line_to_display + @sequence = init_sequence end def init_sequence - sequence = {} - sequence[1] = [first_number] - line_to_display.times.each_with_index do |line, index| + Array.new(line_to_display).each_with_index.each_with_object({}) do |(_, index), sequence| + sequence[1] = [first_number] if index.zero? last_line = sequence[index + 1] sequence[index + 2] = next_line(last_line) end - sequence end def next_line(last_line) count = 0 - result = [] - last_line.each_with_index do |num, index| + last_line.each_with_index.each_with_object([]) do |(num, index), result| count += 1 next if num == last_line[index + 1] - result << [count, num] + + result << count + result << num + result.join(' ') count = 0 end - result.flatten end def start - sequence = init_sequence sequence[line_to_display].join(' ') end end -obj = ConwaySequence.new +first_number = gets.to_i +line_to_display = gets.to_i +obj = ConwaySequence.new(first_number, line_to_display) print obj.start