simplify all_diff

This commit is contained in:
rhiannon morris 2022-12-06 06:49:48 +01:00
parent 414d419212
commit 136e49c2a0

14
day6.m
View file

@ -12,22 +12,16 @@
:- import_module list. :- import_module list.
:- import_module maybe. :- import_module maybe.
:- pred all_diff(list(T)::in) is semidet.
:- func uniq(list(T)) = list(T). all_diff([]).
uniq([]) = []. all_diff([X | Xs]) :- not member(X, Xs), all_diff(Xs).
uniq([X | Xs]) = Ys :-
Ys0 = uniq(Xs),
(if member(X, Ys0) then Ys = Ys0 else Ys = [X | Ys0]).
:- pred alldiff(list(T)::in) is semidet.
alldiff(Xs) :- Xs = uniq(Xs).
:- pred window(int::in, list(T)::in, int::out, list(T)::out) is nondet. :- pred window(int::in, list(T)::in, int::out, list(T)::out) is nondet.
window(Len, In, 0, Out) :- take(Len, In, Out). window(Len, In, 0, Out) :- take(Len, In, Out).
window(Len, [_|In], I + 1, Out) :- window(Len, In, I, Out). window(Len, [_|In], I + 1, Out) :- window(Len, In, I, Out).
:- pred start_index(int::in, list(T)::in, int::out) is cc_nondet. :- pred start_index(int::in, list(T)::in, int::out) is cc_nondet.
start_index(Len, Input, Index) :- window(Len, Input, Index, Out), alldiff(Out). start_index(Len, Input, Index) :- window(Len, Input, Index, Out), all_diff(Out).
:- func length(part) = int. :- func length(part) = int.
length(one) = 4. length(one) = 4.