2022-12-01 02:56:19 -05:00
|
|
|
:- module basics.
|
|
|
|
:- interface.
|
2022-12-10 01:05:22 -05:00
|
|
|
:- import_module io.
|
2022-12-01 02:56:19 -05:00
|
|
|
|
2022-12-01 17:59:19 -05:00
|
|
|
:- type part ---> one; two.
|
2022-12-01 02:56:19 -05:00
|
|
|
:- pred part(string, part).
|
|
|
|
:- mode part(in, out) is semidet.
|
2022-12-01 13:23:53 -05:00
|
|
|
:- mode part(out, in) is det.
|
|
|
|
|
2022-12-09 06:18:58 -05:00
|
|
|
:- func part(part, T, T) = T.
|
|
|
|
|
2022-12-11 03:00:55 -05:00
|
|
|
:- type answer --->
|
|
|
|
int(int); string(string); lines(list(string));
|
|
|
|
some [T] other(T).
|
2022-12-10 01:05:22 -05:00
|
|
|
:- pred output(answer::in, io::di, io::uo) is det.
|
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
|
2022-12-16 10:29:37 -05:00
|
|
|
:- func ite((pred)::((pred) is semidet), T::in, T::in) = (T::out) is det.
|
2022-12-10 03:42:22 -05:00
|
|
|
|
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- import_module list.
|
|
|
|
:- import_module string.
|
|
|
|
|
2022-12-05 04:14:29 -05:00
|
|
|
:- type lines == list(string).
|
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- pred die(string::in) is erroneous.
|
|
|
|
:- pred die(string::in, list(poly_type)::in) is erroneous.
|
|
|
|
|
|
|
|
:- pred need(io.res(T)::in, T::out) is det.
|
2022-12-05 04:14:29 -05:00
|
|
|
:- pred read_lines_need(string::in, lines::out, io::di, io::uo) is det.
|
2022-12-01 13:23:53 -05:00
|
|
|
|
|
|
|
|
2022-12-01 17:59:19 -05:00
|
|
|
:- pred wrap_main(pred(io, io), io, io).
|
|
|
|
:- mode wrap_main(pred(di, uo) is cc_multi, di, uo) is cc_multi.
|
|
|
|
:- mode wrap_main(pred(di, uo) is det, di, uo) is cc_multi.
|
2022-12-01 13:23:53 -05:00
|
|
|
|
|
|
|
|
2022-12-03 00:33:45 -05:00
|
|
|
:- import_module char.
|
|
|
|
|
|
|
|
:- type chars == list(char).
|
|
|
|
|
|
|
|
|
|
|
|
:- func sum(list(int)) = int.
|
2022-12-11 03:00:55 -05:00
|
|
|
:- func prod(list(int)) = int.
|
2022-12-09 06:18:58 -05:00
|
|
|
:- func replicate(int, T) = list(T).
|
2022-12-11 03:00:55 -05:00
|
|
|
:- func rev_sort(list(T)) = list(T).
|
2022-12-03 00:33:45 -05:00
|
|
|
|
2022-12-16 10:29:37 -05:00
|
|
|
|
2022-12-10 03:42:22 -05:00
|
|
|
:- func map_index(func(int, T) = U, list(T)) = list(U).
|
|
|
|
|
2022-12-16 10:29:37 -05:00
|
|
|
:- pred map_index(pred(int, T, U), list(T), list(U)).
|
|
|
|
:- mode map_index(pred(in, in, out) is det, in, out) is det.
|
|
|
|
:- mode map_index(pred(in, di, uo) is det, di, uo) is det.
|
|
|
|
:- mode map_index(pred(in, in, out) is semidet, in, out) is semidet.
|
|
|
|
:- mode map_index(pred(in, in, out) is multi, in, out) is multi.
|
|
|
|
:- mode map_index(pred(in, in, out) is cc_multi, in, out) is cc_multi.
|
|
|
|
|
|
|
|
|
|
|
|
:- func foldl_index(func(int, T, U) = U, list(T), U) = U.
|
|
|
|
|
|
|
|
:- pred foldl_index(pred(int, T, U, U), list(T), U, U).
|
|
|
|
:- mode foldl_index(pred(in, in, in, out) is det, in, in, out) is det.
|
|
|
|
:- mode foldl_index(pred(in, in, di, uo) is det, in, di, uo) is det.
|
|
|
|
:- mode foldl_index(pred(in, di, di, uo) is det, di, di, uo) is det.
|
|
|
|
:- mode foldl_index(pred(in, in, in, out) is semidet, in, in, out) is semidet.
|
|
|
|
:- mode foldl_index(pred(in, in, in, out) is multi, in, in, out) is multi.
|
|
|
|
:- mode foldl_index(pred(in, in, in, out) is cc_multi, in, in, out) is cc_multi.
|
|
|
|
|
2022-12-03 00:33:45 -05:00
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- import_module maybe.
|
|
|
|
|
|
|
|
:- func to_int_may(string) = maybe(int).
|
|
|
|
|
2022-12-01 02:56:19 -05:00
|
|
|
|
|
|
|
:- implementation.
|
|
|
|
|
|
|
|
part("1", one).
|
|
|
|
part("2", two).
|
2022-12-01 13:23:53 -05:00
|
|
|
|
2022-12-16 10:29:37 -05:00
|
|
|
part(P, X, Y) = ite(unify(P, one), X, Y).
|
2022-12-09 06:18:58 -05:00
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
|
2022-12-10 01:05:22 -05:00
|
|
|
output(int(I), !IO) :- write_int(I, !IO), nl(!IO).
|
|
|
|
output(string(S), !IO) :- write_string(S, !IO), nl(!IO).
|
|
|
|
output(lines(Ss), !IO) :- write_list(Ss, "\n", write_string, !IO), nl(!IO).
|
2022-12-11 03:00:55 -05:00
|
|
|
output(other(X), !IO) :- write(X, !IO), nl(!IO).
|
2022-12-10 01:05:22 -05:00
|
|
|
|
2022-12-10 03:42:22 -05:00
|
|
|
|
|
|
|
ite(P, T, E) = X :- if P then X = T else X = E.
|
|
|
|
|
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
:- import_module exception.
|
|
|
|
|
|
|
|
:- type die ---> death(string).
|
|
|
|
|
|
|
|
die(Str) :- throw(death(Str)).
|
|
|
|
die(Fmt, Args) :- die(format(Fmt, Args)).
|
|
|
|
|
|
|
|
wrap_main(Main, !IO) :-
|
|
|
|
try [io(!IO)] Main(!IO)
|
|
|
|
then true
|
2022-12-16 10:29:37 -05:00
|
|
|
catch death(Err) ->
|
2022-12-01 13:23:53 -05:00
|
|
|
format("%s\n", [s(Err)], !IO),
|
2022-12-16 10:29:37 -05:00
|
|
|
set_exit_status(1, !IO).
|
2022-12-01 13:23:53 -05:00
|
|
|
|
|
|
|
need(ok(X), X).
|
|
|
|
need(error(E), _) :- die(error_message(E)).
|
|
|
|
|
|
|
|
read_lines_need(File, Lines, !IO) :-
|
|
|
|
read_named_file_as_lines(File, Res, !IO),
|
|
|
|
need(Res, Lines).
|
|
|
|
|
|
|
|
|
2022-12-11 03:00:55 -05:00
|
|
|
:- import_module int.
|
|
|
|
|
2022-12-03 00:33:45 -05:00
|
|
|
sum(Xs) = foldl(plus, Xs, 0).
|
2022-12-11 03:00:55 -05:00
|
|
|
prod(Xs) = foldl(times, Xs, 1).
|
2022-12-03 00:33:45 -05:00
|
|
|
|
2022-12-09 06:18:58 -05:00
|
|
|
replicate(N, X) = Out :-
|
|
|
|
if N = 0 then Out = [] else Out = [X | replicate(N - 1, X)].
|
2022-12-03 00:33:45 -05:00
|
|
|
|
2022-12-11 03:00:55 -05:00
|
|
|
:- import_module std_util.
|
|
|
|
|
|
|
|
rev_sort(Xs) = sort(converse(ordering), Xs).
|
|
|
|
|
2022-12-10 03:42:22 -05:00
|
|
|
|
2022-12-16 10:29:37 -05:00
|
|
|
:- pred map_index(int, pred(int, T, U), list(T), list(U)).
|
|
|
|
:- mode map_index(in, pred(in, in, out) is det, in, out) is det.
|
|
|
|
:- mode map_index(in, pred(in, di, uo) is det, di, uo) is det.
|
|
|
|
:- mode map_index(in, pred(in, in, out) is semidet, in, out) is semidet.
|
|
|
|
:- mode map_index(in, pred(in, in, out) is cc_multi, in, out) is cc_multi.
|
|
|
|
:- mode map_index(in, pred(in, in, out) is multi, in, out) is multi.
|
|
|
|
map_index(_, _, [], []).
|
|
|
|
map_index(I, P, [X | Xs], [Y | Ys]) :- P(I, X, Y), map_index(I, P, Xs, Ys).
|
|
|
|
|
|
|
|
map_index(P, Xs, Ys) :- map_index(0, P, Xs, Ys).
|
|
|
|
map_index(F, Xs) = Ys :-
|
|
|
|
P = (pred(I::in, X::in, Y::out) is det :- Y = F(I, X)),
|
|
|
|
map_index(P, Xs, Ys).
|
|
|
|
|
|
|
|
|
|
|
|
:- pred foldl_index(int, pred(int, T, U, U), list(T), U, U).
|
|
|
|
:- mode foldl_index(in, pred(in, in, in, out) is det, in, in, out) is det.
|
|
|
|
:- mode foldl_index(in, pred(in, in, di, uo) is det, in, di, uo) is det.
|
|
|
|
:- mode foldl_index(in, pred(in, di, di, uo) is det, di, di, uo) is det.
|
|
|
|
:- mode foldl_index(in, pred(in, in, in, out) is semidet, in, in, out) is semidet.
|
|
|
|
:- mode foldl_index(in, pred(in, in, in, out) is multi, in, in, out) is multi.
|
|
|
|
:- mode foldl_index(in, pred(in, in, in, out) is cc_multi, in, in, out) is cc_multi.
|
|
|
|
foldl_index(_, _, [], !Acc).
|
|
|
|
foldl_index(I, P, [X | Xs], !Acc) :-
|
|
|
|
P(I, X, !Acc),
|
|
|
|
foldl_index(I + 1, P, Xs, !Acc).
|
|
|
|
|
|
|
|
foldl_index(P, Xs, !Acc) :- foldl_index(0, P, Xs, !Acc).
|
|
|
|
foldl_index(F, Xs, Start) = End :-
|
|
|
|
P = (pred(I::in, X::in, A::in, B::out) is det :- B = F(I, X, A)),
|
|
|
|
foldl_index(P, Xs, Start, End).
|
2022-12-10 03:42:22 -05:00
|
|
|
|
2022-12-01 13:23:53 -05:00
|
|
|
to_int_may(Str) = Out :-
|
|
|
|
if to_int(Str, N) then Out = yes(N) else Out = no.
|