This commit is contained in:
rhiannon morris 2022-12-03 06:33:45 +01:00
parent 601ade63d1
commit d5b2222586
6 changed files with 78 additions and 11 deletions

View file

@ -7,8 +7,9 @@ Split ⇐ {(⊏ ∾ 1⊸↓¨∘(1⊸↓)) Sub⟜𝕩¨ (» ⋈¨ ⊢-») /𝕨=
Day1 ( (+´3))(+´¨)Split(•BQN0¨)•FLines Day1 ( (+´3))(+´¨)Split(•BQN0¨)•FLines
Day2 { Day2 {
Line -"AX" ' 'Split
B (A {1+𝕩+3×3|1+𝕩-𝕨}) {3|𝕨+𝕩-1} B (A {1+𝕩+3×3|1+𝕩-𝕨}) {3|𝕨+𝕩-1}
+´˘ (AB)´Line˘ >•FLines 𝕩 +´˘ (AB)´{asb:a-'A',b-'X'}˘ >•FLines 𝕩
} }
# •Show Day2 ⊑•args •Show Day2 •args
# day 3 maybe later

2
aoc.m
View file

@ -33,6 +33,8 @@ main2(!IO) :-
solution(1, day1.run). solution(1, day1.run).
:- import_module day2. :- import_module day2.
solution(2, day2.run). solution(2, day2.run).
:- import_module day3.
solution(3, day3.run).
:- pred run_day(int, part, list(string), univ). :- pred run_day(int, part, list(string), univ).
:- mode run_day(in, in, in, out) is cc_multi. :- mode run_day(in, in, in, out) is cc_multi.

View file

@ -23,6 +23,16 @@
:- mode wrap_main(pred(di, uo) is det, di, uo) is cc_multi. :- mode wrap_main(pred(di, uo) is det, di, uo) is cc_multi.
:- import_module char.
:- type chars == list(char).
:- import_module int.
:- func sum(list(int)) = int.
:- import_module maybe. :- import_module maybe.
:- func to_int_may(string) = maybe(int). :- func to_int_may(string) = maybe(int).
@ -57,5 +67,8 @@ read_lines_need(File, Lines, !IO) :-
need(Res, Lines). need(Res, Lines).
sum(Xs) = foldl(plus, Xs, 0).
to_int_may(Str) = Out :- to_int_may(Str) = Out :-
if to_int(Str, N) then Out = yes(N) else Out = no. if to_int(Str, N) then Out = yes(N) else Out = no.

3
day1.m
View file

@ -27,6 +27,3 @@ gather(Xs) = Ys :- gather(Xs, Ys).
gather([], [[]]). gather([], [[]]).
gather([no | Xs], [[] | Yss]) :- gather(Xs, Yss). gather([no | Xs], [[] | Yss]) :- gather(Xs, Yss).
gather([yes(Y) | Xs], [[Y | Ys] | Yss]) :- gather(Xs, [Ys | Yss]). gather([yes(Y) | Xs], [[Y | Ys] | Yss]) :- gather(Xs, [Ys | Yss]).
:- func sum(list(int)) = int.
sum(Xs) = foldl(plus, Xs, 0).

5
day2.m
View file

@ -22,8 +22,6 @@
:- type result ---> me; you; draw. :- type result ---> me; you; draw.
:- type move_res == pair(move, result). :- type move_res == pair(move, result).
:- type chars == list(char).
:- type parser(T) == (pred(T, chars, chars)). :- type parser(T) == (pred(T, chars, chars)).
:- mode parser(I) == (pred(out(I), in, out) is semidet). :- mode parser(I) == (pred(out(I), in, out) is semidet).
:- mode parser == parser(ground). :- mode parser == parser(ground).
@ -89,9 +87,6 @@ score1(You, Me) = to_int(Me) + win_score(Res) :- win(You, Me, Res).
:- func score2(move, result) = int. :- func score2(move, result) = int.
score2(You, Res) = to_int(Me) + win_score(Res) :- win(You, Me, Res). score2(You, Res) = to_int(Me) + win_score(Res) :- win(You, Me, Res).
:- func sum(list(int)) = int.
sum(Xs) = foldl(plus, Xs, 0).
:- pred run(list(string)::in, parser(pair(T, U))::parser, :- pred run(list(string)::in, parser(pair(T, U))::parser,
(func(T, U) = int)::in, int::out) is det. (func(T, U) = int)::in, int::out) is det.

59
day3.m Normal file
View file

@ -0,0 +1,59 @@
:- module day3.
:- 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 int.
:- import_module char.
:- import_module string.
:- import_module exception.
:- pred split(chars::in, chars::out, chars::out) is semidet.
split(X, L, H) :-
Len = length(X), even(Len),
split_list(Len / 2, X, L, H).
:- pred common(list(chars)::in, char::out) is nondet.
common([S | Ss], C) :- member(C, S), (Ss = [] ; common(Ss, C)).
:- func score(char) = int.
score(C) = I :-
if is_lower(C) then
I = to_int(C) - to_int('a') + 1
else
I = to_int(C) - to_int('A') + 27.
:- pred threes(list(T)::in, list(list(T))::out) is semidet.
threes([], []).
threes([X, Y, Z | Rest], [[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),
common([Fst, Snd], C)
then
Out = score(C)
else
die("invalid line: ""%s""", [s(Line)]).
:- pred go2(list(string)::in, int::out) is cc_multi.
go2(Lines, Score) :-
if common(map(to_char_list, Lines), C) then
Score = score(C)
else
die("no item in common").
run(one, Lines, univ(sum(Scores))) :-
map(go1, Lines, Scores).
run(two, Lines, univ(sum(Scores))) :-
if threes(Lines, Groups) then
map(go2, Groups, Scores)
else
die("not a multiple of three lines").