2022-12-07 03:22:18 -05:00
|
|
|
:- module day7.
|
|
|
|
:- interface.
|
|
|
|
:- import_module basics.
|
|
|
|
:- import_module univ.
|
|
|
|
|
|
|
|
:- pred run(part::in, lines::in, univ::out) is cc_multi.
|
|
|
|
|
|
|
|
:- implementation.
|
|
|
|
:- import_module int.
|
|
|
|
:- import_module string.
|
|
|
|
:- import_module list.
|
|
|
|
|
2022-12-07 04:03:27 -05:00
|
|
|
:- type dir ---> dir(size :: int, dirs :: dirs).
|
|
|
|
:- func size(dir) = int.
|
2022-12-07 03:22:18 -05:00
|
|
|
:- type dirs == list(dir).
|
|
|
|
|
|
|
|
:- pred dir(dir::out, lines::in, lines::out) is nondet.
|
2022-12-07 04:03:27 -05:00
|
|
|
dir(dir(Size, Dirs)) -->
|
|
|
|
[Cd], {append("$ cd ", _, Cd)},
|
|
|
|
["$ ls"],
|
|
|
|
files(Files),
|
2022-12-07 03:22:18 -05:00
|
|
|
dirs(Dirs),
|
2022-12-07 04:03:27 -05:00
|
|
|
{Size = Files + sum(map(size, Dirs))},
|
2022-12-07 03:22:18 -05:00
|
|
|
(["$ cd .."] ; []).
|
|
|
|
|
2022-12-07 04:03:27 -05:00
|
|
|
:- pred files(int::out, lines::in, lines::out) is multi.
|
|
|
|
files(Size + Rest) -->
|
|
|
|
[Line], {to_int(head(words(Line)), Size)},
|
|
|
|
files(Rest).
|
|
|
|
files(Size) -->
|
|
|
|
[Dir], {append("dir ", _, Dir)},
|
|
|
|
files(Size).
|
|
|
|
files(0) --> [].
|
2022-12-07 03:22:18 -05:00
|
|
|
|
|
|
|
:- pred dirs(dirs::out, lines::in, lines::out) is multi.
|
|
|
|
dirs([D | Ds]) --> dir(D), dirs(Ds).
|
|
|
|
dirs([]) --> [].
|
|
|
|
|
|
|
|
:- pred find(pred(dir)::(pred(in) is semidet), dir::in, list(dir)::out) is det.
|
|
|
|
find(P, Dir, Out) :-
|
2022-12-07 04:03:27 -05:00
|
|
|
map(find(P), dirs(Dir), Outs), Children = condense(Outs),
|
2022-12-07 03:22:18 -05:00
|
|
|
(if P(Dir) then Out = [Dir | Children] else Out = Children).
|
|
|
|
|
|
|
|
:- pred size_lte(int::in, dir::in) is semidet.
|
2022-12-07 04:03:27 -05:00
|
|
|
size_lte(Size, Dir) :- size(Dir) =< Size.
|
2022-12-07 03:22:18 -05:00
|
|
|
|
|
|
|
:- pred size_gte(int::in, dir::in) is semidet.
|
2022-12-07 04:03:27 -05:00
|
|
|
size_gte(Size, Dir) :- size(Dir) >= Size.
|
2022-12-07 03:22:18 -05:00
|
|
|
|
2022-12-07 04:03:27 -05:00
|
|
|
:- pred go(part::in, dir::in, int::out) is det.
|
|
|
|
go(one, Top, sum(map(size, Found))) :-
|
|
|
|
find(size_lte(100_000), Top, Found).
|
|
|
|
go(two, Top, det_head(sort(map(size, Found)))) :-
|
|
|
|
find(size_gte(size(Top) - 40_000_000), Top, Found).
|
2022-12-07 03:22:18 -05:00
|
|
|
|
2022-12-07 04:03:27 -05:00
|
|
|
run(Part, Lines, univ(Out)) :-
|
|
|
|
if dir(Top, Lines, []) then go(Part, Top, Out) else die("bad input").
|