fork download
  1. object Main extends App {
  2. def validate(s: String): Boolean = {
  3. if (s.length != 10) false
  4. else {
  5. val hand1 = s.slice(0, 4).reverse
  6. val hand2 = s.slice(6, 10)
  7. !hand1.contains("01") && !hand2.contains("01")
  8. }
  9. }
  10.  
  11. def decode(s: String): Int = {
  12. def decodeInner(s: String): Int = (if (s.head == '0') 0 else 5) + s.tail.filter(_ == '1').size
  13. decodeInner(s.slice(0, 5).reverse) * 10 + decodeInner(s.slice(5, 10))
  14. }
  15.  
  16. Seq("0111011100", "1010010000", "0011101110", "0000110000", "1111110001").foreach(s => println(s"$s -> ${if (validate(s)) decode(s) else "Invalid"}"))
  17. }
Success #stdin #stdout 0.16s 322432KB
stdin
Standard input is empty
stdout
0111011100 -> 37
1010010000 -> Invalid
0011101110 -> 73
0000110000 -> 55
1111110001 -> Invalid