Solution to an algorithm brainteaser implemented graphically