import java.util.Scanner;

public class Digitle {
    public static void main(String[] args) {

        int rand1 = (int) (Math.random() * 10);
        int rand2 = (int) (Math.random() * 10);
        int rand3 = (int) (Math.random() * 10);
        int rand4 = (int) (Math.random() * 10);
        int rand5 = (int) (Math.random() * 10);

        String digit1 = Integer.toString(rand1);
        String digit2 = Integer.toString(rand2);
        String digit3 = Integer.toString(rand3);
        String digit4 = Integer.toString(rand4);
        String digit5 = Integer.toString(rand5);

        String hidden = (digit1 + digit2 + digit3 + digit4 + digit5);
        // System.out.println("The secret code is: " + hidden);

        int attempts = 6;

        Scanner scanner = new Scanner(System.in);

        for (int t = 6; t > 0; t--) {

            attempts = t;
            System.out.println("Guess the code: (" + attempts + ")");

            String guess = scanner.nextLine();

            // Check if guess is 5 digits
            if (guess.length() != 5 || !guess.matches("\\d{5}")) {
                System.out.println("Invalid! Your guess is not 5 digits");
                t++; // give the user another chance
                continue;
            }

            // Check if guess is correct
            if (guess.equals(hidden)) {
                attempts = 6 - t + 1;
                System.out.println("You guessed the code in " + attempts + " tries! The code was " + hidden);
                break; // exit loop if correct
            }

            // Provide hints
            String attempt = "";
            for (int j = 0; j < 5; j++) {
                char current = guess.charAt(j);

                if (current == hidden.charAt(j)) {
                    attempt += "(" + current + ")";
                } else if (current - hidden.charAt(j) < 0) {
                    attempt += "[" + current + "]";
                } else if (current - hidden.charAt(j) > 0) {
                    attempt += "{" + current + "}";
                }
            }
            System.out.println(attempt);
        }

        // Only show failure if code wasn't guessed
        if (!scanner.equals(hidden)) {
            System.out.println("You failed to guess the code in 6 tries. The code was: " + hidden);
        }

        scanner.close();
    }
}