This commit is contained in:
rhiannon morris 2022-12-01 23:59:19 +01:00
parent 65760d712e
commit 88072169a0
4 changed files with 25 additions and 33 deletions

29
aoc.m
View file

@ -9,32 +9,33 @@
:- import_module string.
:- import_module list.
:- import_module exception.
:- import_module univ.
main(!IO) :- wrap_main(main2, !IO).
:- pred main2(io::di, io::uo) is det.
:- pred main2(io::di, io::uo) is cc_multi.
main2(!IO) :-
command_line_arguments(Args, !IO),
(if Args = [DayS, PartS | Rest],
(if Args = [DayS, PartS, File],
to_int(DayS, Day),
part(PartS, Part) then
run(Day, Part, Rest, !IO)
else if Args = [D, P | _] then
die("expected a day and a part, got ""%s"" ""%s""", [s(D), s(P)])
else
die("expected at least two args")).
part(PartS, Part) then (
read_lines_need(File, Lines, !IO),
run_day(Day, Part, Lines, Out),
write_line(univ_value(Out), !IO)
) else
die("expected day, part, filename")).
:- type sol == (pred(part, list(string), io, io)).
:- inst sol == (pred(in, in, di, uo) is det).
:- type sol == (pred(part, list(string), univ)).
:- inst sol == (pred(in, in, out) is cc_multi).
:- pred solution(int::in, sol::out(sol)) is semidet.
:- import_module day1.
solution(1, day1.run).
:- pred run(int::in, part::in, list(string)::in, io::di, io::uo) is det.
run(Day, Part, Args, !IO) :-
:- pred run_day(int, part, list(string), univ).
:- mode run_day(in, in, in, out) is cc_multi.
run_day(Day, Part, Lines, Out) :-
if solution(Day, P) then
P(Part, Args, !IO)
P(Part, Lines, Out)
else
die("no solution for day %d", [i(Day)]).