From 6eb6f4bd15ee85683a4e7057cbe0c8e3b0c49cbe Mon Sep 17 00:00:00 2001 From: rhiannon morris Date: Sat, 3 Dec 2022 11:24:35 +0100 Subject: [PATCH] no more char lists in day3 --- day3.m | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/day3.m b/day3.m index afaf431..c8eea59 100644 --- a/day3.m +++ b/day3.m @@ -11,12 +11,17 @@ :- import_module char. :- import_module string. -:- pred split(chars::in, chars::out, chars::out) is semidet. +:- pred split(string::in, string::out, string::out) is semidet. split(X, L, H) :- Len = length(X), even(Len), - split_list(Len / 2, X, L, H). + split(X, Len / 2, L, H). -:- pred common(list(chars)::in, char::out) is nondet. +:- pred member(char::out, string::in) is nondet. +member(C, S) :- + nondet_int_in_range(0, length(S) - 1, I), + index(S, I, C). + +:- pred common(list(string)::in, char::out) is nondet. common([S | Ss], C) :- member(C, S), (Ss = [] ; common(Ss, C)). :- func prio(char) = int. @@ -36,7 +41,7 @@ threes([X, Y, Z | Rest], [t(X, Y, Z) | Groups]) :- threes(Rest, Groups). :- pred go1(string::in, int::out) is cc_multi. go1(Line, Out) :- - if split(to_char_list(Line), Fst, Snd), + if split(Line, Fst, Snd), common([Fst, Snd], C) then Out = prio(C) @@ -46,11 +51,10 @@ go1(Line, Out) :- :- pred go2(triple(string)::in, int::out) is cc_multi. go2(t(X, Y, Z), Prio) :- - if common(map(to_char_list, [X, Y, Z]), C) then + if common([X, Y, Z], C) then Prio = prio(C) else - die("no item in common in:\n - %s\n - %s\n - %s", - [s(X), s(Y), s(Z)]). + die("no item in common in:\n - %s\n - %s\n - %s", [s(X), s(Y), s(Z)]). run(one, Lines, univ(sum(Prios))) :-