cleanup d20p1

This commit is contained in:
Dory 2023-12-20 02:49:40 -08:00
parent a4856cf9ef
commit 3475d23962

View File

@ -2,51 +2,10 @@
:- use_module(library(dcg/basics)). :- use_module(library(dcg/basics)).
:- initialization(main, main). :- initialization(main, main).
% ================================ begin stupid ===============================
main([FileName|_]) :- main([FileName|_]) :-
input(FileName, Circuit), input(FileName, Circuit),
cycle(Circuit, 0, [], [], First-Cycle-_, Counts), run1000(Circuit, Answer),
NCycles is 1000 div Cycle, Leftovers is 1000 mod Cycle, write(Answer), nl.
CycleEnd1 is First + Cycle,
slice(Counts, 0, First, FirstCounts),
sumpairs(FirstCounts, FirstL-FirstH),
slice(Counts, First, CycleEnd1, CycleCounts),
sumpairs(CycleCounts, CycleL-CycleH),
slice(Counts, First, Leftovers, LeftoverCounts),
sumpairs(LeftoverCounts, LeftoverL-LeftoverH),
Answer is (FirstL + NCycles*CycleL + LeftoverL)*
(FirstH + NCycles*CycleH + LeftoverH),
write("answer="), write(Answer), nl.
sumpairs([], 0-0).
sumpairs([A-B|List], SumA-SumB) :-
sumpairs(List, A1-B1), SumA is A + A1, SumB is B + B1.
cycle(Circuit, N, Hist, Counts, First-Cycle-Hist, Counts) :-
match(Circuit, Hist, Cycle),
write("match. N="), write(N), write(", cycle="), write(Cycle), nl,
First is N - Cycle,
!.
cycle(Circuit, N, Hist, Counts, X, Y) :-
% \+ match(Circuit, Hist, _), % remove this for performance.
run([button-l-broadcaster], Circuit, NewCircuit, Ls-Hs),
NewN is N + 1,
NewHist = [Circuit|Hist],
% write(NewN), write(": "), write(Ls-Hs), write(" - "), write(NewCircuit), nl,
cycle(NewCircuit, NewN, NewHist, [Ls-Hs|Counts], X, Y).
match(Map, [Map|_], 1) :- !.
match(Map, [_|Entries], N) :-
match(Map, Entries, NextN),
N is NextN + 1.
slice(L, From, To, R):-
ToX is To + 1,
length(LFrom, From), length([_|LTo], ToX),
append(LTo, _, L), append(LFrom, R, LTo).
% ================================ end stupid ===============================
% part1 % part1
run1000(Circuit, Answer) :- run1000(Circuit, Answer) :-