Committing a slow, but working, solution. It isn't completely naive, but it takes several 10s of seconds to run.