Hard solution, but ruby solution is so slow