Working and relatively quick solution to problem 012