40 lines
1.1 KiB
Mathematica
40 lines
1.1 KiB
Mathematica
:- module day1.
|
|
:- interface.
|
|
:- import_module basics.
|
|
:- import_module io.
|
|
:- import_module list.
|
|
|
|
:- pred run(part::in, list(string)::in, io::di, io::uo) is det.
|
|
|
|
:- implementation.
|
|
:- import_module std_util.
|
|
:- import_module maybe.
|
|
:- import_module string.
|
|
:- import_module int.
|
|
|
|
run(Part, Args, !IO) :-
|
|
if Args = [File] then (
|
|
read_lines_need(File, Lines, !IO),
|
|
Totals = go(Lines),
|
|
( Part = one,
|
|
format("%d\n", [i(det_head(Totals))], !IO)
|
|
; Part = two,
|
|
Res = sum(take_upto(3, Totals)),
|
|
format("%d\n", [i(Res)], !IO)
|
|
)
|
|
) else die("expected one readable file").
|
|
|
|
:- func go(list(string)) = list(int).
|
|
go(Lines) =
|
|
sort(converse(ordering), 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]).
|
|
|
|
:- func sum(list(int)) = int.
|
|
sum(Xs) = foldl(plus, Xs, 0).
|