Skip to content

Instantly share code, notes, and snippets.

@noslowerdna
Last active June 10, 2019 21:42
Show Gist options
  • Save noslowerdna/58a919d4063293226418dcdc759b25e7 to your computer and use it in GitHub Desktop.
Save noslowerdna/58a919d4063293226418dcdc759b25e7 to your computer and use it in GitHub Desktop.
require 'set'
require 'prime'
letters = 3
max_word_length = 5
alphabet = (0...letters).collect { |i| ('A'.ord + i).chr }
all_words = SortedSet.new
for i in 1..max_word_length
words = SortedSet.new
slice_limit = i.prime? ? 1 : i / 2
alphabet.repeated_permutation(i).map {|permutation| permutation.join}.each do |word|
skip = false
double_word = (word * 2)
(0...i).collect { |j| double_word[j, i] }.each do |rotation|
skip = true if words.include? rotation
unless skip
for k in 1..slice_limit
skip = true if i % k == 0 && rotation.chars.each_slice(k).map(&:join).uniq.size == 1
break if skip
end
end
break if skip
end
words.add(word) unless skip
end
words.each { |word| puts word }
all_words += words
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment