[2016-11-21] Challenge #293 [Easy] Defusing the bomb

+/u/CompileBot

object Main extends App {
  object Wire extends Enumeration {
    val white, black, purple, red, green, orange, none = Value
  }
  @annotation.tailrec
  def fix(fixed: List[List[String]] = List.empty, lines: List[String]): List[List[String]] = {
    if (!lines.contains("")) {
      println("""lines does not contain "".""")
      println(lines)
      lines :: fixed
    } else {
      println("""lines contains "".""")
      println(lines)
      val (current, unfixed) = lines.splitAt(lines.indexOf(""))
      println(s"current = $current")
      println(s"unfixed = $unfixed")
      fix(current :: fixed, unfixed.tail)
    }
  }
  def cut(wires: Seq[Wire.Value], prev: Wire.Value = Wire.none): Boolean = {
    if (wires.isEmpty) return true
    val current = wires.head
    val boom = prev match {
      case Wire.white => current == Wire.white || current == Wire.black
      case Wire.red => current != Wire.green
      case Wire.black => current == Wire.white || current == Wire.green || current == Wire.orange
      case Wire.orange => current != Wire.red && current != Wire.black
      case Wire.green => current != Wire.orange && current != Wire.white
      case Wire.purple => current == Wire.purple || current == Wire.orange || current == Wire.green || current == Wire.white
      case Wire.none => false
    }
    if (boom) false else cut(wires.tail, current)
  }
  fix(lines = io.Source.stdin.getLines.toList).map(_.map(Wire.withName)).map(cut(_)).reverse.foreach(s => println(if (s) "Bomb defused" else "Boom"))
}

Input:

white
red
green
white

white
orange
green
white
/r/dailyprogrammer Thread