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

white
orange
green
white
stdout
Bomb defused
Boom