2022-12-01 04:46:26 -05:00
|
|
|
:- module day1.
|
|
|
|
:- interface.
|
|
|
|
:- import_module basics.
|
|
|
|
|
2022-12-10 01:05:22 -05:00
|
|
|
:- pred run(part::in, lines::in, answer::out) is cc_multi.
|
2022-12-01 04:46:26 -05:00
|
|
|
|
|
|
|
:- implementation.
|
2022-12-05 04:14:29 -05:00
|
|
|
:- import_module list.
|
2022-12-01 13:23:53 -05:00
|
|
|
:- import_module maybe.
|
2022-12-01 04:46:26 -05:00
|
|
|
|
2022-12-01 17:59:19 -05:00
|
|
|
:- pragma no_determinism_warning(run/3).
|
2022-12-10 01:05:22 -05:00
|
|
|
run(one, Lines, int(det_head(go(Lines)))).
|
|
|
|
run(two, Lines, int(sum(take_upto(3, go(Lines))))).
|
2022-12-01 04:46:26 -05:00
|
|
|
|
2022-12-05 04:14:29 -05:00
|
|
|
:- func go(lines) = list(int).
|
2022-12-01 13:23:53 -05:00
|
|
|
go(Lines) =
|
2022-12-11 03:00:55 -05:00
|
|
|
rev_sort(map(sum, gather(map(to_int_may, Lines)))).
|
2022-12-01 04:46:26 -05:00
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- func gather(list(maybe(T))) = list(list(T)).
|
|
|
|
gather(Xs) = Ys :- gather(Xs, Ys).
|
2022-12-01 04:46:26 -05:00
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- 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]).
|