Решение на Втора задача от Добромир Иванов

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

Към профила на Добромир Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 18 успешни тест(а)
  • 2 неуспешни тест(а)

Код

def sum_arrays(a, b)
a.map.with_index {|element, i| element + b[i]}
end
def cells(width, height)
cell_count = [width, height].max
cells = [*0..cell_count].repeated_permutation(2).to_a
cells.reject {|cell| cell[0] >= width or cell[1] >= height}
end
def move(snake, direction)
new_snake = snake + [sum_arrays(snake.last, direction)]
new_snake.shift
new_snake
end
def grow(snake, direction)
snake + [sum_arrays(snake.last, direction)]
end
def new_food(food, snake, dimensions)
(cells(dimensions[:width], dimensions[:height]) - snake - food).sample
end
def obstacle_ahead?(snake, direction, dimensions)
snake = move(snake, direction)
cells = cells(dimensions[:width], dimensions[:height])
snake.count(snake.last) > 1 or !cells.include?(snake.last)
end
def danger?(snake, direction, dimensions)
moved_snake_a = move(snake, direction)
moved_snake_b = move(moved_snake_a, direction)
obstacle_ahead?(moved_snake_a, direction, dimensions) or
obstacle_ahead?(moved_snake_b, direction, dimensions)
end

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

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

Failures:

  1) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-slc2bm/spec.rb:83:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) #danger? returns false if obstacle in three turns
     Failure/Error: expect(danger?([[5, 6], [6, 6], [7, 6]], [1, 0], dimensions)).to eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20151026-15631-slc2bm/spec.rb:117:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01871 seconds
20 examples, 2 failures

Failed examples:

rspec /tmp/d20151026-15631-slc2bm/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead
rspec /tmp/d20151026-15631-slc2bm/spec.rb:116 # #danger? returns false if obstacle in three turns

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

Добромир обнови решението на 19.10.2015 01:25 (преди над 9 години)

+def sum_arrays(a, b)
+ a.map.with_index {|element, i| element + b[i]}
+end
+
+def cells(width, height)
+ cell_count = [width, height].max
+ cells = [*0..cell_count].repeated_permutation(2).to_a
+ cells.reject {|cell| cell[0] >= width or cell[1] >= height}
+end
+
+def move(snake, direction)
+ new_snake = snake + [sum_arrays(snake.last, direction)]
+ new_snake.shift
+ new_snake
+end
+
+def grow(snake, direction)
+ snake + [sum_arrays(snake.last, direction)]
+end
+
+def new_food(food, snake, dimensions)
+ (cells(dimensions[:width], dimensions[:height]) - snake - food).sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ snake = move(snake, direction)
+ cells = cells(dimensions[:width], dimensions[:height])
+
+ snake.count(snake.last) > 1 or !cells.include?(snake.last)
+end
+
+def danger?(snake, direction, dimensions)
+ moved_snake_a = move(snake, direction)
+ moved_snake_b = move(moved_snake_a, direction)
+
+ obstacle_ahead?(moved_snake_a, direction, dimensions) or
+ obstacle_ahead?(moved_snake_b, direction, dimensions)
+end