Under construction

Still being built. Things may change

MasterMind

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 100 guesses with exact and wrong-position counts, find the smallest four-digit secret consistent with all of them, or NONE.
Level

Medium4 of 10

Topics
Brute force, Implementation, Simulation, Hash map
Solved
No attempts yet

Problem

MasterMind is a classic two-player game.

One player is the codemaker: she picks a four-digit secret number SS with 1000≤S≤99991000 \le S \le 9999. The other player is the codebreaker, who repeatedly guesses four-digit numbers until she cracks the code.

For every codebreaker guess GiG_i (with 1000≤Gi≤99991000 \le G_i \le 9999), the codemaker replies with two integers:

  • CiC_i (0≤Ci≤40 \le C_i \le 4): how many digits of the guess are correct and in their correct position in the secret number.
  • WiW_i (0≤Wi≤4−Ci0 \le W_i \le 4 - C_i): how many of the remaining digits (those not counted by CiC_i) are correct but in the wrong position.

For example, if the secret number is 23512351 and the codebreaker guesses 13501350, the feedback is 2 1: the digits 33 and 55 are in their correct positions, and 11 is present but in the wrong position. As another example, in a five-digit variant, if the secret is 1122311223 and the guess is 1232212322, the feedback is 2 2.

You are given NN (1≤N≤1001 \le N \le 100) guesses together with their feedback from the middle of a game. Output the smallest four-digit number (in the range 1000≤S≤99991000 \le S \le 9999) that could still be the codemaker's secret, i.e. that is consistent with every guess and its feedback. If no such number exists, output NONE.

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains guess ii and its two responses as three space-separated integers GiG_i, CiC_i, and WiW_i.

Output

  • A single line with the smallest four-digit number (in the same range as the secret, 1000≤S≤99991000 \le S \le 9999) that could be the secret code. If there is no such number, output a single line containing the word NONE.

Examples1

  1. Example 1

    Input
    4
    3157 1 2
    1350 2 1
    6120 0 2
    2381 3 0
    
    Expected output
    2351