A useful “Count-Once” widget.

Often somebody will present a challenge to construct a pattern containing some number (say B1) of blue balls followed by some other number (say R1) of red balls followed by some other number (say B2) of blue balls, followed by R2 red balls, and so on, so that the output looks like:

Rn Bn Rn-1 Bn-1 … R3 B3 R2 B2 R1 B1.

For example, the challenge of producing a Fibonacci sequence

B1 = 1

R1 = 1

B2 = 2

R2 = 3

B3 = 5

R3 = 8

and so on.

It dawned on me that what is needed is a widget that can count out a certain number n of balls of a given color, but then the widget is just ignored and future balls just pass through without any effect. If we can build such widgets, then we can stack them to get any sequence of reds and blues we’d like.

EXAMPLE:

Let’s write “W(b,n)” to indicate one of these widgets that counts out n blue balls, and “W(r,n)” one that counts out n red balls. (By “counts out” I mean that it triggers n balls of the given color, then after that, lets balls pass through it without any effect.)

By building a stack of these widgets as follows, we can solve the Fibonacci challenge, and, any challenge, for that matter (assuming we are not constrained to using the original board and box parts).

Below, the widgets are arranged vertically in a tower, with W(b,1) at the top,

and all balls pass through all widgets.

W(b,1)

W(r,1)

W(b,2)

W(r,3)

W(b,5)

W(r,8)

W(b,13)

W(r,21)

and so on.

With such a stack one blue ball is counted out, then red is triggered and all future balls pass through the W(b,1) widget.

Then the W(r,1) widget counts out 1 red ball, triggers a blue ball, and all future balls just pass through it.

Then the blue ball passes through the W(b,1) and W(r,1) widgets, and enters the W(b,2) widget, which counts out another blue ball, then triggers a red, and all future balls just pass through the W(b,2) widget. Etc.

I hope I’ve explained this clearly enough.

So, the challenge is to show how to build one of these widgets:

As a concrete example, build W(b,5). It should have the following behavior.

You push the blue lever.

More blue balls are triggered, then after that reds are triggered.

The output sequence is 5 blues, then repeating reds in perpetuity.

(So, the ball lineup at the bottom looks like … rrrrrrrrrrrrrbbbbb)

I did this with the simulator, but the window accidentally closed, and I’m too lazy to do it again (for now). I figure I’ll let somebody else show their solution.