Forum Discussion
Halloween 2026 Scheduling Challenge 🎃
Struggling with this website... what a joke.
Trying again...
= LET(
sch, SCAN(
VALS3(HSTACK(available, treat)),
SEQUENCE(SUM(ROUNDUP(available / items_per_turn, 0))),
LAMBDA(acc,_,
LET(
arr, SORTBY(acc(1), TAKE(acc(1), , 1) = 0),
bal, SINGLE(arr),
amt, MIN(bal, items_per_turn),
VALS3(SORTBY(IFNA(EXPAND(bal - amt, 2, 2), arr), rank = 1), amt, INDEX(arr, 1, 2))
)
)
),
end, SCAN( , MAP(sch, LAMBDA(val, val(2))), SUM),
VSTACK({"Treat","Start","Stop"}, HSTACK(MAP(sch, LAMBDA(val, val(3))), DROP(VSTACK(0, end), -1), end))
)Where:
VALS3 = LAMBDA([value1],[value2],[value3], LAMBDA(x, CHOOSE(x, value1, value2, value3)))Note: SINGLE(arr) should automatically convert to "at"arr when the formula is committed to a cell. I used SINGLE because implicit intersection operators contained within a code block don't seem to be rendering properly in Microsoft Edge after posting.
This solution probably wouldn't perform all that great with a huge table/array of treats, due to the manipulation and indexing of the entire array at each iteration, but it works well for the dataset provided.
A step-through is available in the attached workbook to review the accumulated items for any given iteration (assuming the workbook is not stripped from my post again!).
Happy Halloween! 👻
One small revision to minimize redundancy...
The second accumulated value at each iteration of sch can be changed from amt to amt + acc(2), so that end can simply be evaluated as MAP(sch, LAMBDA(val, val(2))).
I'm not about to edit my previous post, as it would most likely result in the workbook being stripped away.