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|_]) :-
input(FileName, Input),
concurrent_maplist(row_weight, Rocks, Weights),
sum_list(Weights, W),
writef('%t\n', [W]).
rolls(Input, 0, [], First-Cycle-Hist),
reverse(Hist, [_|TrueHist]),
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) :-
phrase(rock_counts(Rocks), Row),
convlist([N-z-_, N]>>(true), Rocks, RockCounts),
sum_list(RockCounts, Weight).
map_weight(Map, W) :- rotate(Map, Rotated), map_weight(Rotated, _, W).
map_weight([_], 0, 0).
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
roll(Map, NewMap) :-
@ -38,12 +69,9 @@ print(Map) :-
write('\n').
collapse(Row, NewRow) :-
% abolish_private_tables,
% table(rock_counts/3), table(condense/3), table(reexpand/3),
phrase(rock_counts(Counts), Row),
phrase(condense(Zs), Counts),
phrase(reexpand(Zs), NewRow),
% NewRow = Zs,
true.
reexpand([]) --> eos.