Solution to level 31 in Untrusted: http://alex.nisnevich.com/untrusted/