<commit_before>package magicsquares;

import solver.Solver;
import solver.constraints.IntConstraintFactory;
import solver.exception.ContradictionException;
import solver.search.strategy.decision.Decision;
import solver.search.strategy.strategy.AbstractStrategy;
import solver.variables.IntVar;
import solver.variables.VariableFactory;
import util.tools.ArrayUtils;

public class MagicSquares {
    public static interface SolverFactory {
        public abstract Solver build();
    }

    public static class BasicSolverFactory implements SolverFactory {
        public Solver build() {
            return new Solver();
        }
    }

    public static IntVar[][] solveMagicSquare(int n, SolverFactory solverFactory) {
        Solver solver = solverFactory.build();
        IntVar sum = VariableFactory.fixed((n * (n * n + 1)) / 2, solver);
        IntVar[][] vs = VariableFactory.enumeratedMatrix("vs", n, n, 1, n * n, solver);

        // Différence
        solver.post(IntConstraintFactory.alldifferent(ArrayUtils.flatten(vs)));

        IntVar[] diagonal1 = new IntVar[n];
        IntVar[] diagonal2 = new IntVar[n];

        // Lignes et colonnes
        for(int i = 0; i < n; i++) {
            solver.post(IntConstraintFactory.sum(vs[i], sum));
            solver.post(IntConstraintFactory.sum(ArrayUtils.getColumn(vs, i), sum));
            diagonal1[i] = vs[i][i];
            diagonal2[i] = vs[i][n - i - 1];
        }

        // Diagonales
        solver.post(IntConstraintFactory.sum(diagonal1, sum));
        solver.post(IntConstraintFactory.sum(diagonal2, sum));

        solver.findSolution();

        return vs;
    }

    public static void main(String[] args) {
        final int n = 6;
        IntVar[][] solution = solveMagicSquare(n, new BasicSolverFactory());
        displaySolution(solution);
    }

    // Affichage d'une solution trouvee
    public static void displaySolution(IntVar[][] solution) {
        for(int i = 0; i < solution.length; i++) {
            for(int j = 0; j < solution[i].length; j++) {
                System.out.print(solution[i][j].getValue());
                System.out.print(' ');
            }
            System.out.println();
        }
    }
}
<commit_msg>SolverFactory -> StrategyFactory<commit_after>package magicsquares;

import solver.Solver;
import solver.constraints.IntConstraintFactory;
import solver.exception.ContradictionException;
import solver.search.strategy.ISF;
import solver.search.strategy.decision.Decision;
import solver.search.strategy.selectors.VariableSelectorWithTies;
import solver.search.strategy.selectors.values.IntDomainMax;
import solver.search.strategy.selectors.values.IntDomainMin;
import solver.search.strategy.selectors.variables.FirstFail;
import solver.search.strategy.selectors.variables.Random;
import solver.search.strategy.selectors.variables.Smallest;
import solver.search.strategy.strategy.AbstractStrategy;
import solver.variables.IntVar;
import solver.variables.VariableFactory;
import util.tools.ArrayUtils;

public class MagicSquares {
    public static interface StrategyFactory {
        public abstract AbstractStrategy build(IntVar... vars);
    }

    public static class DefaultStrategyFactory implements StrategyFactory {
        public AbstractStrategy build(IntVar... vars) {
            return null;
        }
    }

    public static class BasicStrategyFactory implements StrategyFactory {
        public AbstractStrategy build(IntVar... vars) {
            return ISF.custom(new VariableSelectorWithTies<IntVar>(new Smallest()), new IntDomainMax(), vars);
        }
    }

    public static IntVar[][] solveMagicSquare(int n, StrategyFactory strategyFactory) {
        Solver solver = new Solver();
        IntVar sum = VariableFactory.fixed((n * (n * n + 1)) / 2, solver);
        IntVar[][] vs = VariableFactory.enumeratedMatrix("vs", n, n, 1, n * n, solver);

        AbstractStrategy strategy = strategyFactory.build(ArrayUtils.flatten(vs));
        if (strategy != null) {
            solver.set(strategy);
        }

        // Différence
        solver.post(IntConstraintFactory.alldifferent(ArrayUtils.flatten(vs)));

        IntVar[] diagonal1 = new IntVar[n];
        IntVar[] diagonal2 = new IntVar[n];

        // Lignes et colonnes
        for(int i = 0; i < n; i++) {
            solver.post(IntConstraintFactory.sum(vs[i], sum));
            solver.post(IntConstraintFactory.sum(ArrayUtils.getColumn(vs, i), sum));
            diagonal1[i] = vs[i][i];
            diagonal2[i] = vs[i][n - i - 1];
        }

        // Diagonales
        solver.post(IntConstraintFactory.sum(diagonal1, sum));
        solver.post(IntConstraintFactory.sum(diagonal2, sum));

        solver.findSolution();

        return vs;
    }

    public static void main(String[] args) {
        final int n = 6;
        IntVar[][] solution = solveMagicSquare(n, new DefaultStrategyFactory());
        displaySolution(solution);
    }

    // Affichage d'une solution trouvee
    public static void displaySolution(IntVar[][] solution) {
        for(int i = 0; i < solution.length; i++) {
            for(int j = 0; j < solution[i].length; j++) {
                System.out.print(solution[i][j].getValue());
                System.out.print(' ');
            }
            System.out.println();
        }
    }
}

{'fraction_non_alphanumeric': 0.11494883040935673, 'fraction_numerical': 0.005116959064327485, 'mean_word_length': 2.526417525773196, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345199', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1515, 'n_words': 331}