Very impressed with everyones work. Have just tweaked code to make it a bit more idiomaticcly ruby and to remove numerous loops and ifs.