Skip to content

Instantly share code, notes, and snippets.

@Antti
Last active January 1, 2016 05:09
Show Gist options
  • Select an option

  • Save Antti/8096603 to your computer and use it in GitHub Desktop.

Select an option

Save Antti/8096603 to your computer and use it in GitHub Desktop.
Resolve alphabet position encoded puzzle.
ALPHABET = ('a'..'z').to_a
code = '2114111925'
class WordTree
attr_accessor :l, :r, :my_letter, :string
def initialize(my_letter, string)
@string = string
@my_letter = my_letter
try_init_childs
end
def try_init_childs
@l = WordTree.new(@string[0..0].to_i, @string[1..-1]) if @string.size > 0
@r = WordTree.new(@string[0..1].to_i, @string[2..-1]) if @string.size > 1 && @string[0..1].to_i <= (ALPHABET.size+1)
end
end
def walk_tree(tree, word=[])
word = word+[tree.my_letter] if tree.my_letter
print_word word if tree.string.size == 0 #Reached the end of a chain (no more letters left)
walk_tree(tree.l, word) if tree.l
walk_tree(tree.r, word) if tree.r
end
def print_word(arr)
puts arr.map{|x| ALPHABET[x-1]}.join('')
end
x=WordTree.new(nil, code)
walk_tree(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment