Make d15p2 a little more readable

This commit is contained in:
Dory 2023-12-15 00:39:55 -08:00
parent 8683c9c8b3
commit a3b78482b3

View File

@ -6,15 +6,15 @@
main([FileName|_]) :- main([FileName|_]) :-
input(FileName, Actions), input(FileName, Actions),
perform_all(Actions, Boxes), perform_all(Actions, Boxes),
findall( findall(Power, power_of_some_lens(Boxes, Power), Powers),
Power,
( nth1(BoxI, Boxes, _-Box),
nth1(LensI, Box, _-Focal),
Power is BoxI * LensI * Focal),
Powers),
sum_list(Powers, Answer), sum_list(Powers, Answer),
writef('Answer=%t\n', [Answer]). writef('Answer=%t\n', [Answer]).
power_of_some_lens(Boxes, Power) :-
nth1(BoxI, Boxes, _-Box),
nth1(LensI, Box, _-Focal),
Power is BoxI * LensI * Focal.
perform_all(Actions, FinalBoxes) :- perform_all(Actions, FinalBoxes) :-
length(EmptyBoxes, 256), length(EmptyBoxes, 256),
foldl([N-[], N, NextN]>>(NextN is N + 1), EmptyBoxes, 0, _), foldl([N-[], N, NextN]>>(NextN is N + 1), EmptyBoxes, 0, _),
@ -29,8 +29,8 @@ remove(_, [], []).
remove(Label, [Label-_|Box], Box) :- !. remove(Label, [Label-_|Box], Box) :- !.
remove(Label, [Lens|Box], [Lens|NewBox]) :- remove(Label, Box, NewBox). remove(Label, [Lens|Box], [Lens|NewBox]) :- remove(Label, Box, NewBox).
add(F, Label, OldBox, NewBox) :- select(Label-_, OldBox, Label-F, NewBox), !. add(Focal, Label, Box, NewBox) :- select(Label-_, Box, Label-Focal, NewBox), !.
add(F, Label, OldBox, NewBox) :- append(OldBox, [Label-F], NewBox). add(Focal, Label, Box, NewBox) :- append(Box, [Label-Focal], NewBox).
% Input stuff. [0-(add-1)-"rn", 3-remove-"cm", ...] % Input stuff. [0-(add-1)-"rn", 3-remove-"cm", ...]
input(Name, Actions) :- phrase_from_file(insts(Actions), Name). input(Name, Actions) :- phrase_from_file(insts(Actions), Name).