From 0f5dcc20ce1106c50eb6aace92846cb7f679e6ec Mon Sep 17 00:00:00 2001 From: Dory Date: Thu, 14 Dec 2023 22:24:55 -0800 Subject: [PATCH] d14p2 for real --- 14/part2.pl | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/14/part2.pl b/14/part2.pl index d1c7e7e..3f613f4 100644 --- a/14/part2.pl +++ b/14/part2.pl @@ -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.