Library · The Fiddler: Solutions · Chapter 143
Can You Shape the Peloton?
A peloton of cyclists rides in rows. Its shape can be a triangle, a rhombus, or something in between: rows of riders widening to a widest row, then narrowing again by one rider per row, but stopping anywhere from straight after the widest row (a triangle) to a final single rider (a rhombus). With a widest row of four riders there are four such formations, shown below.
Some numbers of riders allow more than one formation. With riders there are two: a triangle five riders wide, and a rhombus four riders wide missing its bottom rider. After , what is the next number of riders with two distinct formations?
The Fiddler, Zach Wissner-Gross, September 22, 2023(original post)
The official solution appeared in the post of September 29, 2023 and gives for the main puzzle and for the Extra Credit. Both agree with the answers below.
Solution
First, count the riders in a formation. Write for the width of the widest row. A full rhombus has rows , and it holds riders: it is a square of side squashed sideways, and indeed . Every other formation is this rhombus with its bottom rows removed, and the removed rows, , form a triangle holding the triangular number . So a formation has with the rhombus and the triangle, which holds riders.
So the formations with a widest row of give the numbers from up to , spaced by triangular numbers below . Listing them row by row:
| widest row | riders for |
|---|---|
This table is complete for every count up to , because a formation with widest row has at least riders, and . So the formations for any number up to all appear above. Reading the counts above in order, each appear once, and the next count, , appears twice: as the triangle eight wide and as the rhombus six wide. That is both a square and a triangular number is exactly what makes it work here. The next two counts with two formations, and , are visible in the table too.
The computation
The program builds every formation up to a widest row of riders, row by row exactly as described in the puzzle, counts its riders, and groups the formations by count. A formation with widest row has at least riders, so every count up to has all its formations in the list, and the search only looks there.
from collections import defaultdict
W = 30 # widest formation built
shapes = defaultdict(list)
for a in range(1, W + 1): # widest row
for b in range(a): # bottom rows removed
rows = [*range(1, a + 1), *range(a - 1, b, -1)]
shapes[sum(rows)].append(rows)
# Every formation has at least 1+2+...+a riders, so any count
# up to 1+2+...+W is covered by widths at most W: complete.
limit = W * (W + 1) // 2
def smallest(k, above=0):
return min(n for n, f in shapes.items()
if above < n <= limit and len(f) >= k)
print("main:", smallest(2, above=15))
It prints
main: 36
Extra Credit
What is the fewest number of riders with three distinct formations?
Solution
Turn the formula around. A count has a formation with widest row exactly when is a triangular number with , and that last condition holds exactly when . So to list the formations of , run through the squares from the first one at least , stop when passes , and keep each for which the gap is triangular.
For the candidates are to , since is the first square past and is too big:
| triangular? | ||
|---|---|---|
| no (, ) | ||
| no (, ) |
So has three formations, none of them a triangle or a rhombus.
That no smaller count has three is a matter of checking each count below the same way, and there is no shortcut that avoids it: the counts with two formations are scattered irregularly (). The exhaustive check is the computation below. Its verdict is
The computation
The list of formations from The computation above already settles it: ask for the smallest count with at least three formations, and print their widest and last rows.
n = smallest(3)
print("extra credit:", n)
for rows in shapes[n]:
print(f" widest row {max(rows)}, last row {rows[-1]}")
It prints
extra credit: 141
widest row 12, last row 3
widest row 13, last row 8
widest row 14, last row 11