25 lines
737 B
Mathematica
25 lines
737 B
Mathematica
:- module day1.
|
|
:- interface.
|
|
:- import_module basics.
|
|
|
|
:- pred run(part::in, lines::in, answer::out) is cc_multi.
|
|
|
|
:- implementation.
|
|
:- import_module list.
|
|
:- import_module maybe.
|
|
|
|
:- pragma no_determinism_warning(run/3).
|
|
run(one, Lines, int(det_head(go(Lines)))).
|
|
run(two, Lines, int(sum(take_upto(3, go(Lines))))).
|
|
|
|
:- func go(lines) = list(int).
|
|
go(Lines) =
|
|
rev_sort(map(sum, gather(map(to_int_may, Lines)))).
|
|
|
|
:- func gather(list(maybe(T))) = list(list(T)).
|
|
gather(Xs) = Ys :- gather(Xs, Ys).
|
|
|
|
:- pred gather(list(maybe(T))::in, list(list(T))::out(non_empty_list)) is det.
|
|
gather([], [[]]).
|
|
gather([no | Xs], [[] | Yss]) :- gather(Xs, Yss).
|
|
gather([yes(Y) | Xs], [[Y | Ys] | Yss]) :- gather(Xs, [Ys | Yss]).
|