This produces correct output quite efficiently.
In reply to Re^2: One for the weekend: challenge by BrowserUk in thread One for the weekend: challenge by BrowserUk