Clarence Risher's solution:

neat challenge.  beyond my abilities to solve generally in code, but a fun little task to solve manually.