New solution implemented in ruby (without multi-threading)