aoc2022/day11.m

151 lines
4.6 KiB
Mathematica
Raw Normal View History

2022-12-11 03:00:55 -05:00
:- module day11.
:- interface.
:- import_module basics.
:- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation.
:- import_module int.
:- import_module string.
:- import_module list.
2022-12-13 02:27:18 -05:00
:- import_module array.
2022-12-11 03:00:55 -05:00
:- type item == int.
:- type op ---> add(int); mul(int); square.
:- type test == int.
:- type monkey --->
m(items :: list(item),
op :: op, test :: test,
if_true :: int, if_false :: int,
inspected :: int).
:- func inspected(monkey) = int.
:- func test(monkey) = int.
2022-12-13 02:27:18 -05:00
:- type monkeys == array(monkey).
:- mode m_in == array_di.
:- mode m_out == array_uo.
2022-12-11 03:00:55 -05:00
:- type divmod ---> dm(d :: int, m :: int).
:- func op(op, item) = item.
op(add(M), N) = M + N.
op(mul(M), N) = M * N.
op(square, N) = N * N.
:- func op(op, divmod, item) = item.
2022-12-13 02:27:18 -05:00
op(O, dm(D, M), N) = (op(O, N) div D) mod M.
2022-12-11 03:00:55 -05:00
:- pred test(test::in, item::in) is semidet.
test(M, N) :- N mod M = 0.
:- import_module parsing_utils.
eof(Src) --> not next_char(Src, _).
:- pred kw(src::in, string::in, ps::in, ps::out) is semidet.
kw(Src, Kw) --> ikeyword("abcdefghijklmnopqrstuvwxyz", Kw, Src, _).
:- pred kws(src::in, list(string)::in, ps::in, ps::out) is semidet.
kws(_, []) --> [].
kws(Src, [Kw | Kws]) --> kw(Src, Kw), kws(Src, Kws).
colon(Src) --> punct(":", Src, _).
comma(Src) --> punct(",", Src, _).
equal(Src) --> punct("=", Src, _).
plus(Src) --> punct("+", Src, _).
star(Src) --> punct("*", Src, _).
2022-12-13 02:27:18 -05:00
:- pred monkeys(src::in, list(monkey)::out, ps::in, ps::out) is semidet.
2022-12-11 03:00:55 -05:00
monkeys(Src, Ms) --> one_or_more(monkey, Src, Ms), eof(Src).
:- pred monkey(src::in, monkey::out, ps::in, ps::out) is semidet.
monkey(Src, m(reverse(I), O, P, T, F, 0)) -->
kw(Src, "monkey"), int_literal_as_string(Src, _), colon(Src),
kws(Src, ["starting", "items"]), colon(Src), items(Src, I),
kw(Src, "operation"), colon(Src),
kw(Src, "new"), equal(Src), op(Src, O),
kw(Src, "test"), colon(Src), test(Src, P),
kws(Src, ["if", "true"]), colon(Src), act(Src, T),
kws(Src, ["if", "false"]), colon(Src), act(Src, F).
:- pred items(src::in, list(item)::out, ps::in, ps::out) is semidet.
items(Src, Is) --> comma_separated_list(int_literal, Src, Is).
:- pred op(src::in, op::out, ps::in, ps::out) is semidet.
op(Src, Op) -->
kw(Src, "old"),
(if star(Src) then
(if int_literal(Src, N) then {Op = mul(N)}
else kw(Src, "old"), {Op = square})
else plus(Src), int_literal(Src, N), {Op = add(N)}).
:- pred test(src::in, test::out, ps::in, ps::out) is semidet.
test(Src, N) --> kws(Src, ["divisible", "by"]), int_literal(Src, N).
:- pred act(src::in, int::out, ps::in, ps::out) is semidet.
act(Src, N) --> kws(Src, ["throw", "to", "monkey"]), int_literal(Src, N).
2022-12-13 02:27:18 -05:00
:- pred give(item::in, int::in, monkeys::m_in, monkeys::m_out) is det.
give(I, N, !Ms) :-
lookup(!.Ms, N, M0),
M = M0^items := [I | M0^items],
set(N, M, !Ms).
2022-12-11 03:00:55 -05:00
2022-12-13 02:27:18 -05:00
:- pred get(int::in, monkey::out, monkeys::m_in, monkeys::m_out) is det.
get(N, M0, !Ms) :-
lookup(!.Ms, N, M0),
Len = length(M0^items), Insp = M0^inspected,
M = (M0^inspected := Insp + Len)^items := [],
set(N, M, !Ms).
2022-12-11 03:00:55 -05:00
2022-12-13 02:27:18 -05:00
:- pred step(divmod::in, monkey::in, item::in,
monkeys::m_in, monkeys::m_out) is det.
2022-12-11 03:00:55 -05:00
step(DM, M, I, !Ms) :-
I2 = op(M^op, DM, I),
(if test(M^test, I2) then
give(I2, M^if_true, !Ms)
else
give(I2, M^if_false, !Ms)).
2022-12-13 02:27:18 -05:00
:- pred steps(divmod::in, monkey::in, list(item)::in,
monkeys::m_in, monkeys::m_out) is det.
steps(_, _, [], !Ms).
steps(DM, M, [I | Is], !Ms) :- steps(DM, M, Is, !Ms), step(DM, M, I, !Ms).
:- pred turn(divmod::in, int::in, monkeys::m_in, monkeys::m_out) is det.
turn(DM, N, !Ms) :- get(N, M, !Ms), steps(DM, M, M^items, !Ms).
2022-12-11 03:00:55 -05:00
2022-12-13 02:27:18 -05:00
:- pred round(divmod::in, int::in, monkeys::m_in, monkeys::m_out) is det.
round(DM, N, !Ms) :-
if N < size(!.Ms) then turn(DM, N, !Ms), round(DM, N + 1, !Ms) else true.
2022-12-11 03:00:55 -05:00
2022-12-13 02:27:18 -05:00
:- pred round(divmod::in, monkeys::m_in, monkeys::m_out) is det.
2022-12-11 03:00:55 -05:00
round(DM, !Ms) :- round(DM, 0, !Ms).
2022-12-13 02:27:18 -05:00
:- pred rounds(divmod::in, int::in, monkeys::m_in, monkeys::m_out) is det.
2022-12-11 03:00:55 -05:00
rounds(DM, N, !Ms) :-
2022-12-13 02:27:18 -05:00
if N > 0 then round(DM, !Ms), rounds(DM, N - 1, !Ms) else true.
2022-12-11 03:00:55 -05:00
2022-12-13 02:27:18 -05:00
:- pred rounds(part::in, monkeys::m_in, monkeys::m_out) is det.
2022-12-11 03:00:55 -05:00
rounds(Part, !Ms) :-
2022-12-13 02:27:18 -05:00
DM = dm(part(Part, 3, 1), foldl(func(X, A) = test(X) * A, !.Ms, 1)),
2022-12-11 03:00:55 -05:00
rounds(DM, part(Part, 20, 10_000), !Ms).
2022-12-13 02:27:18 -05:00
% i broke part 1 at some point and i can't see where
2022-12-11 03:00:55 -05:00
run(Part, Lines, Out) :-
2022-12-13 02:27:18 -05:00
parse(join_list("\n", Lines), monkeys, Res),
(if Res = ok(MList) then
from_list(MList, Arr0),
rounds(Part, Arr0, Arr1),
to_list(Arr1, Final),
2022-12-11 03:00:55 -05:00
Out = int(prod(take_upto(2, rev_sort(map(inspected, Final)))))
else
Out = 'new other'(Res)).