This commit is contained in:
rhiannon morris 2022-12-06 06:35:24 +01:00
parent e8b5d3cfcf
commit 63831a6104
3 changed files with 50 additions and 2 deletions

View File

@ -35,7 +35,7 @@ Day4 ⇐ {
Day5 {
inp •FLines 𝕩
Start {(𝕨=0)¨/ 𝕩}
stks ' '/¨ <˘ > (1¨ 4Chunk)¨ '[' Start inp
stks ' '/¨ <˘> (1¨ 4Chunk)¨ '[' Start inp
todo •BQN1 (135 ' 'Split)¨ 'm' Start inp
_Do1 {
nst 𝕩 - 011
@ -47,4 +47,7 @@ Day5 ⇐ {
}
# ⟨ "QNNTGTPFN" "GGNPJBTTR" ⟩
# •Show Day5 ⊑•args
Day6 {{𝕨+ / ¨ <˘ 𝕨𝕩}(•FLines 𝕩)¨ 414}
# ⟨ 1929 3298 ⟩
# •Show Day6 ⊑•args

2
aoc.m
View File

@ -39,6 +39,8 @@ solution(3, day3.run).
solution(4, day4.run).
:- import_module day5.
solution(5, day5.run).
:- import_module day6.
solution(6, day6.run).
:- pred run_day(int, part, lines, univ).
:- mode run_day(in, in, in, out) is cc_multi.

43
day6.m Normal file
View 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").