Seven Segment Counter

Time limit1sMemory limit128 MB

Summary
Given timestamped photos with partly hidden seven-segment bars, find how many starting counter values from 0 to 999 fit every photo.
Level

Medium4 of 10

Topics
Brute force, Simulation, Implementation
Solved
No attempts yet

Problem

A seven segment display shows one digit from 0 to 9 with seven bars. Price boards at exchange offices and fuel stations use them.

Shelby wired three of these displays to a circuit and built a three digit display that shows any number from 0 to 999 with no leading zero. He went further and made it run as a time counter: the number goes up by one every second, and after 999 it goes back to 0.

Shelby is a technician at a company, and one of these three digit counters hangs on his office wall. Every day at 15:00:00, the time he leaves work, Shelby has to write the number on the counter down on a piece of paper. The big boss asked for it, for security reasons.

Yesterday Shelby forgot to write the number down. He did find photos that several surveillance cameras took at 15:00:00 or later. The trouble is that in each photo some of the 21 bars are not clear enough to tell whether they are on or off.

Each photo comes with the state of all 21 bars (visible and on, visible and off, or not visible in that photo) and the time it was taken. Work out the number on the counter at 15:00:00.

Input

The input holds several test cases. One test case holds at least one and at most 20 photos.

A photo takes six lines. The first line holds a single non negative integer, at most 61200, that says how many seconds passed since 15:00:00. The next five lines draw the photo.

When all 21 bars are visible and on, so the display reads 888, the five lines look like this.

 -  -  -
| || || |
 -  -  -
| || || |
 -  -  -

A bar that is visible but off gets a period . instead of - or |. A bar that is not visible in this photo gets an asterisk *.

In a clear photo the digits 0 to 9 look like this. The off column on the right is a position whose seven bars are all off, so it shows no digit.

 0    1    2    3    4    5    6    7    8    9   off
 -    .    -    -    .    -    -    -    -    -    .
| |  . |  . |  . |  | |  | .  | .  . |  | |  | |  . .
 .    .    -    -    -    -    -    .    -    -    .
| |  . |  | .  . |  . |  . |  | |  . |  | |  . |  . .
 -    .    -    -    .    -    -    .    -    -    .

The counter never shows a leading zero. The value 70, for example, puts 7 in the second position from the left and 0 in the third, and every bar of the first position is off. It is not shown as 070.

A line holding a single # separates two test cases. The last line of the input holds a single $.

No line carries extra spaces on the left, but a line may carry extra spaces on the right. Only the first nine columns of a line matter.

Output

For every test case, if the counter value at 15:00:00 can be worked out, meaning exactly one value fits every photo, print that value.

Otherwise print a question mark, then one space, then the number of different values the counter may have shown at 15:00:00. When the photos disagree with each other and no value fits, print ? 0.

Examples1

  1. Example 1

    Input
    1
     -  -  -
    | || || |
     -  -  -
    | || || |
     -  -  -
    #
    0
     *  *  -
    * ** *| |
     *  *  -
    * ** *. |
     *  *  -
    1
     .  .  *
    . |. |* *
     .  .  *
    . |. |* *
     .  .  *
    #
    6120
     -  -  -
    | || || *
     -  *  -
    * || || |
     -  -  -
    $
    
    Expected output
    887
    109
    ? 8