d14p2 for real

This commit is contained in:
Dory 2023-12-14 22:24:55 -08:00
parent a6c506874c
commit 0f5dcc20ce

View File

@ -4,14 +4,45 @@
main([FileName|_]) :- main([FileName|_]) :-
input(FileName, Input), input(FileName, Input),
concurrent_maplist(row_weight, Rocks, Weights), rolls(Input, 0, [], First-Cycle-Hist),
sum_list(Weights, W), reverse(Hist, [_|TrueHist]),
writef('%t\n', [W]). BillionOffset is ((999999999 - First) mod Cycle) + First,
nth0(BillionOffset, TrueHist, BillionMap),
% maplist({TrueHist}/[M]>>(
% nth0(Nth, TrueHist, M),
% map_weight(M, W),
% writef('Weight(%t) = %t\n', [Nth, W]),
% print(M)), TrueHist),
write('======================\n'),
print(BillionMap),
map_weight(BillionMap, FinalWeight),
writef('First = %t, Cycle = %t, FinalOffset = %t, Answer = %t\n',
[First, Cycle, BillionOffset, FinalWeight]).
row_weight(Row, Weight) :- map_weight(Map, W) :- rotate(Map, Rotated), map_weight(Rotated, _, W).
phrase(rock_counts(Rocks), Row),
convlist([N-z-_, N]>>(true), Rocks, RockCounts), map_weight([_], 0, 0).
sum_list(RockCounts, Weight). map_weight([Row|Map], RowI, W) :-
map_weight(Map, PrevRowI, PrevW),
RowI is PrevRowI + 1,
include(['O']>>(true), Row, Rocks),
length(Rocks, NRocks),
W is PrevW + RowI*NRocks.
rolls(Map, N, Hist, First-Cycle-Hist) :-
match(Map, Hist, Cycle),
First is N - Cycle - 1,
!.
rolls(Map, N, Hist, X) :-
roll(Map, NewMap),
NextN is N + 1,
NewHist = [Map|Hist],
rolls(NewMap, NextN, NewHist, X).
match(Map, [Map|_], 1) :- !.
match(Map, [_|Entries], N) :-
match(Map, Entries, NextN),
N is NextN + 1.
% North is to the left % North is to the left
roll(Map, NewMap) :- roll(Map, NewMap) :-
@ -38,12 +69,9 @@ print(Map) :-
write('\n'). write('\n').
collapse(Row, NewRow) :- collapse(Row, NewRow) :-
% abolish_private_tables,
% table(rock_counts/3), table(condense/3), table(reexpand/3),
phrase(rock_counts(Counts), Row), phrase(rock_counts(Counts), Row),
phrase(condense(Zs), Counts), phrase(condense(Zs), Counts),
phrase(reexpand(Zs), NewRow), phrase(reexpand(Zs), NewRow),
% NewRow = Zs,
true. true.
reexpand([]) --> eos. reexpand([]) --> eos.