fork download
  1. object Main extends App {
  2. object Wire extends Enumeration {
  3. val white, black, purple, red, green, orange, none = Value
  4. }
  5. @annotation.tailrec
  6. def fix(fixed: List[List[String]] = List.empty, lines: List[String]): List[List[String]] = {
  7. if (!lines.contains("")) {
  8. println("""lines does not contain "".""")
  9. println(lines)
  10. lines :: fixed
  11. } else {
  12. println("""lines contains "".""")
  13. println(lines)
  14. val (current, unfixed) = lines.splitAt(lines.indexOf(""))
  15. println(s"current = $current")
  16. println(s"unfixed = $unfixed")
  17. fix(current :: fixed, unfixed.tail)
  18. }
  19. }
  20. def cut(wires: Seq[Wire.Value], prev: Wire.Value = Wire.none): Boolean = {
  21. if (wires.isEmpty) return true
  22. val current = wires.head
  23. val boom = prev match {
  24. case Wire.white => current == Wire.white || current == Wire.black
  25. case Wire.red => current != Wire.green
  26. case Wire.black => current == Wire.white || current == Wire.green || current == Wire.orange
  27. case Wire.orange => current != Wire.red && current != Wire.black
  28. case Wire.green => current != Wire.orange && current != Wire.white
  29. case Wire.purple => current == Wire.purple || current == Wire.orange || current == Wire.green || current == Wire.white
  30. case Wire.none => false
  31. }
  32. if (boom) false else cut(wires.tail, current)
  33. }
  34. fix(lines = io.Source.stdin.getLines.toList).map(_.map(Wire.withName)).map(cut(_)).reverse.foreach(s => println(if (s) "Bomb defused" else "Boom"))
  35. }
Success #stdin #stdout 0.18s 322432KB
stdin
white
red
green
white

white
orange
green
white
stdout
lines contains "".
List(white, red, green, white, , white, orange, green, white)
current = List(white, red, green, white)
unfixed = List(, white, orange, green, white)
lines does not contain "".
List(white, orange, green, white)
Bomb defused
Boom