Solution for https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/0000000000088231