formatting

This commit is contained in:
wi11-holdsworth 2025-08-28 15:57:18 +10:00
parent 77c82024d0
commit 42694f7e6e

45
main.pl
View file

@ -27,10 +27,11 @@
:- use_module(library(clpfd)). :- use_module(library(clpfd)).
%% puzzle_solution(+Puzzle) %% puzzle_solution(+Puzzle: list)
% %
% Holds when `Puzzle` is a solved math puzzle. % Holds when `Puzzle` is a solved math puzzle.
% See the top of this file for more information. % See the top of this file for more information.
puzzle_solution(Puzzle) :- puzzle_solution(Puzzle) :-
Puzzle = [_|Rows], Puzzle = [_|Rows],
unify_diagonal(Puzzle), unify_diagonal(Puzzle),
@ -40,17 +41,19 @@ puzzle_solution(Puzzle) :-
maplist(valid_row, Columns). maplist(valid_row, Columns).
%% unify_diagonal(+Puzzle) %% unify_diagonal(+Puzzle: list)
% %
% Holds when every variable in the main diagonal of `Puzzle` is the same. % Holds when every variable in the main diagonal of `Puzzle` is the same.
unify_diagonal(Puzzle) :- unify_diagonal(Puzzle) :-
main_diagonal(Puzzle, [_|Diag]), main_diagonal(Puzzle, [_|Diag]),
all_same(Diag). all_same(Diag).
%% main_diagonal(+Matrix, -Diag) %% main_diagonal(+Matrix: list, -Diag: list)
% %
% Holds when the list `Diag` is the main diagonal of the 2d list `Matrix`. % Holds when `Diag` is the main diagonal of the 2d list `Matrix`.
main_diagonal(Matrix, Diag) :- main_diagonal(Matrix, Diag) :-
main_diagonal(Matrix, 0, Diag). main_diagonal(Matrix, 0, Diag).
@ -61,50 +64,52 @@ main_diagonal([Row|Rows], Column, [D|Ds]) :-
main_diagonal(Rows, Next_column, Ds). main_diagonal(Rows, Next_column, Ds).
%% all_same(+Vars) %% all_same(+Vars: list)
% %
% Holds when the variables in the list `Vars` can be unified % Holds when the variables in `Vars` can be unified.
all_same([Var|Vars]) :-
all_same(Var, Vars).
all_same(Var, [Var]). all_same([]).
all_same(Var, [Var|Vars]) :- all_same([_]).
all_same(Var, Vars). all_same([Head,Head|Tail]) :-
all_same([Head|Tail]).
%% valid(+Row) %% valid_row(+Row: list)
% %
% Holds when `Row` is valid. % Holds when `Row` is valid.
% A row is valid when: % A row is valid when:
% 1. All elements except the head are integers from 1 to 9 (inclusive) % 1. All elements except the head are integers from 1 to 9 (inclusive)
% 2. All elements except the head are distinct % 2. All elements except the head are distinct
% 3. The head of the row is either the sum or the product of the tail of `Row` % 3. The head of the row is either the sum or the product of the tail of `Row`
valid_row([Total|Vars]) :- valid_row([Total|Vars]) :-
Vars ins 1..9, Vars ins 1..9,
all_distinct(Vars), all_distinct(Vars),
valid_total(Total, Vars), valid_total(Vars, Total),
labeling([ff], Vars). labeling([ff], Vars).
%% valid_total(+Total, +Vars) %% valid_total(+Vars: list, +Total: integer)
% %
% Holds when the integer `Total` is either the sum or the product of the list % Holds when `Total` is either the sum or the product of `Vars`.
% `Vars`.
valid_total(Total, Vars) :- valid_total(Vars, Total) :-
sum(Vars, #=, Total) sum(Vars, #=, Total)
; product(Vars, Total). ; product(Vars, Total).
%% product(+Vars, -Product) %% product(+Vars: list, -Product: integer)
% %
% Holds when the integer `Product` is the product of the list `Vars`. % Holds when `Product` is the product of `Vars`.
product(Vars, Product) :- product(Vars, Product) :-
foldl(times, Vars, 1, Product). foldl(times, Vars, 1, Product).
%% times(?Int1, ?Int2, ?Int3) %% times(?Int1: integer, ?Int2: integer, ?Int3: integer)
% %
% Holds when Int3 #= Int1 * Int2. % Holds when Int3 #= Int1 * Int2.
times(Int1, Int2, Int3) :- times(Int1, Int2, Int3) :-
Int3 #= Int1 * Int2. Int3 #= Int1 * Int2.