day6
This commit is contained in:
parent
e8b5d3cfcf
commit
63831a6104
3 changed files with 50 additions and 2 deletions
7
aoc.bqn
7
aoc.bqn
|
@ -35,7 +35,7 @@ Day4 ⇐ {
|
|||
Day5 ⇐ {
|
||||
inp ← •FLines 𝕩
|
||||
Start ← {(𝕨⊸=⊑⎊0)¨⊸/ 𝕩}
|
||||
stks ← ' '⊸≠⊸/¨ <˘ ⍉> (1⊸⊑¨ 4⊸Chunk)¨ '[' Start inp
|
||||
stks ← ' '⊸≠⊸/¨ <˘⍉> (1⊸⊑¨ 4⊸Chunk)¨ '[' Start inp
|
||||
todo ← •BQN⚇1 (1‿3‿5⊸⊏ ' '⊸Split)¨ 'm' Start inp
|
||||
_Do1 ← {
|
||||
n‿s‿t ← 𝕩 - 0‿1‿1
|
||||
|
@ -47,4 +47,7 @@ Day5 ⇐ {
|
|||
}
|
||||
# ⟨ "QNNTGTPFN" "GGNPJBTTR" ⟩
|
||||
|
||||
# •Show Day5 ⊑•args
|
||||
Day6 ⇐ {{𝕨+ ⊑/ ≡⟜⍷¨ <˘ 𝕨↕𝕩}⟜(⊑•FLines 𝕩)¨ 4‿14}
|
||||
# ⟨ 1929 3298 ⟩
|
||||
|
||||
# •Show Day6 ⊑•args
|
||||
|
|
2
aoc.m
2
aoc.m
|
@ -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
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
Reference in a new issue