University of Calgary

japanese abacus

Japanese Abacus
source: 1998 ACM programming contest

A Japanese abacus and its components are shown in the figure below:
- The model shown here can be used to represent non-negative numbers having up to 9 digits in base 10.
- There are five beads on each of the 9 vertical rods, one in the upper deck and four in the lower deck.
- The beads can be moved up or down on the rods. Each rod will always have one vacant position in the upper deck and one in the lower deck.
- The beads in the rightmost position represents the units digit; the beads on the next rod to the left are the 10's digit; and so on.
- Each bead in the upper deck can count as 0 or 5.
- Each bead in the lower deck can count as 1 or 0.
- To make a bead count it must be moved toward the centerpiece. In the configuration shown above, all of the beads are away from the centerpiece, so the value displayed is zero.
- In the configuration below, in the units (1's) place, for example, the bead in the upper deck (=5) and two beads in the lower deck (each having value 1) have been moved towards the centerpiece. Therefor the units digit is 5 + 2 = 7. The value represented by this configuration is 196350847.

An abacus configuration will be represented according to the following rules:
- 10 lines are needed for the whole configuration.
- Each of the 10 lines contains 11 characters, not counting end of line characters.
- Equal signs are used to represent the frame and centerpiece.
- Each bead is represented by an uppercase letter `O'.
- Each rod position not occupied is represented by the vertical bar character `|'.
- Each of the first, fourth, and tenth lines contain 11 equal signs.
- Each of the remaining lines has an equal sign in column1 and 11; each of the characters in columns 2..10 can be either a')' or a `|' depending on the configuration.

SAMPLE DATA: abacus1.txt, abacus2.txt, abacus3.txt, abacus4.txt
set 1 = 196250847, set 2 = 000000000, set 3 = 012345678, set 4 = 220719841

With the configuration shown in the above example, PROG3.DAT would be as follows:
===========
=O||O|O|O|=
=|OO|O|O|O=
===========
=OOOO||OOO=
=|O|OOOOOO=
=OOOOOOOO|=
=OOO|OO|OO=
=O|OOOOO|O=
===========

You may assume that the input file PROG3.DAT contains a valid abacus configuration. The output from your program, written to PROG3.OUT, will consist of the non-negative integer represented by the configuration. The configuration shown above will yield the following output:
Problem 3
Value of the abacus configuration = 196350847
End of problem 3

Hereis a line of output showing the output format:
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
Value of the abacus configuration = 196350847

- The units digit of the integer will be in column 45.
- If the result is zero, it will be represented by a single digit 0 in column 45.
- If the result is non-zero and has fewer than 9 digits, there will not be any leading zeros displayed.



Updated: January 29, 2006 10:56 AM