Solution to code challenge on codegolf.stackexchange.com