Refactored solution of Round #276 Problem A