diff --git a/Training/Easy/ascii_art.rb b/Training/Easy/ascii_art.rb index 46a0cf2..fe176cb 100644 --- a/Training/Easy/ascii_art.rb +++ b/Training/Easy/ascii_art.rb @@ -1,28 +1,41 @@ # Solution for https://www.codingame.com/ide/puzzle/ascii-art class AsciiArt - attr_accessor :ascii_style, :letter_width, :letter_height, :word, :lines + attr_accessor :ascii_style, :letter_width, :letter_height, :word def initialize - @ascii_style = ('A'..'Z').to_a << '?' @letter_width = gets.to_i @letter_height = gets.to_i @word = gets.chomp end - def letter_index(letter) - index = ascii_style.find_index(letter.upcase) - index ? index : ascii_style.size - 1 - end - def start - letter_height.times do + Array.new(letter_height) do row = gets.chomp splitted_row = row.scan(/.{#{letter_width}}|./) - line = word.chars.map { |letter| splitted_row[letter_index(letter)] }.join - puts line + word.chars.map { |char| AsciiArt::Letter.new(splitted_row, char).in_ascii }.join + end + end + + # Represent letter + class Letter + attr_accessor :row, :char + LETTERS = ('A'..'Z').to_a << '?' + + def initialize(row, char) + @row = row + @char = char + end + + def letter_index(letter) + index = LETTERS.find_index(letter.upcase) + index ? index : LETTERS.size - 1 + end + + def in_ascii + row[letter_index(char)] end end end obj = AsciiArt.new -obj.start +puts obj.start.join("\n")