2023-12-14 04:01:31 -06:00
|
|
|
:- use_module(library(pio)).
|
|
|
|
:- use_module(library(clpfd)).
|
|
|
|
:- initialization(main, main).
|
2023-12-14 15:50:21 -06:00
|
|
|
:- table rocks/3.
|
2023-12-14 04:01:31 -06:00
|
|
|
|
|
|
|
main([FileName|_]) :-
|
|
|
|
input(FileName, Input),
|
|
|
|
transpose(Input, Rotated),
|
|
|
|
concurrent_maplist([Row, [#|Row]]>>(true), Rotated, Rocks),
|
2023-12-14 15:50:21 -06:00
|
|
|
/*concurrent_*/maplist(row_weight, Rocks, Weights),
|
2023-12-14 04:01:31 -06:00
|
|
|
sum_list(Weights, W),
|
|
|
|
writef('%t\n', [W]).
|
|
|
|
|
|
|
|
row_weight(Row, Weight) :-
|
|
|
|
phrase(rocks(Rocks), Row),
|
|
|
|
convlist([N-'#', N]>>(true), Rocks, RockCounts),
|
|
|
|
sum_list(RockCounts, Weight),
|
|
|
|
writef('>> %t <-- %t\n', [Weight, Row]).
|
|
|
|
|
|
|
|
rocks([0-0]) --> eos.
|
|
|
|
rocks([LastN-I, LastN-LastI|Rocks]) -->
|
|
|
|
[0], rocks([LastN-LastI|Rocks]),
|
|
|
|
{I is LastI + 1}.
|
|
|
|
rocks([N-I, LastN-LastI|Rocks]) -->
|
|
|
|
[1], rocks([LastN-LastI|Rocks]),
|
|
|
|
{I is LastI + 1, N is LastN + 1}.
|
|
|
|
rocks([0-I, N-'#', LastN-LastI|Rocks]) -->
|
2023-12-14 15:50:21 -06:00
|
|
|
[#], rocks([LastN-LastI|Rocks]),
|
|
|
|
{I is LastI + 1, N is LastN*(2*LastI - LastN + 1) / 2}.
|
2023-12-14 04:01:31 -06:00
|
|
|
|
|
|
|
% things below are for reading from input file
|
|
|
|
input(FileName, Input) :- phrase_from_file(lines(Input), FileName).
|
|
|
|
|
|
|
|
lines([]) --> eos, !.
|
|
|
|
lines([Line|Lines]) --> line(Line), lines(Lines).
|
|
|
|
|
|
|
|
line([]) --> "\n"; eos.
|
|
|
|
line([1|Chars]) --> "O", line(Chars).
|
|
|
|
line([0|Chars]) --> ".", line(Chars).
|
|
|
|
line(['#'|Chars]) --> "#", line(Chars).
|
|
|
|
|
|
|
|
eos([], []).
|