package UE11_140125_Rekursion4;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

public class Sudoku {

    public static void main(String[] args) {
        String sudokuPath;
        sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/easy.sudoku";
        readSudoku(sudokuPath);
        sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/expert.sudoku";
        readSudoku(sudokuPath);
        sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/intermedia.sudoku";
        readSudoku(sudokuPath);
        sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/simple.sudoku";
        readSudoku(sudokuPath);
    }

    public static void readSudoku(String path) {
        try {
            List<String> input = Files.readAllLines(Path.of(path));
            for (String line : input) {
                int[][] sudoku = new int[9][9];
                String[] split = line.split(": ");
                System.out.println("\n" + split[0] + ":");
                for (int i = 0; i < split[1].length(); i++)
                    if (!(split[1].charAt(i) == '.'))
                        sudoku[i / 9][i % 9] = Integer.parseInt(String.valueOf(split[1].charAt(i)));
                solveSudoku(sudoku);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void solveSudoku(int[][] sudoku) {
        boolean[][] isBlockedRow = new boolean[9][10], isBlockedCol = new boolean[9][10], isBlockedBlock = new boolean[9][10];
        for (int row = 0; row < sudoku.length; row++)
            for (int col = 0; col < sudoku[row].length; col++) {
                int value = sudoku[col][row];
                if (value != 0)
                    isBlockedRow[row][value] = isBlockedCol[col][value] = isBlockedBlock[3 * (row / 3) + col / 3][value] = true;
            }
        solveSudoku(sudoku, 0, isBlockedRow, isBlockedCol, isBlockedBlock);
    }

    public static void solveSudoku(int[][] sudoku, int n, boolean[][] isBlockedRow, boolean[][] isBlockedCol, boolean[][] isBlockedBlock) {
        if (n >= sudoku.length * sudoku[0].length) {
            for (int[] i : sudoku) System.out.println(Arrays.toString(i));
            return;
        }
        int row = n / 9, col = n % 9, block = 3 * (row / 3) + col / 3;
        if (sudoku[col][row] == 0) {
            for (int num = 1; num <= 9; num++) {
                if (isBlockedRow[row][num] || isBlockedCol[col][num] || isBlockedBlock[block][num]) continue;
                isBlockedRow[row][num] = isBlockedCol[col][num] = isBlockedBlock[block][num] = true;
                sudoku[col][row] = num;
                solveSudoku(sudoku, n + 1, isBlockedRow, isBlockedCol, isBlockedBlock);
                sudoku[col][row] = 0;
                isBlockedRow[row][num] = isBlockedCol[col][num] = isBlockedBlock[block][num] = false;
            }
        } else solveSudoku(sudoku, n + 1, isBlockedRow, isBlockedCol, isBlockedBlock);
    }
}