This commit is contained in:
rhiannon morris 2022-12-01 10:46:26 +01:00
parent ea6fb40895
commit 835eeefc0a
3 changed files with 49 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ Mercury/
*.mh
*.err
aoc
input/

3
aoc.m
View file

@ -31,7 +31,8 @@ main2(!IO) :-
:- inst solution == (pred(in, in, di, uo) is det).
:- pred solution(int::in, solution::out(solution)) is semidet.
solution(_, _) :- fail.
:- import_module day1.
solution(1, day1.run).
:- pred run(int::in, part::in, list(string)::in, io::di, io::uo) is det.

46
day1.m Normal file
View file

@ -0,0 +1,46 @@
:- 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 die.
:- import_module std_util.
:- import_module string.
:- import_module int.
run(Part, Args, !IO) :-
if Args = [File] then (
read_named_file_as_lines(File, LinesM, !IO),
need(LinesM, Lines),
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(int_sum, gather(Lines))).
:- func gather(list(string)::in) = (list(list(string))::out(non_empty_list)).
gather([]) = [[]].
gather([X | Xs]) = Groups :-
[Ys | Yss] = gather(Xs),
(if X = "" then Groups = [[], Ys | Yss]
else Groups = [[X | Ys] | Yss]).
:- func sum(list(int)) = int.
sum(Xs) = foldl(func(X, Y) = X + Y, Xs, 0).
:- func int_sum(list(string)) = int.
int_sum(Xs) = sum(map(det_to_int, Xs)).
:- pred need(io.res(T)::in, T::out) is det.
need(ok(X), X).
need(error(E), _) :- die(error_message(E)).