When I first met Carl Bass, he was a programmer on HOOPS — a platform-independent 3D graphics system. Carl and Garry Weigand were the founders of Ithaca Software. Carl and Garry worked on what we called "upper HOOPS" which was the part of the system that created a retained database of rendering attributes and geometric primitives. Only a few other elite programmers like Bob Covey, Brian Mathews, and Jeff Kowalski also worked on upper HOOPS. My cohorts like Milt Capsimalis and Norbert Jeske and I worked on what we called "lower HOOPS" which was the driver end that converted the database to device-specific graphics commands. HOOPS still exists today.
Carl has a son who is a senior in high school. Like all good fathers, even though he is the CEO of a Fortune 1000 company, Carl sometimes helps his son with his homework. For fun, Carl sent me something that he and his son worked on and asked me to try it.
My solution was:
The coding part of Carl's solution (also 100% correct) was:
Though different in form, our solutions are not far apart in substance. Mine is a bit verbose. Carl's is more elegant. Carl later sent me a solution that he preferred to what we each independently came up with:
The brute force way is to try every combination of small and big brick until you get a match.
The brute force method takes too long for cases where the goal row of bricks is very large. All programming challenges, even code for AutoCAD, Inventor, Revit, 3ds Max, are based on getting the correct answer but also how long it takes to get there.
In my case:
(small - small_needed + big_reach) < (goal - small_needed)
could have been reduced to:
(small + big_reach) < (goal)
to save the arithmetic operation of subtracting small_needed, but I did not think of that right away. I was building my solution as I looked at the test cases. Also for efficiency I could have avoided computing big_needed and big_reach by elimiating big_needed and placing big_reach in the else clause, but I was going for a "compute the parts and then do the tests" approach. Had I done that, I would have avoided computing these two numbers for cases where there were enough small bricks to reach the goal.
You're never too old to learn.
Proving "father knows best" is alive in the lab.