http://www.perlmonks.org?node_id=986183

If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader !!! :)