Created
January 25, 2022 05:02
-
-
Save litonico/ec585b67374676a777a2553a2384cd17 to your computer and use it in GitHub Desktop.
This file contains 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
def sp(s) | |
s.split("") | |
end | |
# should i do this with primes | |
def sub_anagram(haystack, needle) | |
is_anagram = true | |
leftovers = sp(haystack) | |
sp(needle).each do |letter| | |
index = leftovers.index(letter) | |
if index == nil | |
is_anagram = false | |
break | |
end | |
leftovers.delete_at(index) | |
end | |
return [is_anagram, leftovers.join] | |
end | |
TIMOTHEE = "timotheehalchalamet" | |
# # can simplify the dictionary; for instance, timothee does not contain a "z" | |
# def letters_not_in(word) | |
# "abcdefghijklmnopqrstuvwxyz".chars.select do |c| | |
# !word.include? c | |
# end | |
# end | |
# | |
# WHOLE_DICTIONARY = File.read("words").split("\n").map(&:downcase) | |
# puts "simplifying the dictionary" | |
# | |
# DICTIONARY = [] | |
# WHOLE_DICTIONARY.each do |word| | |
# acceptable = true | |
# letters_not_in(TIMOTHEE).each do |c| | |
# if word.include? c | |
# acceptable = false | |
# end | |
# end | |
# if acceptable | |
# DICTIONARY << word | |
# end | |
# end | |
# | |
# DICTIONARY.sort_by! do |word| | |
# word.length | |
# end | |
# DICTIONARY.reverse! | |
# puts "dict length:" | |
# puts DICTIONARY.length | |
# File.write("timdict", DICTIONARY.join("\n")) | |
DICTIONARY = File.read("timdict").split("\n").map(&:downcase).uniq | |
Node = Struct.new(:word, :leftovers, :children, :parent) | |
PERFECTION = [] | |
puts "looking for good anagrams" | |
def perfect_anagrams(word_to_anagram_node, dictionary) | |
dictionary.each do |dict_word| | |
is_anagram, leftovers = sub_anagram(word_to_anagram_node.leftovers, dict_word) | |
if is_anagram | |
node = Node.new(dict_word, leftovers, [], word_to_anagram_node) | |
if leftovers == "" | |
ana = [] | |
while node.parent != nil | |
ana << node.word | |
node = node.parent | |
end | |
PERFECTION << ana | |
if PERFECTION.length % 1000 == 0 | |
File.write("out.txt", PERFECTION.map(&:sort).uniq.map { |x| x.join(" ") }.join("\n")) | |
puts "#{PERFECTION.map(&:sort).uniq.length} anagrams found total" | |
end | |
next | |
end | |
word_to_anagram_node.children << perfect_anagrams(node, dictionary) | |
end | |
end | |
return word_to_anagram_node | |
end | |
perfect_anagrams(Node.new(nil, TIMOTHEE, [], nil), DICTIONARY) | |
puts "DONE" | |
File.write("perfect anagrams", PERFECTION.map(&:sort).uniq.map { |x| x.join(" ") }.join("\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment