This commit is contained in:
Dory 2023-12-19 23:15:01 -08:00
parent cbe8fbde87
commit 43a83d487b

View File

@ -4,11 +4,19 @@
main([FileName|_]) :-
input(FileName, Workflows),
findall(Limit, (wf(Workflows, in, Route, Limit), write(Route), write(" - "), write(Limit), nl), Limits),
or_limits(Limits, NewLimit),
write(NewLimit), nl,
choices(NewLimit, Answer),
write(Answer), nl.
findall(
Count-Limit,
(
wf(Workflows, in, Route, Limit),
Limit = [x-Xmin-Xmax, m-Mmin-Mmax, a-Amin-Amax, s-Smin-Smax],
Count is (Xmax - Xmin)*(Mmax - Mmin)*(Amax - Amin)*(Smax - Smin),
write(Route), write(" - "), write(Limit), write(" - "), write(Count), nl
),
Limits),
length(Limits, LenLimits), write("lenlimits = "), write(LenLimits), nl,
maplist([Count-Limit, Count]>>(true), Limits, CountsOnly),
sum_list(CountsOnly, Answer), write("answer = "), write(Answer), nl,
true.
choices(Limit, N) :-
foldl([_-Min-Max, V0, V]>>(V is V0*(Max - Min + 1)), Limit, 1, N).
@ -22,8 +30,8 @@ or_limits([Limit1, Limit2 | Limits], Limit) :-
NewLimit),
or_limits([NewLimit|Limits], Limit).
wf(_, accept, [accept], [x-1-4000, m-1-4000, a-1-4000, s-1-4000]).
% x -> [Min, Max)
wf(_, accept, [accept], [x-1-4001, m-1-4001, a-1-4001, s-1-4001]).
wf(Workflows, WorkflowName, [WorkflowName|Route], Limits) :-
\+ WorkflowName = accept, \+ WorkflowName = reject,
member(WorkflowName-Rules, Workflows),
@ -46,9 +54,9 @@ with_rule(Workflows, [Attr-Cond-N-Dest|Rules], Route, NewLimits) :-
negate('<'-N, '>'-NewN) :- NewN is N - 1.
negate('>'-N, '<'-NewN) :- NewN is N + 1.
combine(Min-Max, '<'-N, Min-NewMax) :- NewMax is min(N-1, Max), Min < NewMax.
combine(Min-Max, '<'-N, Min-NewMax) :- NewMax is min(N, Max), Min < NewMax.
combine(Min-Max, '>'-N, NewMin-Max) :- NewMin is max(N+1, Min), NewMin < Max.
% input parsing stuff below
input(FileName, Workflows) :-
phrase_from_file((workflows(Workflows), remainder(_)), FileName).