ASCII Addition

난이도

Gold 5

인증

문제

Nowadays, there are smartphone applications that instantly translate text and even solve math problems if you just point your phone’s camera at them. Your job is to implement a much simpler functionality reminiscent of the past – add two integers written down as ASCII art.

An ASCII art is a matrix of characters, exactly 7 rows high, with each individual character either a dot or the lowercase letter x.

An expression of the form a + b is given, where both a and b are positive integers. The expression is converted into ASCII art by writing all the expression characters (the digits of a and b as well as the + sign) as 7 × 5 matrices, and concatenating the matrices together with a single column of dot characters between consecutive individual matrices. The exact matrices corresponding to the digits and the + sign are as folows:

xxxxx ….x xxxxx xxxxx x…x xxxxx xxxxx xxxxx xxxxx xxxxx ….. x…x ….x ….x ….x x…x x…. x…. ….x x…x x…x ..x.. x…x ….x ….x ….x x…x x…. x…. ….x x…x x…x ..x.. x…x ….x xxxxx xxxxx xxxxx xxxxx xxxxx ….x xxxxx xxxxx xxxxx x…x ….x x…. ….x ….x ….x x…x ….x x…x ….x ..x.. x…x ….x x…. ….x ….x ….x x…x ….x x…x ….x ..x.. xxxxx ….x xxxxx xxxxx ….x xxxxx xxxxx ….x xxxxx xxxxx ….. Given an ASCII art for an expression of the form a + b, find the result of the addition and write it out in the ASCII art form.

입력

Input consists of exactly 7 lines and contains the ASCII art for an expression of the form a + b, where both a and b are positive integers consisting of at most 9 decimal digits and written without leading zeros.

출력

Output 7 lines containing ASCII art corresponding to the result of the addition, without leading zeros.

예제 입력

....x.xxxxx.xxxxx.x...x.xxxxx.xxxxx.xxxxx.......xxxxx.xxxxx.xxxxx
....x.....x.....x.x...x.x.....x.........x...x...x...x.x...x.x...x
....x.....x.....x.x...x.x.....x.........x...x...x...x.x...x.x...x
....x.xxxxx.xxxxx.xxxxx.xxxxx.xxxxx.....x.xxxxx.xxxxx.xxxxx.x...x
....x.x.........x.....x.....x.x...x.....x...x...x...x.....x.x...x
....x.x.........x.....x.....x.x...x.....x...x...x...x.....x.x...x
....x.xxxxx.xxxxx.....x.xxxxx.xxxxx.....x.......xxxxx.xxxxx.xxxxx

예제 출력

....x.xxxxx.xxxxx.xxxxx.x...x.xxxxx.xxxxx
....x.....x.....x.x.....x...x.x.........x
....x.....x.....x.x.....x...x.x.........x
....x.xxxxx.xxxxx.xxxxx.xxxxx.xxxxx.....x
....x.x.........x.....x.....x.....x.....x
....x.x.........x.....x.....x.....x.....x
....x.xxxxx.xxxxx.xxxxx.....x.xxxxx.....x

해설 및 후기

단순한 구현 문제이다. ASCII로 표현되는 덧셈식을 변환하여 값을 구한 후, 이를 다시 ASCII로 표현해야 한다. 구현에서 놓칠만한 부분도 보이진 않아서 어렵지 않게 풀 수 있었다.

제출 코드

import sys
l = []
n = 0

solver = [
    ['xxxxx','x...x','x...x','x...x','x...x','x...x','xxxxx'],
    ['....x','....x','....x','....x','....x','....x','....x'],
    ['xxxxx','....x','....x','xxxxx','x....','x....','xxxxx'],
    ['xxxxx','....x','....x','xxxxx','....x','....x','xxxxx'],
    ['x...x','x...x','x...x','xxxxx','....x','....x','....x'],
    ['xxxxx','x....','x....','xxxxx','....x','....x','xxxxx'],
    ['xxxxx','x....','x....','xxxxx','x...x','x...x','xxxxx'],
    ['xxxxx','....x','....x','....x','....x','....x','....x'],
    ['xxxxx','x...x','x...x','xxxxx','x...x','x...x','xxxxx'],
    ['xxxxx','x...x','x...x','xxxxx','....x','....x','xxxxx']]

def getNum(s):
    for i in range(10):
        if(s == solver[i]):
            return i
    else:
        return '+'

def calc(l, n):
    start = 0
    end = start+5
    calc = 0
    tmpCalc = 0
    while(start < n):
        tmpStr = [l[0][start:end],l[1][start:end],l[2][start:end],l[3][start:end],l[4][start:end],l[5][start:end],l[6][start:end]]
        start += 6
        end = start + 5
        a = getNum(tmpStr)
        if(a != '+'):
            calc*=10
            calc+=int(a)
        else:
            tmpCalc = calc
            calc = 0
    
    return tmpCalc+calc

def numToAscii(num):
    s = str(num)
    res = ['' for _ in range(7)]
    for i in range(7):
        for j in s:
            res[i] += solver[int(j)][i]
            res[i] += '.'
        res[i] = res[i][:-1]
    return res

for i in range(7):
    tmp = sys.stdin.readline().rstrip()
    l.append(tmp)
    n = len(tmp)

res = calc(l,n)
asc = numToAscii(res)

for i in range(7):
    print(asc[i])