Решение на Шеста задача от Недислав Стойчев

Обратно към всички решения

Към профила на Недислав Стойчев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 17 успешни тест(а)
  • 1 неуспешни тест(а)

Код

require 'set'
module GameOfLife
class Board
include Enumerable
attr_accessor :cells
def initialize(*args)
@cells = args.map do |x|
if x.is_a? Cell
x
else
Cell.new(*x)
end
end
end
def each
cells.each do |x|
yield [x.x, x.y]
end
end
def [](x,y)
cells.include? Cell.new(x, y)
end
def next_generation
cell_next_gen
out = Board.new(*cell_next_gen)
end
def cell_next_gen
output = cells.inject([]) do |new_cells, cell|
new_cells | cell.next_gen_live(cells) | cell.next_gen_reproduction(cells)
end
end
end
class Cell
attr_accessor :x,:y
Displacements = [[0, 1], [0, -1], [1, 0], [-1, 0],
[1, 1], [-1, -1], [1, -1], [-1, 1]]
def initialize(x, y)
@x = x
@y = y
end
def ==(other)
x == other.x && y == other.y
end
def displace(dx, dy)
Cell.new(x + dx, y + dy)
end
def adjancent_cells
Displacements.map { |x, y| displace x, y }
end
def next_gen_live(cells)
if [2,3].include? live_adjancent(cells)
[self]
else
[]
end
end
def next_gen_reproduction(cells)
adjancent_cells.select { |cell| cell.live_adjancent(cells) == 3}
end
def live_adjancent(cells)
adjancent_cells.count { |c| cells.include? c }
end
def eql?(other)
self == other
end
def hash
[x, y].hash
end
def inspect
"Cell at: [#{x}, #{y}]"
end
end
end

Лог от изпълнението

......F...........

Failures:

  1) GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
     Failure/Error: new_board([0, 0], [1, 1], [0, 0]).count.should eq 2
       
       expected: 2
            got: 3
       
       (compared using ==)
     # /tmp/d20111221-3114-1t2v7rm/spec.rb:46:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.47293 seconds
18 examples, 1 failure

Failed examples:

rspec /tmp/d20111221-3114-1t2v7rm/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board

История (1 версия и 0 коментара)

Недислав обнови решението на 19.12.2011 17:21 (преди над 8 години)

+require 'set'
+
+module GameOfLife
+
+ class Board
+ include Enumerable
+ attr_accessor :cells
+
+ def initialize(*args)
+ @cells = args.map do |x|
+ if x.is_a? Cell
+ x
+ else
+ Cell.new(*x)
+ end
+ end
+ end
+
+ def each
+ cells.each do |x|
+ yield [x.x, x.y]
+ end
+ end
+
+ def [](x,y)
+ cells.include? Cell.new(x, y)
+ end
+
+ def next_generation
+ cell_next_gen
+ out = Board.new(*cell_next_gen)
+ end
+
+ def cell_next_gen
+ output = cells.inject([]) do |new_cells, cell|
+ new_cells | cell.next_gen_live(cells) | cell.next_gen_reproduction(cells)
+ end
+ end
+ end
+
+ class Cell
+ attr_accessor :x,:y
+ Displacements = [[0, 1], [0, -1], [1, 0], [-1, 0],
+ [1, 1], [-1, -1], [1, -1], [-1, 1]]
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ end
+
+ def ==(other)
+ x == other.x && y == other.y
+ end
+
+ def displace(dx, dy)
+ Cell.new(x + dx, y + dy)
+ end
+
+ def adjancent_cells
+ Displacements.map { |x, y| displace x, y }
+ end
+
+ def next_gen_live(cells)
+ if [2,3].include? live_adjancent(cells)
+ [self]
+ else
+ []
+ end
+ end
+
+ def next_gen_reproduction(cells)
+ adjancent_cells.select { |cell| cell.live_adjancent(cells) == 3}
+ end
+
+ def live_adjancent(cells)
+ adjancent_cells.count { |c| cells.include? c }
+ end
+
+ def eql?(other)
+ self == other
+ end
+
+ def hash
+ [x, y].hash
+ end
+
+ def inspect
+ "Cell at: [#{x}, #{y}]"
+ end
+ end
+end