Skip to content

Instantly share code, notes, and snippets.

@bmarkons
Created November 20, 2021 21:56
Show Gist options
  • Save bmarkons/f55d12a80ea881f2e414367791c120b1 to your computer and use it in GitHub Desktop.
Save bmarkons/f55d12a80ea881f2e414367791c120b1 to your computer and use it in GitHub Desktop.
Finding longest kaladont sequence
words = File.read("input.txt").split
def get(words, beginning, sequence)
words[beginning].select { |word| !sequence.include?(word) } if words[beginning]
end
def kalodont(words, sequence)
selected_words = get(words, sequence.last[-2..-1], sequence)
if selected_words && !selected_words.empty?
selected_words.each do |word|
kalodont(words, sequence + [word])
end
elsif sequence.size > $max
$max = sequence.size
puts "******* NEW BIGGEST SEQUENCE WITH #{sequence.size} CHARACTERS *********"
File.write("sequence.txt", sequence.join(" "))
end
end
$max = 0
words_indexed_by_beginning = words.group_by { |word| word[0..1] }
words.each do |word|
kalodont(words_indexed_by_beginning, [word])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment