Receipt Bank Logo + Balkan Ruby Logo

OCR number

OCR a number

OCR a number drawn on a board in ASCII art.

Input

A multi-line string representing the board. The number on the board is a sequence of digits, each digit fitting into a 3x3 square. This means that the board is represented by 3 lines, each of length 3 * the number of digits. There is only one way each digit is drawn as (the example shows how they all look).

Output

The OCRed number.

Example:

board = " _  _  _  _  _     _  _     _ \n" \
        "|_||_|  ||_ |_ |_| _| _|  || |\n" \
        " _||_|  ||_| _|  | _||_   ||_|\n"

ocr_number board  # => 9876543210

Leader board

The lower the score - the better. It is the shortest solution that wins.

If you're ranked in the first 10, stop by our booth to claim your reward.
Winner!

Rank: 1
Nickname: ssk
Score: 105

def ocr_number s
  eval s.lines.map { |l| l.scan /.../ }.transpose.map { |d|
    % n9uCyRzS8r .index (d * %  ).crypt(% aa )[2]
  } * %  
end
Winner!

Rank: 2
Nickname: hv
Score: 106

def ocr_number b
eval b.lines.map { |l| l.scan /.../ }.transpose.map { |x| % kwcEoYQIgU .index (x* %  ).crypt(% 12 )[-1] }* %  
end
Winner!

Rank: 3
Nickname: ssk2
Score: 133

def ocr_number s
  eval s.lines.map { |l| l.scan /.../ }.transpose.map { |d|
    $_ = d * %  
    [
      / . .._/,
      /^    /,
      / $/,
      / _  _/,
      /^  /,
      / ._  /,
      / ._ /,
      /  /,
      /\S{6}/,
      //
    ].index &:~
  } * %  
end
Winner!

Rank: 4
Nickname: vizvamitra
Score: 137

def ocr_number b
  eval b.lines.map{|x|x.scan(/.../)}.transpose.map{|c|%w[af 9 9e 9b 39 b3 b7 89 bf bb].index(% %x %c.join.tr(' _|',% 011 ).to_i(2))}.join
end
Winner!

Rank: 5
Nickname: rb-bikezilla-mauron
Score: 149

def ocr_number s
    m = <<E.split "\n"
 _ | ||
     | 
 _  _||
 _  _| 
   |_| 
 _ |_  
 _ |_ |
 _   | 
 _ |_||
 _ |_| 
E

    s.each_line.map { |l|
      l.scan /.../
    }.reduce(:zip).map { |n|
      m.index n.join[0..-3]
    }.join.to_i
  end
Winner!

Rank: 6
Nickname: Upnext
Score: 162

def ocr_number b
  s = b.tr!("
",'').size
  r = [0]*(s/9)
  0.upto(s-1) { |i|
    r[i % (s/3)/3] += b[i].ord * (i%3+1)*(i*3/s+1)
    }
  Integer r.map { |e| %w[4 8 9 3 12 7 15 0 10 2].index (e%200/9-6).to_s }*""
end
Winner!

Rank: 6
Nickname: Gosho337
Score: 162

def ocr_number b
b.lines.map {|x| x.scan /.../}.transpose.map {|x|
  ['_ | ||_', '    |  ', '_  _||_', '_  _| _', '  |_|  ', '_ |_  _', '_ |_ |_', '_   |  ', '_ |_||_', '_ |_| _'].index x.join[1..7]
}.join.to_i
end
Winner!

Rank: 8
Nickname: meerkat
Score: 174

def ocr_number b
  r = b.each_line.map { |f| f.scan /.../ }
  r[0].zip(r[1], r[2]).map { |d|
    d = d.join
    [
      1, 
      7, 
      4, 
      d[3] == ?| ? 5 : (d[6] == ?| ? 2 : 3), 
      d[4] == ' ' ? 0 : (d[5] == ?| ? 9 : 6), 
      8
    ][d.count('|_') - 2]
  }.join.to_i
end
Winner!

Rank: 9
Nickname: Hiroyuki
Score: 196

def ocr_number b
  o = ""
  l = b.lines
  m = l[1].size
  (0...m/3).map { |x|
    (" _ | ||_|     |  | _  _||_  _  _| _|   |_|  | _ |_  _| _ |_ |_| _   |  | _ |_||_| _ |_| _|".index((0..8).map {|i|
      l.map{|y| ' '*(m-y.size)+y }[i/3][3*x+i%3]
    }.join)/9).to_s
  }.join.to_i
end
Winner!

Rank: 10
Nickname: svetlio
Score: 309

M = [
  {
    '   ' => [1, 4],
    ' _ ' => [0, 2, 3, 5, 6, 7, 8, 9]
  },
  {
    '|_|' => [9, 8, 4],
    '  |' => [7, 1],
    '|_ ' => [6, 5],
    ' _|' => [2, 3],
    '| |' => [0]
  },
  {
    ' _|' => [9, 5, 3],
    '|_|' => [8, 6, 0],
    '  |' => [7, 4, 1],
    '|_ ' => [2]
  }
]

def ocr_number s
  t, m, b = s.split("\n").each_with_index.map { |l, i|
    l.scan(/(.{3})/).map { |e| M[i][e[0]] }
  }

  t.each_with_index.map { |e, i| (t[i] & m[i] & b[i])[0] }.join.to_i
end

Rank: 11
Nickname: meh
Score: 351

# guvnokod
S = {' ' => 0, '_' => 1, '|' => 5}
M = [44, 30, 26, 36, 37, 26, 31, 32, 46, 41]

def ocr_number b 
  f = -> s {
    return 5 if s == " _ \n|_ \n _|"
    num = s.split("\n").map { |e| e.chars.each_with_index.map { |y, i| (i+1) * S[y] }}.flatten.reduce(&:+)
    M.index(num)
  }
  h = -> b {
    x = b.split("\n").map {|a| a[0..2]}.join("\n")
    y = b.split("\n").map {|a| a[3..-1]}.join("\n")
    x.strip.empty? ? [] : [f[x]] + h[y]
  }

  h[b].join().to_i
end