Skip to content

Instantly share code, notes, and snippets.

@codesnik
Created July 6, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codesnik/83f1e0b8927cd06dc33ae62fdf7aa986 to your computer and use it in GitHub Desktop.
Save codesnik/83f1e0b8927cd06dc33ae62fdf7aa986 to your computer and use it in GitHub Desktop.
find permutations
# find arrangements of numbers from 1 to 8, so that
# if they're put into matrix like this, no consequentive numbers
# would "touch" directly or diagonally.
# 0 1
# 2 3 4 5
# 6 7
next_neighbours = [[1,2,3,4], [3,4,5], [3,6], [4,6,7], [5,6,7], [7], [7]]
result = [*1..8].permutation.filter do |row|
next_neighbours.each_with_index.none? do |neighbours, current|
neighbours.any? do |neighbour|
(row[current] - row[neighbour]).abs == 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment