Created
July 6, 2019 18:54
-
-
Save codesnik/83f1e0b8927cd06dc33ae62fdf7aa986 to your computer and use it in GitHub Desktop.
find permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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