From 95de6f1f7b9baba5f322185338193d32b50e5df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 18 Dec 2023 21:13:50 +0100 Subject: [PATCH] Added another small performance improvement for day 14 --- solvers/UParabolicReflectorDish.pas | 37 +++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/solvers/UParabolicReflectorDish.pas b/solvers/UParabolicReflectorDish.pas index 5658d5b..9f20c03 100644 --- a/solvers/UParabolicReflectorDish.pas +++ b/solvers/UParabolicReflectorDish.pas @@ -204,27 +204,34 @@ end; procedure TPlatform.Tilt(constref AIntervals: TCubeRockIntervalsList; constref ASource, ATarget: TIntegersList; const AUp: Boolean); var - i, rock, nextFree, step: Integer; - interval: TCubeRockInterval; + i, j, rock, nextFree, direction: Integer; begin if AUp then - step := 1 + direction := 1 else - step := -1; + direction := -1; for i := 0 to AIntervals.Count - 1 do begin - for interval in AIntervals[i] do + j := 0; + if AUp then + nextFree := AIntervals[i][j].Start + else + nextFree := AIntervals[i][j].Stop; + for rock in ASource[i] do begin - if AUp then - nextFree := interval.Start - else - nextFree := interval.Stop; - for rock in ASource[i] do - if (interval.Start <= rock) and (rock <= interval.Stop) then - begin - ATarget[nextFree].Add(i); - Inc(nextFree, step); - end; + // Goes to next interval if rock is not in current interval. + while AIntervals[i][j].Stop < rock do + begin + Inc(j); + if AUp then + nextFree := AIntervals[i][j].Start + else + nextFree := AIntervals[i][j].Stop; + end; + + // rock must now be in current interval. + ATarget[nextFree].Add(i); + Inc(nextFree, direction); end; ASource[i].Clear; end;