Faulty Traffic Light
Time limit2sMemory limit2048 MB
Given which segments of a two-digit countdown light work, find the longest run of seconds where broken segments make the light look like it counts up by 1.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Statement
Near Vlad's home, there is a pedestrian traffic light that shows the remaining time to cross the road. The number on the light decreases by 1 every second. One day, Vlad approached the crossing and saw the light switch from 35 to 36. He could not believe his eyes.
Instead of 37 or any other number, the light showed something that was not a number at all. One of its indicators was broken and never lit up. So 39 looked like 35, and 38 looked like 36 (see Figures 1-3). Vlad then wondered how long this illusion of the light ticking up by 1 every second could last.
39 looks like 35
38 looks like 36
37 looks like nonsense
Statement
The light shows two digits. Each digit is drawn with seven stick-like indicators: four vertical and three horizontal (see Figure 4).

Each indicator is either functioning or broken. A broken indicator never lights up. The color switching always works correctly and always destroys the illusion of ticking up.
You are given exactly 99 seconds to cross the road. A functioning light shows 99, 98, 97, ..., 2, 1 in that order. It does not show 0. Instead, it switches to another color right away.
The light does not show leading zeros. On a functioning light, the number 6 is shown as a single six in the second digit, not as '06' (see Figures 5-6).
The number 6 on a functioning light
The number 37 on a functioning light
If such an illusion cannot be created at all, for example when the light always shows nonsense, the answer is 0.
Formally, find the largest non-negative integer k such that there exists a y with where the numbers y, y-1, ..., y-k look like the numbers x, x+1, ..., x+k because of the broken indicators. If no such k exists, the answer is 0.
Input
The first line contains a string of seven characters, each either 0 or 1. It describes the indicators of the first digit in the order shown in Figure 7. The first character is the top horizontal indicator, the second is the upper left vertical, the third is the upper right vertical, the fourth is the middle horizontal, the fifth is the lower left vertical, the sixth is the lower right vertical, and the seventh is the bottom horizontal. A 1 means the indicator is functioning, and a 0 means it is broken.
The second line describes the second digit in the same way.

Output
Print a single integer: the answer to the problem.
Hint
The first example is the situation discussed in the statement. The illusion lasts for 1 second, and it cannot last longer.
In the second example, the second and fifth (left vertical) indicators in the first digit are also broken. The answer 1 is still achieved in the same way: 39 looks like 35, and 38 looks like 36.
In the third example, the answer is 0. On this light, 90 looks like 51 and 89 looks like 61, and 61 is greater than 51. However, these two displays do not form an illusion of length 1, because 51 and 61 are not consecutive.
In the fourth example, all indicators are broken, so the light never displays a correctly written number. The answer is 0.