import system'text; import system'routines; import extensions; import extensions'text; singleton Compressor { string compress(string s) { auto tb := new TextBuilder(); int count := 0; char current := s[0]; s.forEach::(ch) { if (ch == current) { count += 1 } else { tb.writeFormatted("{0}{1}",count,current); count := 1; current := ch } }; tb.writeFormatted("{0}{1}",count,current); ^ tb } string decompress(string s) { auto tb := new TextBuilder(); char current := $0; var a := new StringWriter(); s.forEach::(ch) { current := ch; if (current.isDigit()) { a.append(ch) } else { int count := a.toInt(); a.clear(); tb.fill(current,count) } }; ^ tb } } public Program() { var s := "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"; s := Compressor.compress(s); Console.printLine(s); s := Compressor.decompress(s); Console.printLine(s) }