aoc2022/day1.m
2022-12-01 23:59:19 +01:00

32 lines
925 B
Mathematica

:- module day1.
:- interface.
:- import_module basics.
:- import_module list.
:- import_module univ.
:- pred run(part::in, list(string)::in, univ::out) is cc_multi.
:- implementation.
:- import_module std_util.
:- import_module maybe.
:- import_module string.
:- import_module int.
:- pragma no_determinism_warning(run/3).
run(one, Lines, univ(det_head(go(Lines)))).
run(two, Lines, univ(sum(take_upto(3, go(Lines))))).
:- 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).