#P7034. Overlapping Columnar Addition
Overlapping Columnar Addition
Overlapping Columnar Addition
Deidra performs a columnar addition by writing two non‐negative integer summands one under the other. She left‐pads them with zeroes so that they have equal length and computes the sum. If, because of a carry, the sum has more digits than each summand, she prepends a zero to each summand so that all three numbers have the same length. She even allows unnecessary leading zeroes as long as the lengths of all three numbers are equal.
Deidra then prints the addition using a standard 3×5 digit font. However, her homemade printing press mis‐aligns the digits: digits that were supposed to be horizontally adjacent are printed so that the right two columns of the left digit coincide with the left two columns of the right digit; digits that were supposed to be vertically adjacent (that is, the numbers appearing one below the other) are printed so that the bottom 2 rows of the upper number overlap with the top 2 rows of the lower number. In the overlapping process a cell becomes black if at least one printed segment is black.
Given the resulting picture, find any valid addition (i.e. two summands and their sum) that could have produced it or determine that no valid addition exists. In case there is a valid addition, output the three numbers (each as an L‐digit string, possibly with leading zeroes) on three separate lines. Otherwise, print NO
.
Note on the Digit Font: Each digit is printed on a 3×5 grid. The patterns for the digits 0–9 are given below (with '#' representing black and '.' white):
0:
###
#.#
#.#
#.#
###
1:
..#
..#
..#
..#
..#
2:
###
..#
###
#..
###
3:
###
..#
###
..#
###
4:
#.#
#.#
###
..#
..#
5:
###
#..
###
..#
###
6:
###
#..
###
#.#
###
7:
###
..#
..#
..#
..#
8:
###
#.#
###
#.#
###
9:
###
#.#
###
..#
###
The printing process first combines the digits horizontally. For a number with L digits the horizontal printed image has 5 rows and (L+2) columns (since the first digit contributes 3 columns and every subsequent digit adds 1 column because its left 2 columns overlap with the previous digit’s right 2 columns). Then, the three numbers (the first summand, the second summand, and the result) are stacked vertically so that the top number is printed at row 0, the second number is printed with its top row at row 3 (overlapping the bottom 2 rows of the first number) and the result is printed with its top row at row 6 (overlapping the bottom 2 rows of the second number). This produces a final picture of 11 rows and (L+2) columns.
inputFormat
The input consists of 11 lines. Each line contains exactly W characters where W = L+2 (with L being the number of digits in each of the three numbers). Each character is either '#' (black) or '.' (white).
outputFormat
If there exists a valid addition that could have produced the picture, output three lines: the first summand, the second summand, and the result (each exactly L digits long, possibly with leading zeroes). Otherwise, output a single line with the word NO
.
sample
###
..#
###
###
###
###
###
###
###
..#
###
2
3
5
</p>