Solution by Garrett Goebel