day11
This commit is contained in:
parent
d7b45f85d2
commit
506f945d80
5 changed files with 161 additions and 11 deletions
141
day11.m
Normal file
141
day11.m
Normal file
|
@ -0,0 +1,141 @@
|
|||
:- 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.
|
||||
|
||||
:- 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.
|
||||
:- type monkeys == list(monkey).
|
||||
|
||||
:- 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.
|
||||
op(O, dm(D, M), N) = op(O, N) `div` D `mod` M.
|
||||
|
||||
:- 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, _).
|
||||
|
||||
|
||||
:- pred monkeys(src::in, monkeys::out, ps::in, ps::out) is semidet.
|
||||
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).
|
||||
|
||||
|
||||
:- pred give(item::in, int::in, monkeys::in, monkeys::out) is det.
|
||||
give(_, _, [], _) :- die("list too short").
|
||||
give(I, N, [!.M | !.Ms], [!:M | !:Ms]) :-
|
||||
if N = 0 then !M^items := [I | !.M^items]
|
||||
else give(I, N - 1, !Ms).
|
||||
|
||||
:- pred get(int::in, monkey::out, monkeys::in, monkeys::out) is semidet.
|
||||
get(N, X, [!.M | !.Ms], [!:M | !:Ms]) :-
|
||||
if N = 0 then
|
||||
X = !.M,
|
||||
!M^inspected := !.M^inspected + length(!.M^items),
|
||||
!M^items := []
|
||||
else
|
||||
get(N - 1, X, !Ms).
|
||||
|
||||
:- pred step(divmod::in, monkey::in, int::in, monkeys::in, monkeys::out) is det.
|
||||
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)).
|
||||
|
||||
:- pred turn(divmod::in, int::in, monkeys::in, monkeys::out) is semidet.
|
||||
turn(DM, I, !Ms) :-
|
||||
get(I, M, !Ms),
|
||||
foldr(step(DM, M), M^items, !Ms).
|
||||
|
||||
:- pred round(divmod::in, int::in, monkeys::in, monkeys::out) is det.
|
||||
round(DM, I, !Ms) :-
|
||||
if turn(DM, I, !Ms) then round(DM, I + 1, !Ms) else true.
|
||||
|
||||
:- pred round(divmod::in, monkeys::in, monkeys::out) is det.
|
||||
round(DM, !Ms) :- round(DM, 0, !Ms).
|
||||
|
||||
:- pred rounds(divmod::in, int::in, monkeys::in, monkeys::out) is det.
|
||||
rounds(DM, N, !Ms) :-
|
||||
if N = 0 then true else
|
||||
round(DM, !Ms),
|
||||
rounds(DM, N - 1, !Ms).
|
||||
|
||||
:- pred rounds(part::in, monkeys::in, monkeys::out) is det.
|
||||
rounds(Part, !Ms) :-
|
||||
DM = dm(part(Part, 3, 1), prod(map(test, !.Ms))),
|
||||
rounds(DM, part(Part, 20, 10_000), !Ms).
|
||||
|
||||
run(Part, Lines, Out) :-
|
||||
parse(join_list("\n", Lines), one_or_more(monkey), Res),
|
||||
(if Res = ok(Monkeys) then
|
||||
rounds(Part, Monkeys, Final),
|
||||
Out = int(prod(take_upto(2, rev_sort(map(inspected, Final)))))
|
||||
else
|
||||
Out = 'new other'(Res)).
|
Loading…
Add table
Add a link
Reference in a new issue