first half of part2 seems to work I hate swipl tabling

This commit is contained in:
2023-12-14 13:50:21 -08:00
parent db08023b9e
commit a6c506874c
2 changed files with 93 additions and 7 deletions

View File

@@ -1,17 +1,17 @@
:- use_module(library(pio)).
:- use_module(library(clpfd)).
:- initialization(main, main).
:- table rocks/3.
main([FileName|_]) :-
input(FileName, Input),
transpose(Input, Rotated),
concurrent_maplist([Row, [#|Row]]>>(true), Rotated, Rocks),
concurrent_maplist(row_weight, Rocks, Weights),
/*concurrent_*/maplist(row_weight, Rocks, Weights),
sum_list(Weights, W),
writef('%t\n', [W]).
row_weight(Row, Weight) :-
abolish_private_tables, table(rocks/3),
phrase(rocks(Rocks), Row),
convlist([N-'#', N]>>(true), Rocks, RockCounts),
sum_list(RockCounts, Weight),
@@ -24,12 +24,9 @@ rocks([LastN-I, LastN-LastI|Rocks]) -->
rocks([N-I, LastN-LastI|Rocks]) -->
[1], rocks([LastN-LastI|Rocks]),
{I is LastI + 1, N is LastN + 1}.
rocks([0-I, 0-LastI|Rocks]) -->
['#'], rocks([0-LastI|Rocks]),
{I is LastI + 1}.
rocks([0-I, N-'#', LastN-LastI|Rocks]) -->
['#'], rocks([LastN-LastI|Rocks]),
{LastN =\= 0, I is LastI + 1, N is LastN*(2*LastI - LastN + 1) / 2}.
[#], rocks([LastN-LastI|Rocks]),
{I is LastI + 1, N is LastN*(2*LastI - LastN + 1) / 2}.
% things below are for reading from input file
input(FileName, Input) :- phrase_from_file(lines(Input), FileName).