Skip to content

Instantly share code, notes, and snippets.

@Heavyblade
Created October 16, 2020 21:55
Show Gist options
  • Save Heavyblade/87e10bee59da7204d73b9f271a7ece1f to your computer and use it in GitHub Desktop.
Save Heavyblade/87e10bee59da7204d73b9f271a7ece1f to your computer and use it in GitHub Desktop.
require "pry"
require "pp"
class Point
attr_accessor :x, :y, :n, :mov, :history
def initialize(x, y, n, first, last, history = nil)
@x = x
@y = y
@n = n
@first = first
@last = last
@history = history
end
def can_move_up_left?
(@y - @first >= 0) && (@x - @last >= 0)
end
def can_move_up_right?
(@y - @first >= 0) && (@x + @last <= @n)
end
def can_move_down_left?
(@y + @first <= @n) && (@x - @last >= 0)
end
def can_move_down_right?
(@y + @first <= @n) && (@x + @last <= @n)
end
def can_move_right_up?
(@x + @first <= @n) && (@y - @last >= 0)
end
def can_move_right_down?
(@x + @first <= @n) && (@y + @last <= @n)
end
def can_move_left_up?
(@x - @first >= 0) && (@y - @last >= 0)
end
def can_move_left_down?
(@x - @first >= 0) && (@y + @last <= @n)
end
def posible_positions
possible = []
possible << Point.new(@x - @last, @y - @first, @n, @first, @last) if can_move_up_left? && not_in_history(@x - @last, @y - @first)
possible << Point.new(@x + @last, @y - @first, @n, @first, @last) if can_move_up_right? && not_in_history(@x + @last, @y - @first)
possible << Point.new(@x - @last, @y + @first, @n, @first, @last) if can_move_down_left? && not_in_history(@x - @last, @y + @first)
possible << Point.new(@x + @last, @y + @first, @n, @first, @last) if can_move_down_right? && not_in_history(@x + @last, @y + @first)
possible << Point.new(@x + @first, @y - @last, @n, @first, @last) if can_move_right_up? && not_in_history(@x + @first, @y - @last)
possible << Point.new(@x + @first, @y + @last, @n, @first, @last) if can_move_right_down? && not_in_history(@x + @first, @y + @last)
possible << Point.new(@x - @first, @y - @last, @n, @first, @last) if can_move_left_up? && not_in_history(@x - @first, @y - @last)
possible << Point.new(@x - @first, @y + @last, @n, @first, @last) if can_move_left_down? && not_in_history(@x - @first, @y + @last)
possible.uniq! do |position|
"#{position.x}#{position.y}"
end
if possible.length > 0
hist = history.dup
hist << [x,y]
possible.each { |pos| pos.history = hist }
end
possible
end
def finished?
x == n && y == n
end
def not_in_history(x, y)
!@history.any? { |point| point.first === x && point.last === y }
end
def find_path
return (history << [x, y]) if finished?
positions = posible_positions
return nil if positions.length == 0
clean = positions.map(&:find_path)
clean.compact!
clean.min { |a, b| a.length <=> b.length }
end
end
def knightlOnAChessboard(n)
n = n -1
(1..n).to_a.map do |y|
(1..n).to_a.map do |x|
(Point.new(0,0, n, y, x, []).find_path || []).length - 1
end
end
end
pp knightlOnAChessboard(5)
# [[4, 4, 2, 8], [4, 2, 4, 4], [2, 4, -1, -1], [8, 4, -1, 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment