day6
This commit is contained in:
parent
e8b5d3cfcf
commit
63831a6104
3 changed files with 50 additions and 2 deletions
43
day6.m
Normal file
43
day6.m
Normal file
|
@ -0,0 +1,43 @@
|
|||
:- module day6.
|
||||
:- interface.
|
||||
:- import_module basics.
|
||||
:- import_module univ.
|
||||
|
||||
:- pred run(part::in, lines::in, univ::out) is cc_multi.
|
||||
|
||||
:- implementation.
|
||||
:- import_module int.
|
||||
:- import_module char.
|
||||
:- import_module string.
|
||||
:- import_module list.
|
||||
:- import_module maybe.
|
||||
|
||||
|
||||
:- func uniq(list(T)) = list(T).
|
||||
uniq([]) = [].
|
||||
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.
|
||||
window(Len, In, 0, Out) :- take(Len, In, 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.
|
||||
start_index(Len, Input, Index) :- window(Len, Input, Index, Out), alldiff(Out).
|
||||
|
||||
:- func length(part) = int.
|
||||
length(one) = 4.
|
||||
length(two) = 14.
|
||||
|
||||
:- pragma no_determinism_warning(run/3).
|
||||
run(Part, Lines, univ(Out)) :-
|
||||
if Lines = [Line] then
|
||||
Len = length(Part),
|
||||
(if start_index(Len, to_char_list(Line), Out0)
|
||||
then Out = Len + Out0
|
||||
else die("not found"))
|
||||
else die("bad input").
|
Loading…
Add table
Add a link
Reference in a new issue