BIG DIGITS(CodeEval)

hatenaでは見にくいか。

CHALLENGE DESCRIPTION:

In this challenge you're presented with a situation in which you need to output big symbols on devices which only support ASCII characters and single, fixed-width fonts. To do this you're going to use pseudo-graphics to ‘draw’ these big symbols.

Here is an example of the font with digits from 0 to 9:

-**----*--***--***---*---****--**--****--**---**--
*--*--**-----*----*-*--*-*----*-------*-*--*-*--*-
*--*---*---**---**--****-***--***----*---**---***-
*--*---*--*-------*----*----*-*--*--*---*--*----*-
-**---***-****-***-----*-***---**---*----**---**--
--------------------------------------------------

Each pixel is marked either with asterisk ‘*’ or with minus ‘-’. Size of a digit is 5×6 pixels.

Your task is to write a program, which outputs the numbers given to you with the font as in the example.

INPUT SAMPLE:

The first argument is a file that contains the lines with digits sequences you need to magnify. E.g.:

3.1415926
1.41421356
01-01-1970
2.7182818284
4 8 15 16 23 42

OUTPUT SAMPLE:

Print to stdout the magnified digits:

***----*---*-----*--****--**--***---**--
---*--**--*--*--**--*----*--*----*-*----
-**----*--****---*--***---***--**--***--
---*---*-----*---*-----*----*-*----*--*-
***---***----*--***-***---**--****--**--
----------------------------------------
--*---*-----*---*---***----*--***--****--**--
-**--*--*--**--*--*----*--**-----*-*----*----
--*--****---*--****--**----*---**--***--***--
--*-----*---*-----*-*------*-----*----*-*--*-
-***----*--***----*-****--***-***--***---**--
---------------------------------------------
-**----*---**----*----*---**--****--**--
*--*--**--*--*--**---**--*--*----*-*--*-
*--*---*--*--*---*----*---***---*--*--*-
*--*---*--*--*---*----*-----*--*---*--*-
-**---***--**---***--***--**---*----**--
----------------------------------------
***--****---*---**--***---**----*---**--***---**---*---
---*----*--**--*--*----*-*--*--**--*--*----*-*--*-*--*-
-**----*----*---**---**---**----*---**---**---**--****-
*-----*-----*--*--*-*----*--*---*--*--*-*----*--*----*-
****--*----***--**--****--**---***--**--****--**-----*-
-------------------------------------------------------
-*----**----*--****---*---**--***--***---*---***--
*--*-*--*--**--*-----**--*-------*----*-*--*----*-
****--**----*--***----*--***---**---**--****--**--
---*-*--*---*-----*---*--*--*-*-------*----*-*----
---*--**---***-***---***--**--****-***-----*-****-
--------------------------------------------------

CONSTRAINTS:

  1. Input lines are up to 16 symbols long.
  2. Input can contain some other symbols, which should be ignored (i.e. points, hyphens, spaces); only numbers must be printed out.

My Code

#!/usr/bin/env ruby -w
class String
  def unindent
    margin = scan(/^ +/).map(&:size).min
    gsub(/^ {#{margin}}/, '')
  end
end

class BigDigitConverter
  def print(chars)
    rtn = Array.new(6)
    chars.each_char do |c|
      big_digit = convert(c)
      rtn = rtn.zip(big_digit) if big_digit
    end
    rtn.each { |line| puts line.join }
  end

  private
  def convert(c)
    str =
      case c
      when '0' then zero
      when '1' then one
      when '2' then two
      when '3' then three
      when '4' then four
      when '5' then five
      when '6' then six
      when '7' then seven
      when '8' then eight
      when '9' then nine
      else return nil
      end
    str.each_line.map(&:chomp)
  end

  def zero
    <<-EOF.unindent
    -**--
    *--*-
    *--*-
    *--*-
    -**--
    -----
    EOF
  end

  def one
    <<-EOF.unindent
    --*--
    -**--
    --*--
    --*--
    -***-
    -----
    EOF
  end

  def two
    <<-EOF.unindent
    ***--
    ---*-
    -**--
    *----
    ****-
    -----
    EOF
  end

  def three
    <<-EOF.unindent
    ***--
    ---*-
    -**--
    ---*-
    ***--
    -----
    EOF
  end

  def four
    <<-EOF.unindent
    -*---
    *--*-
    ****-
    ---*-
    ---*-
    -----
    EOF
  end

  def five
    <<-EOF.unindent
    ****-
    *----
    ***--
    ---*-
    ***--
    -----
    EOF
  end

  def six
    <<-EOF.unindent
    -**--
    *----
    ***--
    *--*-
    -**--
    -----
    EOF
  end

  def seven
    <<-EOF.unindent
    ****-
    ---*-
    --*--
    -*---
    -*---
    -----
    EOF
  end

  def eight
    <<-EOF.unindent
    -**--
    *--*-
    -**--
    *--*-
    -**--
    -----
    EOF
  end

  def nine
    <<-EOF.unindent
    -**--
    *--*-
    -***-
    ---*-
    -**--
    -----
    EOF
  end
end

ARGF.each_line do |line|
  converter = BigDigitConverter.new
  converter.print(line.chomp)
end