bloxorz's solution in scala