optimal solution and test harness
ad-hoc solution added for comparison