Converted my own Python solution for #31 without re-understanding it