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