Back
Close

Bulls and Cows

Statement

 Goal

In the classic code-breaking game of Bulls and Cows, your opponent chooses a 4-digit secret number, and you need to guess it. After each guess, your opponent tells you how many "bulls" and how many "cows" are in your guess, interpreted as:

- Each bull indicates a digit in your guess that exactly matches the value and position of a digit in your opponent's secret number.

- Each cow indicates a digit in your guess that matches the value of a digit in your opponent's secret number, but is in the wrong position.

So for example, if the secret number is 1234 and you guess 5678, your guess has 0 bulls and 0 cows. However, if you guess 2324 then your guess has 1 bull (the 4) and 2 cows (one of the 2s, and the 3.)

You will be given a series of guesses along with the number of bulls and cows in each guess. Your job is to determine the secret number based on the given information.

NOTE: This version of the game deviates from the classic Bulls and Cows rules in that digits may be repeated any number of times in the secret number.
Input
Line 1: The number N of guesses.
Next N lines: A guess, consisting of 4 digits [0-9], followed by a space, then the number of bulls, another space, and then the number of cows.
Output
The 4-digit secret number.
Constraints
1 ≤ N ≤ 20
0≤ bulls+cows ≤ 4
Example
Input
1
1234 4 0
Output
1234

Tags

Difficulty

Test cases
All bulls Test
Input
1 1234 4 0
Output
1234

All bulls Validator
Input
1 5678 4 0
Output
5678

Pair of cows Test
Input
2 0473 2 2 7403 0 4
Output
0374

Pair of cows Validator
Input
2 8935 2 2 8395 0 4
Output
5938

Two pair of bulls Test
Input
3 9073 2 0 1248 2 0 1043 0 0
Output
9278

Two pair of bulls Validator
Input
3 5871 2 0 4320 2 0 5820 0 0
Output
4371

Nothing but cows Test
Input
1 7878 0 4
Output
8787

Nothing but cows Validator
Input
1 4455 0 4
Output
5544

Lone bulls Test
Input
5 0123 1 0 4567 1 0 8901 1 0 8522 3 0 8525 3 0
Output
8528

Lone bulls Validator
Input
5 0123 1 0 4567 1 0 8901 1 0 2927 3 0 7927 3 0
Output
9927

One for all Test
Input
4 0123 1 0 4567 0 0 8901 1 0 1110 3 0
Output
1111

All for naught Validator
Input
4 0123 1 0 4567 0 0 8901 1 0 1110 1 0
Output
0000

Bad guesser Test
Input
10 1111 0 0 2222 1 0 3333 0 0 4444 0 0 5555 0 0 6666 0 0 7777 2 0 8888 1 0 2778 0 4 7287 2 2
Output
7827

Bad guesser Validator
Input
10 1111 0 0 2222 1 0 3333 0 0 4444 0 0 5555 0 0 6666 0 0 7777 2 0 8888 1 0 7287 0 4 2778 2 2
Output
8772

Solution language

Solution

Stub generator input