Tentative solution to problem 146 but it takes more than a couple minutes to run.