Last active
January 1, 2016 05:09
-
-
Save Antti/8096603 to your computer and use it in GitHub Desktop.
Resolve alphabet position encoded puzzle.
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
| 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