Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
:- use_module(library(clpfd)). % Main solver for a Sudoku puzzle solver(Rows) :- initialize_grid(Rows), apply_constraints(Rows), maplist(labeling([ff]), Rows). % Initializes a 9x9 grid with domain constraints initialize_grid(Rows) :- length(Rows, 9), maplist(same_length(Rows), Rows), append(Rows, Cells), Cells ins 1..9. % Applies row, column, and block constraints apply_constraints(Rows) :- maplist(all_distinct, Rows), % Ensure rows are unique transpose(Rows, Columns), maplist(all_distinct, Columns), % Ensure columns are unique enforce_blocks(Rows). % Ensure 3x3 blocks are unique % Ensures all 3x3 blocks are distinct enforce_blocks([A, B, C, D, E, F, G, H, I]) :- blocks(A, B, C), blocks(D, E, F), blocks(G, H, I). blocks([], [], []). blocks([A, B, C | Rest1], [D, E, F | Rest2], [G, H, I | Rest3]) :- all_distinct([A, B, C, D, E, F, G, H, I]), blocks(Rest1, Rest2, Rest3). % Difficulty analysis difficulty(Rows, Start, Weak, Constraint, Search) :- initialize_grid(Rows), count_filled_cells(Rows, Start), weak_propagation(Rows, Weak), constraint_propagation(Rows, Constraint), search_propagation(Rows, Search), print_analysis(Start, Weak, Constraint, Search). % Calculates the number of pre-filled cells count_filled_cells(Rows, Count) :- append(Rows, Cells), count_numbers(Cells, Count). count_numbers([], 0). count_numbers([H | T], Count) :- count_numbers(T, Rest), (number(H) -> Count is Rest + 1 ; Count = Rest). % Weak propagation: Applies minimal constraints weak_propagation(Rows, Filled) :- initialize_grid(Rows), apply_constraints(Rows), count_filled_cells(Rows, Filled). % Constraint propagation: Uses domain reduction to propagate constraints constraint_propagation(Rows, Filled) :- solver(Rows), % Solver automatically applies all constraints count_filled_cells(Rows, Filled). % Search propagation: Solves with search and calculates filled cells search_propagation(Rows, Filled) :- solver(Rows), % Reuse the solver for search-based filling count_filled_cells(Rows, Filled). % Prints the percentage analysis print_analysis(Start, Weak, Constraint, Search) :- write('\nPercent Solved from Start:\n'), percent_solved(Start, Weak, WeakPercent), format('Weak Propagation: ~2f%%\n', [WeakPercent]), percent_solved(Start, Constraint, ConstraintPercent), format('Constraint Propagation: ~2f%%\n', [ConstraintPercent]), percent_solved(Start, Search, SearchPercent), format('Search: ~2f%%\n', [SearchPercent]). percent_solved(Start, End, Percent) :- Percent is ((End - Start) / (81 - Start)) * 100. % Sample puzzles puzzle(1, [[_,4,_,9,_,_,_,5,_], [2,_,_,_,_,_,_,4,_], [1,9,_,_,8,_,7,_,_], [5,_,_,_,_,_,1,_,_], [_,_,7,_,6,_,_,_,3], [_,_,_,_,3,_,8,9,_], [_,8,_,3,4,_,_,6,_], [3,_,_,2,_,8,_,_,_], [_,_,_,_,_,_,_,_,_]]). puzzle(2, [[_,_,9,5,_,_,_,3,7], [1,3,7,9,_,_,_,5,2], [2,_,_,_,_,3,6,9,_], [3,5,2,_,1,_,_,_,6], [_,_,_,4,5,2,3,_,_], [_,8,1,_,3,_,2,_,_], [6,_,3,_,4,_,8,_,9], [5,2,_,_,_,1,_,6,_], [_,_,_,3,_,7,_,_,_]]). puzzle(3, [[_,5,_,1,_,_,_,_,_], [2,_,_,5,_,_,6,_,_], [1,_,_,_,8,_,2,_,_], [_,8,_,4,3,_,_,_,_], [_,_,_,_,_,_,_,4,_], [_,_,_,_,_,7,9,3,2], [_,4,_,6,7,_,_,_,_], [_,7,_,_,_,_,_,1,9], [9,_,_,_,_,8,_,_,_]]).