Which wire to cut

29

3

This task is about compressing and processing a sequence of conditionals.


In the game Keep Talking and Nobody Explodes, a bomb defuser must disarm a bomb with the help of instructions relayed by experts consulting a convoluted Bomb Defusal Manual. This challenge deals with the module "On the Subject of Wires", explained on page 5 of the manual. The defuser is presented with an array of colored wires. Only one of them is safe to cut -- the rest detonate the bomb.

Your code acts as the expert to determine which wire to cut based on the number and colors of the wires, as per the instructions in the manual reproduced under "Wire cutting rules".

Input: An ordered list or string of 3, 4, 5, or 6 wire colors, represented by uppercase letters:

  • B: Black
  • U: Blue
  • R: Red
  • W: White
  • Y: Yellow

Note that blue is U, not B.

The input also includes a bit (True/False or 0/1) for whether the last digit of the bomb's serial number is odd, a condition used in some rules.

You should not take the number of wires as a separate input, but derive it from the list or string of colors. You may have your list or string have a terminator element after the colors, perhaps if your language cannot tell how long it is. This terminator should be a fixed value that doesn't encode additional information.

Output: A number 1 through 6, indicating which wire to cut. This may not be zero-indexed.

Wire cutting rules: These rules are reproduced from page 5 of the defusal manual

3 wires:
  If there are no red wires, cut the second wire.
  Otherwise, if the last wire is white, cut the last wire.
  Otherwise, if there is more than one blue wire, cut the last blue wire.
  Otherwise, cut the last wire.

4 wires:
  If there is more than one red wire and the last digit of the serial number is odd, cut the last red wire.
  Otherwise, if the last wire is yellow and there are no red wires, cut the first wire.
  Otherwise, if there is exactly one blue wire, cut the first wire.
  Otherwise, if there is more than one yellow wire, cut the last wire.
  Otherwise, cut the second wire.

5 wires:
  If the last wire is black and the last digit of the serial number is odd, cut the fourth wire.
  Otherwise, if there is exactly one red wire and there is more than one yellow wire, cut the first wire.
  Otherwise, if there are no black wires, cut the second wire.
  Otherwise, cut the first wire.

6 wires:
  If there are no yellow wires and the last digit of the serial number is odd, cut the third wire.
  Otherwise, if there is exactly one yellow wire and there is more than one white wire, cut the fourth wire.
  Otherwise, if there are no red wires, cut the last wire.
  Otherwise, cut the fourth wire.

Reference solution (TIO)

This code is in Python.

def wire_to_cut(wires, serial_odd):
    """Return the index of the wire to cut, one-indexed. This is a number 1 to 6.
    wires: A list of 3 through 6 color characters from BURWY
    serial_odd: A Boolean for whether the last digit of the serial is odd.

    >>> wire_to_cut(['R', 'B', 'R', 'W'], True):
    3
    """
    num_wires = len(wires)
    last_wire = wires[-1]

    if num_wires == 3:
        if wires.count('R') == 0:
            return 2
        elif last_wire == 'W':
            return num_wires
        elif wires.count('U') > 1:
            # Last blue wire
            return ''.join(wires).rindex('U') + 1
        else:
            return num_wires

    elif num_wires == 4:
        if wires.count('R') > 1 and serial_odd:
            # Last red wire
            return ''.join(wires).rindex('R') + 1
        elif last_wire == 'Y' and wires.count('R') == 0:
            return 1
        elif wires.count('U') == 1:
            return 1
        elif wires.count('Y') > 1:
            return num_wires
        else:
            return 2

    elif num_wires == 5:
        if last_wire == 'B' and serial_odd:
            return 4
        elif wires.count('R') == 1 and wires.count('Y') > 1:
            return 1
        elif wires.count('B') == 0:
            return 2
        else:
            return 1

    elif num_wires == 6:
        if wires.count('Y') == 0 and serial_odd:
            return 3
        elif wires.count('Y') == 1 and wires.count('W') > 1:
            return 4
        elif wires.count('R') == 0:
            return num_wires
        else:
            return 4

    else:
        raise ValueError("Wrong number of wires.")

Test cases

As (input, output) where input = (wires, odd_serial).

((['B', 'B', 'B'], False), 2)
((['B', 'B', 'Y'], True), 2)
((['B', 'R', 'R'], False), 3)
((['B', 'W', 'U'], True), 2)
((['U', 'B', 'B'], True), 2)
((['U', 'B', 'Y'], False), 2)
((['U', 'U', 'R'], True), 2)
((['U', 'U', 'U'], False), 2)
((['U', 'R', 'R'], True), 3)
((['U', 'Y', 'Y'], False), 2)
((['R', 'B', 'U'], False), 3)
((['R', 'B', 'Y'], False), 3)
((['R', 'U', 'B'], False), 3)
((['R', 'R', 'U'], False), 3)
((['R', 'W', 'U'], True), 3)
((['W', 'B', 'W'], False), 2)
((['W', 'B', 'Y'], True), 2)
((['W', 'R', 'U'], True), 3)
((['W', 'W', 'B'], True), 2)
((['W', 'W', 'U'], True), 2)
((['W', 'Y', 'W'], True), 2)
((['Y', 'U', 'B'], True), 2)
((['Y', 'U', 'W'], False), 2)
((['Y', 'R', 'U'], False), 3)
((['Y', 'Y', 'B'], False), 2)
((['Y', 'Y', 'B'], True), 2)
((['B', 'B', 'U', 'U'], True), 2)
((['B', 'B', 'R', 'W'], True), 2)
((['B', 'B', 'R', 'Y'], True), 2)
((['B', 'U', 'B', 'R'], False), 1)
((['B', 'U', 'R', 'W'], False), 1)
((['B', 'U', 'W', 'R'], True), 1)
((['B', 'U', 'W', 'Y'], True), 1)
((['B', 'U', 'Y', 'R'], False), 1)
((['B', 'R', 'U', 'B'], True), 1)
((['B', 'R', 'R', 'B'], True), 3)
((['B', 'R', 'Y', 'W'], True), 2)
((['B', 'R', 'Y', 'Y'], True), 4)
((['B', 'W', 'R', 'U'], True), 1)
((['B', 'W', 'Y', 'B'], False), 2)
((['B', 'Y', 'R', 'U'], False), 1)
((['B', 'Y', 'R', 'R'], False), 2)
((['U', 'B', 'R', 'W'], False), 1)
((['U', 'B', 'W', 'Y'], False), 1)
((['U', 'B', 'Y', 'W'], True), 1)
((['U', 'U', 'R', 'W'], True), 2)
((['U', 'U', 'W', 'B'], False), 2)
((['U', 'U', 'W', 'Y'], False), 1)
((['U', 'R', 'B', 'U'], False), 2)
((['U', 'R', 'Y', 'U'], True), 2)
((['U', 'R', 'Y', 'W'], False), 1)
((['U', 'R', 'Y', 'Y'], False), 1)
((['U', 'W', 'U', 'Y'], False), 1)
((['U', 'W', 'W', 'W'], False), 1)
((['U', 'Y', 'B', 'B'], False), 1)
((['U', 'Y', 'B', 'W'], True), 1)
((['U', 'Y', 'U', 'R'], True), 2)
((['U', 'Y', 'R', 'W'], False), 1)
((['R', 'B', 'R', 'R'], False), 2)
((['R', 'U', 'B', 'B'], True), 1)
((['R', 'U', 'W', 'B'], False), 1)
((['R', 'R', 'B', 'R'], True), 4)
((['R', 'R', 'W', 'R'], True), 4)
((['R', 'R', 'W', 'W'], True), 2)
((['R', 'R', 'Y', 'Y'], False), 4)
((['R', 'R', 'Y', 'Y'], True), 2)
((['R', 'W', 'U', 'W'], True), 1)
((['R', 'W', 'W', 'U'], False), 1)
((['R', 'W', 'Y', 'W'], False), 2)
((['R', 'Y', 'R', 'U'], False), 1)
((['R', 'Y', 'Y', 'W'], False), 4)
((['W', 'B', 'U', 'R'], False), 1)
((['W', 'B', 'U', 'Y'], False), 1)
((['W', 'U', 'B', 'Y'], False), 1)
((['W', 'U', 'U', 'W'], True), 2)
((['W', 'U', 'R', 'W'], False), 1)
((['W', 'W', 'R', 'U'], False), 1)
((['W', 'Y', 'R', 'R'], False), 2)
((['W', 'Y', 'Y', 'U'], False), 1)
((['W', 'Y', 'Y', 'Y'], True), 1)
((['Y', 'B', 'B', 'R'], True), 2)
((['Y', 'B', 'W', 'U'], False), 1)
((['Y', 'B', 'W', 'W'], False), 2)
((['Y', 'U', 'R', 'Y'], False), 1)
((['Y', 'R', 'B', 'R'], False), 2)
((['Y', 'R', 'U', 'R'], True), 4)
((['Y', 'R', 'R', 'Y'], False), 4)
((['Y', 'R', 'W', 'U'], False), 1)
((['Y', 'R', 'Y', 'B'], False), 4)
((['Y', 'R', 'Y', 'B'], True), 4)
((['Y', 'W', 'U', 'B'], False), 1)
((['Y', 'W', 'R', 'R'], True), 4)
((['Y', 'W', 'W', 'R'], True), 2)
((['Y', 'W', 'W', 'Y'], True), 1)
((['Y', 'W', 'Y', 'U'], False), 1)
((['Y', 'Y', 'B', 'B'], True), 4)
((['Y', 'Y', 'R', 'R'], True), 4)
((['B', 'B', 'B', 'R', 'W'], False), 1)
((['B', 'B', 'R', 'R', 'W'], False), 1)
((['B', 'U', 'B', 'W', 'U'], True), 1)
((['B', 'R', 'R', 'U', 'R'], True), 1)
((['B', 'R', 'R', 'W', 'W'], False), 1)
((['B', 'R', 'Y', 'Y', 'R'], False), 1)
((['B', 'W', 'B', 'W', 'B'], False), 1)
((['B', 'W', 'U', 'B', 'U'], True), 1)
((['B', 'W', 'R', 'U', 'W'], True), 1)
((['B', 'W', 'R', 'W', 'B'], False), 1)
((['B', 'W', 'W', 'R', 'U'], False), 1)
((['B', 'W', 'W', 'R', 'U'], True), 1)
((['B', 'W', 'W', 'W', 'B'], False), 1)
((['B', 'W', 'Y', 'R', 'Y'], True), 1)
((['B', 'Y', 'B', 'W', 'U'], True), 1)
((['B', 'Y', 'U', 'W', 'B'], True), 4)
((['B', 'Y', 'U', 'Y', 'W'], False), 1)
((['U', 'B', 'R', 'W', 'Y'], False), 1)
((['U', 'B', 'W', 'B', 'R'], False), 1)
((['U', 'B', 'W', 'B', 'W'], False), 1)
((['U', 'B', 'W', 'Y', 'R'], False), 1)
((['U', 'B', 'Y', 'U', 'B'], True), 4)
((['U', 'B', 'Y', 'U', 'Y'], False), 1)
((['U', 'B', 'Y', 'R', 'W'], False), 1)
((['U', 'U', 'B', 'B', 'U'], True), 1)
((['U', 'U', 'R', 'U', 'W'], True), 2)
((['U', 'U', 'Y', 'U', 'R'], True), 2)
((['U', 'U', 'Y', 'U', 'W'], False), 2)
((['U', 'R', 'B', 'Y', 'Y'], False), 1)
((['U', 'R', 'U', 'B', 'Y'], False), 1)
((['U', 'R', 'W', 'W', 'B'], False), 1)
((['U', 'R', 'Y', 'Y', 'W'], False), 1)
((['U', 'W', 'B', 'U', 'B'], True), 4)
((['U', 'W', 'U', 'U', 'B'], True), 4)
((['U', 'W', 'R', 'U', 'Y'], True), 2)
((['U', 'W', 'R', 'R', 'R'], True), 2)
((['U', 'W', 'R', 'R', 'W'], False), 2)
((['U', 'W', 'R', 'Y', 'W'], True), 2)
((['U', 'W', 'W', 'Y', 'R'], True), 2)
((['U', 'Y', 'B', 'W', 'Y'], False), 1)
((['U', 'Y', 'U', 'R', 'W'], True), 2)
((['U', 'Y', 'R', 'R', 'U'], False), 2)
((['U', 'Y', 'Y', 'B', 'W'], False), 1)
((['U', 'Y', 'Y', 'R', 'B'], True), 4)
((['U', 'Y', 'Y', 'Y', 'R'], False), 1)
((['R', 'B', 'B', 'W', 'U'], False), 1)
((['R', 'B', 'U', 'B', 'Y'], False), 1)
((['R', 'B', 'R', 'R', 'Y'], True), 1)
((['R', 'B', 'W', 'W', 'R'], True), 1)
((['R', 'B', 'W', 'W', 'W'], False), 1)
((['R', 'U', 'U', 'B', 'U'], True), 1)
((['R', 'U', 'U', 'R', 'Y'], False), 2)
((['R', 'U', 'R', 'B', 'W'], False), 1)
((['R', 'U', 'R', 'Y', 'R'], True), 2)
((['R', 'R', 'B', 'U', 'U'], True), 1)
((['R', 'R', 'B', 'R', 'W'], True), 1)
((['R', 'R', 'W', 'B', 'Y'], True), 1)
((['R', 'R', 'Y', 'Y', 'B'], False), 1)
((['R', 'W', 'U', 'Y', 'W'], False), 2)
((['R', 'W', 'Y', 'B', 'U'], True), 1)
((['R', 'Y', 'B', 'U', 'U'], True), 1)
((['R', 'Y', 'B', 'R', 'Y'], True), 1)
((['R', 'Y', 'B', 'W', 'R'], True), 1)
((['R', 'Y', 'R', 'U', 'U'], False), 2)
((['R', 'Y', 'Y', 'W', 'B'], True), 4)
((['R', 'Y', 'Y', 'W', 'W'], True), 1)
((['W', 'B', 'R', 'R', 'R'], False), 1)
((['W', 'U', 'U', 'U', 'B'], False), 1)
((['W', 'U', 'U', 'R', 'B'], False), 1)
((['W', 'U', 'R', 'B', 'R'], False), 1)
((['W', 'U', 'W', 'W', 'R'], True), 2)
((['W', 'U', 'Y', 'R', 'W'], True), 2)
((['W', 'R', 'R', 'B', 'Y'], True), 1)
((['W', 'W', 'U', 'B', 'W'], True), 1)
((['W', 'W', 'U', 'W', 'R'], False), 2)
((['W', 'W', 'W', 'W', 'B'], False), 1)
((['W', 'W', 'W', 'W', 'W'], False), 2)
((['W', 'W', 'Y', 'W', 'U'], True), 2)
((['W', 'W', 'Y', 'Y', 'R'], False), 1)
((['W', 'Y', 'R', 'B', 'B'], False), 1)
((['W', 'Y', 'W', 'B', 'W'], True), 1)
((['W', 'Y', 'Y', 'W', 'U'], True), 2)
((['Y', 'B', 'U', 'R', 'B'], True), 4)
((['Y', 'B', 'U', 'Y', 'R'], False), 1)
((['Y', 'B', 'R', 'Y', 'Y'], False), 1)
((['Y', 'B', 'W', 'U', 'B'], True), 4)
((['Y', 'B', 'Y', 'R', 'R'], False), 1)
((['Y', 'U', 'U', 'U', 'U'], False), 2)
((['Y', 'U', 'R', 'W', 'B'], False), 1)
((['Y', 'U', 'W', 'U', 'Y'], True), 2)
((['Y', 'U', 'Y', 'Y', 'W'], False), 2)
((['Y', 'R', 'R', 'R', 'Y'], False), 2)
((['Y', 'R', 'R', 'Y', 'R'], False), 2)
((['Y', 'R', 'W', 'W', 'U'], False), 2)
((['Y', 'W', 'B', 'R', 'U'], True), 1)
((['Y', 'W', 'U', 'U', 'W'], True), 2)
((['Y', 'W', 'U', 'R', 'B'], False), 1)
((['Y', 'W', 'R', 'R', 'R'], True), 2)
((['Y', 'W', 'R', 'Y', 'R'], False), 2)
((['Y', 'W', 'W', 'B', 'U'], True), 1)
((['Y', 'W', 'W', 'W', 'B'], False), 1)
((['Y', 'Y', 'R', 'Y', 'U'], False), 1)
((['B', 'B', 'B', 'B', 'R', 'U'], False), 4)
((['B', 'B', 'B', 'R', 'R', 'R'], True), 3)
((['B', 'B', 'R', 'U', 'W', 'Y'], False), 4)
((['B', 'B', 'R', 'R', 'R', 'B'], True), 3)
((['B', 'B', 'W', 'U', 'B', 'B'], False), 6)
((['B', 'B', 'W', 'U', 'B', 'U'], True), 3)
((['B', 'B', 'W', 'W', 'B', 'R'], True), 3)
((['B', 'B', 'Y', 'Y', 'W', 'R'], False), 4)
((['B', 'U', 'B', 'B', 'W', 'U'], False), 6)
((['B', 'U', 'U', 'W', 'W', 'Y'], True), 4)
((['B', 'U', 'U', 'Y', 'Y', 'R'], False), 4)
((['B', 'U', 'R', 'R', 'B', 'Y'], True), 4)
((['B', 'U', 'W', 'B', 'W', 'Y'], True), 4)
((['B', 'U', 'Y', 'R', 'R', 'R'], False), 4)
((['B', 'U', 'Y', 'R', 'Y', 'B'], False), 4)
((['B', 'R', 'U', 'B', 'U', 'B'], True), 3)
((['B', 'R', 'R', 'R', 'Y', 'B'], True), 4)
((['B', 'R', 'R', 'W', 'B', 'R'], True), 3)
((['B', 'R', 'Y', 'B', 'R', 'W'], False), 4)
((['B', 'R', 'Y', 'W', 'B', 'Y'], False), 4)
((['B', 'W', 'U', 'Y', 'U', 'W'], False), 4)
((['B', 'W', 'R', 'U', 'Y', 'Y'], True), 4)
((['B', 'W', 'R', 'Y', 'U', 'W'], False), 4)
((['B', 'W', 'W', 'Y', 'U', 'R'], False), 4)
((['B', 'W', 'Y', 'R', 'B', 'R'], False), 4)
((['B', 'W', 'Y', 'W', 'Y', 'U'], False), 6)
((['B', 'Y', 'B', 'R', 'B', 'R'], True), 4)
((['B', 'Y', 'U', 'B', 'Y', 'U'], False), 6)
((['B', 'Y', 'R', 'U', 'Y', 'U'], True), 4)
((['B', 'Y', 'R', 'R', 'W', 'W'], True), 4)
((['B', 'Y', 'W', 'W', 'U', 'B'], True), 4)
((['U', 'B', 'B', 'W', 'R', 'R'], True), 3)
((['U', 'B', 'W', 'B', 'W', 'U'], False), 6)
((['U', 'B', 'Y', 'U', 'B', 'R'], False), 4)
((['U', 'U', 'B', 'B', 'W', 'Y'], False), 6)
((['U', 'U', 'B', 'W', 'B', 'B'], True), 3)
((['U', 'U', 'B', 'Y', 'Y', 'Y'], False), 6)
((['U', 'U', 'U', 'B', 'U', 'Y'], True), 6)
((['U', 'U', 'U', 'B', 'Y', 'Y'], False), 6)
((['U', 'U', 'U', 'Y', 'W', 'B'], False), 6)
((['U', 'U', 'R', 'U', 'W', 'R'], True), 3)
((['U', 'U', 'Y', 'W', 'W', 'U'], True), 4)
((['U', 'U', 'Y', 'Y', 'B', 'R'], False), 4)
((['U', 'R', 'B', 'R', 'Y', 'R'], False), 4)
((['U', 'R', 'B', 'R', 'Y', 'Y'], True), 4)
((['U', 'R', 'R', 'B', 'U', 'R'], False), 4)
((['U', 'R', 'W', 'B', 'B', 'B'], False), 4)
((['U', 'R', 'W', 'Y', 'U', 'U'], True), 4)
((['U', 'R', 'Y', 'U', 'B', 'Y'], True), 4)
((['U', 'W', 'B', 'B', 'B', 'U'], False), 6)
((['U', 'W', 'B', 'R', 'W', 'Y'], True), 4)
((['U', 'W', 'R', 'R', 'B', 'R'], True), 3)
((['U', 'W', 'R', 'W', 'Y', 'B'], True), 4)
((['U', 'W', 'W', 'B', 'Y', 'R'], True), 4)
((['U', 'W', 'W', 'W', 'R', 'W'], False), 4)
((['U', 'W', 'W', 'W', 'R', 'Y'], True), 4)
((['U', 'Y', 'B', 'Y', 'R', 'W'], False), 4)
((['U', 'Y', 'U', 'R', 'U', 'Y'], False), 4)
((['U', 'Y', 'U', 'R', 'Y', 'W'], False), 4)
((['U', 'Y', 'R', 'W', 'U', 'U'], False), 4)
((['U', 'Y', 'R', 'Y', 'Y', 'U'], False), 4)
((['U', 'Y', 'Y', 'B', 'W', 'Y'], True), 6)
((['U', 'Y', 'Y', 'R', 'R', 'Y'], True), 4)
((['R', 'B', 'B', 'U', 'U', 'W'], False), 4)
((['R', 'B', 'B', 'Y', 'R', 'U'], False), 4)
((['R', 'B', 'R', 'Y', 'B', 'R'], True), 4)
((['R', 'B', 'W', 'B', 'R', 'B'], False), 4)
((['R', 'B', 'W', 'W', 'U', 'U'], True), 3)
((['R', 'B', 'Y', 'R', 'Y', 'W'], False), 4)
((['R', 'U', 'B', 'B', 'B', 'W'], True), 3)
((['R', 'U', 'B', 'B', 'R', 'W'], False), 4)
((['R', 'U', 'U', 'U', 'R', 'Y'], False), 4)
((['R', 'U', 'U', 'Y', 'U', 'W'], False), 4)
((['R', 'U', 'R', 'W', 'W', 'R'], False), 4)
((['R', 'U', 'R', 'W', 'W', 'W'], False), 4)
((['R', 'U', 'R', 'Y', 'R', 'U'], False), 4)
((['R', 'U', 'W', 'U', 'Y', 'W'], False), 4)
((['R', 'U', 'W', 'W', 'Y', 'Y'], True), 4)
((['R', 'U', 'W', 'Y', 'W', 'Y'], False), 4)
((['R', 'R', 'B', 'W', 'U', 'W'], False), 4)
((['R', 'R', 'B', 'W', 'W', 'U'], True), 3)
((['R', 'R', 'U', 'B', 'B', 'U'], False), 4)
((['R', 'R', 'U', 'W', 'R', 'B'], True), 3)
((['R', 'R', 'U', 'Y', 'Y', 'R'], False), 4)
((['R', 'R', 'W', 'U', 'W', 'W'], True), 3)
((['R', 'R', 'W', 'W', 'B', 'W'], False), 4)
((['R', 'R', 'Y', 'U', 'B', 'W'], False), 4)
((['R', 'R', 'Y', 'Y', 'U', 'Y'], True), 4)
((['R', 'W', 'B', 'Y', 'R', 'B'], True), 4)
((['R', 'W', 'U', 'B', 'U', 'R'], True), 3)
((['R', 'W', 'U', 'Y', 'U', 'Y'], False), 4)
((['R', 'W', 'W', 'U', 'B', 'Y'], True), 4)
((['R', 'W', 'Y', 'B', 'W', 'Y'], False), 4)
((['R', 'W', 'Y', 'U', 'B', 'Y'], False), 4)
((['R', 'W', 'Y', 'W', 'U', 'U'], False), 4)
((['R', 'Y', 'B', 'W', 'W', 'R'], False), 4)
((['R', 'Y', 'U', 'R', 'B', 'W'], False), 4)
((['R', 'Y', 'U', 'Y', 'R', 'U'], False), 4)
((['R', 'Y', 'R', 'R', 'U', 'R'], True), 4)
((['R', 'Y', 'Y', 'B', 'U', 'R'], False), 4)
((['R', 'Y', 'Y', 'B', 'R', 'W'], False), 4)
((['R', 'Y', 'Y', 'B', 'Y', 'R'], True), 4)
((['R', 'Y', 'Y', 'Y', 'Y', 'R'], False), 4)
((['W', 'B', 'B', 'B', 'R', 'U'], True), 3)
((['W', 'B', 'B', 'R', 'Y', 'Y'], False), 4)
((['W', 'B', 'B', 'Y', 'Y', 'R'], False), 4)
((['W', 'B', 'R', 'R', 'U', 'U'], True), 3)
((['W', 'B', 'R', 'W', 'R', 'Y'], False), 4)
((['W', 'B', 'Y', 'U', 'Y', 'Y'], True), 6)
((['W', 'B', 'Y', 'R', 'R', 'U'], False), 4)
((['W', 'U', 'U', 'B', 'R', 'W'], True), 3)
((['W', 'U', 'U', 'R', 'W', 'R'], False), 4)
((['W', 'U', 'R', 'U', 'B', 'W'], True), 3)
((['W', 'U', 'R', 'U', 'U', 'Y'], True), 4)
((['W', 'U', 'R', 'U', 'R', 'W'], True), 3)
((['W', 'U', 'R', 'U', 'R', 'Y'], False), 4)
((['W', 'U', 'R', 'R', 'U', 'R'], False), 4)
((['W', 'U', 'W', 'U', 'U', 'Y'], True), 4)
((['W', 'U', 'W', 'Y', 'B', 'R'], True), 4)
((['W', 'U', 'Y', 'R', 'B', 'W'], True), 4)
((['W', 'R', 'B', 'B', 'U', 'W'], False), 4)
((['W', 'R', 'B', 'B', 'U', 'Y'], True), 4)
((['W', 'R', 'B', 'Y', 'W', 'R'], False), 4)
((['W', 'R', 'U', 'B', 'W', 'B'], True), 3)
((['W', 'R', 'U', 'Y', 'Y', 'Y'], True), 4)
((['W', 'R', 'R', 'B', 'W', 'Y'], False), 4)
((['W', 'R', 'R', 'R', 'U', 'B'], False), 4)
((['W', 'R', 'R', 'W', 'W', 'Y'], True), 4)
((['W', 'R', 'W', 'B', 'B', 'W'], True), 3)
((['W', 'R', 'Y', 'U', 'B', 'B'], True), 4)
((['W', 'R', 'Y', 'R', 'R', 'R'], True), 4)
((['W', 'W', 'B', 'R', 'R', 'Y'], True), 4)
((['W', 'W', 'B', 'Y', 'U', 'U'], True), 4)
((['W', 'W', 'U', 'W', 'R', 'U'], True), 3)
((['W', 'W', 'U', 'W', 'Y', 'B'], True), 4)
((['W', 'W', 'U', 'Y', 'Y', 'B'], True), 6)
((['W', 'W', 'R', 'R', 'R', 'W'], True), 3)
((['W', 'W', 'W', 'U', 'W', 'Y'], False), 4)
((['W', 'Y', 'R', 'B', 'W', 'U'], False), 4)
((['W', 'Y', 'R', 'W', 'U', 'W'], True), 4)
((['W', 'Y', 'R', 'Y', 'R', 'B'], True), 4)
((['W', 'Y', 'W', 'U', 'U', 'B'], True), 4)
((['W', 'Y', 'Y', 'Y', 'R', 'B'], False), 4)
((['Y', 'B', 'B', 'R', 'W', 'R'], False), 4)
((['Y', 'B', 'R', 'R', 'U', 'B'], True), 4)
((['Y', 'B', 'R', 'Y', 'W', 'R'], False), 4)
((['Y', 'B', 'W', 'Y', 'B', 'R'], True), 4)
((['Y', 'B', 'Y', 'W', 'W', 'Y'], True), 6)
((['Y', 'U', 'B', 'U', 'B', 'U'], False), 6)
((['Y', 'U', 'B', 'U', 'U', 'U'], False), 6)
((['Y', 'U', 'B', 'U', 'Y', 'Y'], False), 6)
((['Y', 'U', 'B', 'W', 'R', 'Y'], True), 4)
((['Y', 'U', 'U', 'B', 'R', 'W'], False), 4)
((['Y', 'U', 'R', 'B', 'W', 'U'], False), 4)
((['Y', 'U', 'Y', 'R', 'Y', 'Y'], True), 4)
((['Y', 'R', 'B', 'B', 'U', 'R'], False), 4)
((['Y', 'R', 'B', 'B', 'U', 'W'], True), 4)
((['Y', 'R', 'B', 'B', 'R', 'B'], False), 4)
((['Y', 'R', 'B', 'R', 'B', 'W'], False), 4)
((['Y', 'R', 'U', 'U', 'U', 'R'], False), 4)
((['Y', 'R', 'R', 'U', 'B', 'W'], True), 4)
((['Y', 'R', 'R', 'W', 'B', 'W'], True), 4)
((['Y', 'R', 'R', 'W', 'U', 'W'], False), 4)
((['Y', 'R', 'W', 'B', 'Y', 'B'], True), 4)
((['Y', 'R', 'W', 'Y', 'Y', 'R'], False), 4)
((['Y', 'R', 'Y', 'B', 'Y', 'B'], False), 4)
((['Y', 'W', 'B', 'R', 'W', 'W'], False), 4)
((['Y', 'W', 'U', 'R', 'W', 'W'], False), 4)
((['Y', 'W', 'R', 'B', 'Y', 'U'], False), 4)
((['Y', 'W', 'R', 'U', 'U', 'Y'], False), 4)
((['Y', 'W', 'R', 'R', 'W', 'B'], True), 4)
((['Y', 'W', 'W', 'U', 'Y', 'W'], True), 6)
((['Y', 'W', 'Y', 'U', 'U', 'U'], True), 6)
((['Y', 'W', 'Y', 'R', 'B', 'B'], False), 4)
((['Y', 'Y', 'B', 'B', 'B', 'B'], True), 6)
((['Y', 'Y', 'B', 'B', 'W', 'R'], True), 4)
((['Y', 'Y', 'B', 'R', 'W', 'Y'], False), 4)
((['Y', 'Y', 'B', 'Y', 'Y', 'B'], False), 6)
((['Y', 'Y', 'R', 'B', 'Y', 'W'], False), 4)
((['Y', 'Y', 'R', 'Y', 'U', 'W'], True), 4)

This Python code generates all 39000 possible inputs (TIO).

import itertools

def generate_all_inputs():
    colors = ['B', 'U', 'R', 'W', 'Y']

    for num_wires in [3, 4, 5, 6]:
        for wires in itertools.product(colors, repeat=num_wires):
            for serial_odd in [False, True]:
                yield (list(wires), serial_odd)

Leaderboard

var QUESTION_ID=125665,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/125665/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

xnor

Posted 2017-06-09T18:08:30.210

Reputation: 115 687

5...so when they say "never cut the blue wire", they really should say "never cut the fifth wire" (counts of "wires to cut" by index: 4453, 2359, 4252, 22045, 0, 5891). – Jonathan Allan – 2017-06-09T19:42:06.730

@Notts90 UUR is the only combination that it matters for, right? I'll add it to the test cases. – xnor – 2017-06-09T21:43:02.497

For languages like C, can we take the number of wires in addition to the input of characters? – Conor O'Brien – 2017-06-10T02:21:31.937

@ConorO'Brien Can C and similar take a string or list with a terminator instead? – xnor – 2017-06-10T04:23:36.367

@xnor A string, yes. A list, possibly. Would a string be an acceptable substitute for the unordered list? – Conor O'Brien – 2017-06-10T04:31:02.627

@ConorO'Brien Yes, a string would be fine. I'll say a list can end in a reasonable terminator as well. – xnor – 2017-06-10T04:31:59.440

1Complicated Wires when? :P – CAD97 – 2017-06-10T14:29:46.397

Don't bother posting a question about Big (Red) Buttons. The solution is (almost always) "press and hold, then release when timer is..." I'd have to grab my defusal manual to recheck things. But so much of it boils down to the final "else" statement of "see press and hold." (It got to the point where I was able to defuse that module myself with no instruction). – Draco18s no longer trusts SE – 2017-06-12T20:35:09.760

2I think all solutions could save a few bytes by noticing the second case where there are 3 wires is useless: if that case should trigger, there are at least one red and one white wires, so there can't be more than one blue wire, and the default case will take care of it. – nore – 2017-06-13T04:45:23.460

Answers

10

JavaScript (ES6), 210 203 199 187 180 bytes

Saved 7 bytes thanks to nore.


Takes the list of wires w and the flag o in currying syntax (w)(o).

w=>o=>(([,,C,D,E,F]=w).map(c=>eval(c+`=${++i}+`+c),i=B=U=R=W=Y=''),F?!Y&o?3:7/Y&7&&W>7||R?4:6:E?E<'C'&o?4:7/R&7&&Y>7||B?1:2:D?R>7&o?+R[0]:D>'X'&!R|7/U&7?1:Y>7?4:2:!R?2:U<7?3:+U[0])

How?

For each color, we build a string representing the list of 1-based indices at which this color is found, from last to first position.

For instance, the last test case ['Y', 'Y', 'R', 'Y', 'U', 'W'] will be translated as:

  • B = ''
  • U = '5'
  • R = '3'
  • W = '6'
  • Y = '421'

These variables provide enough information to process all rules in a rather short format, except tests on specific wires which are performed on the destructured input list [, , C, D, E, F].

Rule                              | Code
----------------------------------+---------------------------------------------------
no X wires                        | !X
more than one X wire              | X>7
exactly one X wire                | 7/X&7
position of last X wire (1-based) | +X[0]
(N+1)th wire is X (1-based)       | w[N]=='X' (where w[N] is replaced by C, D, E or F)

Test cases

let f =

w=>o=>(([,,C,D,E,F]=w).map(c=>eval(c+`=${++i}+`+c),i=B=U=R=W=Y=''),F?!Y&o?3:7/Y&7&&W>7||R?4:6:E?E<'C'&o?4:7/R&7&&Y>7||B?1:2:D?R>7&o?+R[0]:D>'X'&!R|7/U&7?1:Y>7?4:2:!R?2:U<7?3:+U[0])

let passed = 0
let test = (a, r) => passed += (f(a[0])(a[1]) === r)

test([['B', 'B', 'B'], false], 2)
test([['B', 'B', 'Y'], true ], 2)
test([['B', 'R', 'R'], false], 3)
test([['B', 'W', 'U'], true ], 2)
test([['U', 'B', 'B'], true ], 2)
test([['U', 'B', 'Y'], false], 2)
test([['U', 'U', 'R'], true ], 2)
test([['U', 'U', 'U'], false], 2)
test([['U', 'R', 'R'], true ], 3)
test([['U', 'Y', 'Y'], false], 2)
test([['R', 'B', 'U'], false], 3)
test([['R', 'B', 'Y'], false], 3)
test([['R', 'U', 'B'], false], 3)
test([['R', 'R', 'U'], false], 3)
test([['R', 'W', 'U'], true ], 3)
test([['W', 'B', 'W'], false], 2)
test([['W', 'B', 'Y'], true ], 2)
test([['W', 'R', 'U'], true ], 3)
test([['W', 'W', 'B'], true ], 2)
test([['W', 'W', 'U'], true ], 2)
test([['W', 'Y', 'W'], true ], 2)
test([['Y', 'U', 'B'], true ], 2)
test([['Y', 'U', 'W'], false], 2)
test([['Y', 'R', 'U'], false], 3)
test([['Y', 'Y', 'B'], false], 2)
test([['Y', 'Y', 'B'], true ], 2)
test([['B', 'B', 'U', 'U'], true ], 2)
test([['B', 'B', 'R', 'W'], true ], 2)
test([['B', 'B', 'R', 'Y'], true ], 2)
test([['B', 'U', 'B', 'R'], false], 1)
test([['B', 'U', 'R', 'W'], false], 1)
test([['B', 'U', 'W', 'R'], true ], 1)
test([['B', 'U', 'W', 'Y'], true ], 1)
test([['B', 'U', 'Y', 'R'], false], 1)
test([['B', 'R', 'U', 'B'], true ], 1)
test([['B', 'R', 'R', 'B'], true ], 3)
test([['B', 'R', 'Y', 'W'], true ], 2)
test([['B', 'R', 'Y', 'Y'], true ], 4)
test([['B', 'W', 'R', 'U'], true ], 1)
test([['B', 'W', 'Y', 'B'], false], 2)
test([['B', 'Y', 'R', 'U'], false], 1)
test([['B', 'Y', 'R', 'R'], false], 2)
test([['U', 'B', 'R', 'W'], false], 1)
test([['U', 'B', 'W', 'Y'], false], 1)
test([['U', 'B', 'Y', 'W'], true ], 1)
test([['U', 'U', 'R', 'W'], true ], 2)
test([['U', 'U', 'W', 'B'], false], 2)
test([['U', 'U', 'W', 'Y'], false], 1)
test([['U', 'R', 'B', 'U'], false], 2)
test([['U', 'R', 'Y', 'U'], true ], 2)
test([['U', 'R', 'Y', 'W'], false], 1)
test([['U', 'R', 'Y', 'Y'], false], 1)
test([['U', 'W', 'U', 'Y'], false], 1)
test([['U', 'W', 'W', 'W'], false], 1)
test([['U', 'Y', 'B', 'B'], false], 1)
test([['U', 'Y', 'B', 'W'], true ], 1)
test([['U', 'Y', 'U', 'R'], true ], 2)
test([['U', 'Y', 'R', 'W'], false], 1)
test([['R', 'B', 'R', 'R'], false], 2)
test([['R', 'U', 'B', 'B'], true ], 1)
test([['R', 'U', 'W', 'B'], false], 1)
test([['R', 'R', 'B', 'R'], true ], 4)
test([['R', 'R', 'W', 'R'], true ], 4)
test([['R', 'R', 'W', 'W'], true ], 2)
test([['R', 'R', 'Y', 'Y'], false], 4)
test([['R', 'R', 'Y', 'Y'], true ], 2)
test([['R', 'W', 'U', 'W'], true ], 1)
test([['R', 'W', 'W', 'U'], false], 1)
test([['R', 'W', 'Y', 'W'], false], 2)
test([['R', 'Y', 'R', 'U'], false], 1)
test([['R', 'Y', 'Y', 'W'], false], 4)
test([['W', 'B', 'U', 'R'], false], 1)
test([['W', 'B', 'U', 'Y'], false], 1)
test([['W', 'U', 'B', 'Y'], false], 1)
test([['W', 'U', 'U', 'W'], true ], 2)
test([['W', 'U', 'R', 'W'], false], 1)
test([['W', 'W', 'R', 'U'], false], 1)
test([['W', 'Y', 'R', 'R'], false], 2)
test([['W', 'Y', 'Y', 'U'], false], 1)
test([['W', 'Y', 'Y', 'Y'], true ], 1)
test([['Y', 'B', 'B', 'R'], true ], 2)
test([['Y', 'B', 'W', 'U'], false], 1)
test([['Y', 'B', 'W', 'W'], false], 2)
test([['Y', 'U', 'R', 'Y'], false], 1)
test([['Y', 'R', 'B', 'R'], false], 2)
test([['Y', 'R', 'U', 'R'], true ], 4)
test([['Y', 'R', 'R', 'Y'], false], 4)
test([['Y', 'R', 'W', 'U'], false], 1)
test([['Y', 'R', 'Y', 'B'], false], 4)
test([['Y', 'R', 'Y', 'B'], true ], 4)
test([['Y', 'W', 'U', 'B'], false], 1)
test([['Y', 'W', 'R', 'R'], true ], 4)
test([['Y', 'W', 'W', 'R'], true ], 2)
test([['Y', 'W', 'W', 'Y'], true ], 1)
test([['Y', 'W', 'Y', 'U'], false], 1)
test([['Y', 'Y', 'B', 'B'], true ], 4)
test([['Y', 'Y', 'R', 'R'], true ], 4)
test([['B', 'B', 'B', 'R', 'W'], false], 1)
test([['B', 'B', 'R', 'R', 'W'], false], 1)
test([['B', 'U', 'B', 'W', 'U'], true ], 1)
test([['B', 'R', 'R', 'U', 'R'], true ], 1)
test([['B', 'R', 'R', 'W', 'W'], false], 1)
test([['B', 'R', 'Y', 'Y', 'R'], false], 1)
test([['B', 'W', 'B', 'W', 'B'], false], 1)
test([['B', 'W', 'U', 'B', 'U'], true ], 1)
test([['B', 'W', 'R', 'U', 'W'], true ], 1)
test([['B', 'W', 'R', 'W', 'B'], false], 1)
test([['B', 'W', 'W', 'R', 'U'], false], 1)
test([['B', 'W', 'W', 'R', 'U'], true ], 1)
test([['B', 'W', 'W', 'W', 'B'], false], 1)
test([['B', 'W', 'Y', 'R', 'Y'], true ], 1)
test([['B', 'Y', 'B', 'W', 'U'], true ], 1)
test([['B', 'Y', 'U', 'W', 'B'], true ], 4)
test([['B', 'Y', 'U', 'Y', 'W'], false], 1)
test([['U', 'B', 'R', 'W', 'Y'], false], 1)
test([['U', 'B', 'W', 'B', 'R'], false], 1)
test([['U', 'B', 'W', 'B', 'W'], false], 1)
test([['U', 'B', 'W', 'Y', 'R'], false], 1)
test([['U', 'B', 'Y', 'U', 'B'], true ], 4)
test([['U', 'B', 'Y', 'U', 'Y'], false], 1)
test([['U', 'B', 'Y', 'R', 'W'], false], 1)
test([['U', 'U', 'B', 'B', 'U'], true ], 1)
test([['U', 'U', 'R', 'U', 'W'], true ], 2)
test([['U', 'U', 'Y', 'U', 'R'], true ], 2)
test([['U', 'U', 'Y', 'U', 'W'], false], 2)
test([['U', 'R', 'B', 'Y', 'Y'], false], 1)
test([['U', 'R', 'U', 'B', 'Y'], false], 1)
test([['U', 'R', 'W', 'W', 'B'], false], 1)
test([['U', 'R', 'Y', 'Y', 'W'], false], 1)
test([['U', 'W', 'B', 'U', 'B'], true ], 4)
test([['U', 'W', 'U', 'U', 'B'], true ], 4)
test([['U', 'W', 'R', 'U', 'Y'], true ], 2)
test([['U', 'W', 'R', 'R', 'R'], true ], 2)
test([['U', 'W', 'R', 'R', 'W'], false], 2)
test([['U', 'W', 'R', 'Y', 'W'], true ], 2)
test([['U', 'W', 'W', 'Y', 'R'], true ], 2)
test([['U', 'Y', 'B', 'W', 'Y'], false], 1)
test([['U', 'Y', 'U', 'R', 'W'], true ], 2)
test([['U', 'Y', 'R', 'R', 'U'], false], 2)
test([['U', 'Y', 'Y', 'B', 'W'], false], 1)
test([['U', 'Y', 'Y', 'R', 'B'], true ], 4)
test([['U', 'Y', 'Y', 'Y', 'R'], false], 1)
test([['R', 'B', 'B', 'W', 'U'], false], 1)
test([['R', 'B', 'U', 'B', 'Y'], false], 1)
test([['R', 'B', 'R', 'R', 'Y'], true ], 1)
test([['R', 'B', 'W', 'W', 'R'], true ], 1)
test([['R', 'B', 'W', 'W', 'W'], false], 1)
test([['R', 'U', 'U', 'B', 'U'], true ], 1)
test([['R', 'U', 'U', 'R', 'Y'], false], 2)
test([['R', 'U', 'R', 'B', 'W'], false], 1)
test([['R', 'U', 'R', 'Y', 'R'], true ], 2)
test([['R', 'R', 'B', 'U', 'U'], true ], 1)
test([['R', 'R', 'B', 'R', 'W'], true ], 1)
test([['R', 'R', 'W', 'B', 'Y'], true ], 1)
test([['R', 'R', 'Y', 'Y', 'B'], false], 1)
test([['R', 'W', 'U', 'Y', 'W'], false], 2)
test([['R', 'W', 'Y', 'B', 'U'], true ], 1)
test([['R', 'Y', 'B', 'U', 'U'], true ], 1)
test([['R', 'Y', 'B', 'R', 'Y'], true ], 1)
test([['R', 'Y', 'B', 'W', 'R'], true ], 1)
test([['R', 'Y', 'R', 'U', 'U'], false], 2)
test([['R', 'Y', 'Y', 'W', 'B'], true ], 4)
test([['R', 'Y', 'Y', 'W', 'W'], true ], 1)
test([['W', 'B', 'R', 'R', 'R'], false], 1)
test([['W', 'U', 'U', 'U', 'B'], false], 1)
test([['W', 'U', 'U', 'R', 'B'], false], 1)
test([['W', 'U', 'R', 'B', 'R'], false], 1)
test([['W', 'U', 'W', 'W', 'R'], true ], 2)
test([['W', 'U', 'Y', 'R', 'W'], true ], 2)
test([['W', 'R', 'R', 'B', 'Y'], true ], 1)
test([['W', 'W', 'U', 'B', 'W'], true ], 1)
test([['W', 'W', 'U', 'W', 'R'], false], 2)
test([['W', 'W', 'W', 'W', 'B'], false], 1)
test([['W', 'W', 'W', 'W', 'W'], false], 2)
test([['W', 'W', 'Y', 'W', 'U'], true ], 2)
test([['W', 'W', 'Y', 'Y', 'R'], false], 1)
test([['W', 'Y', 'R', 'B', 'B'], false], 1)
test([['W', 'Y', 'W', 'B', 'W'], true ], 1)
test([['W', 'Y', 'Y', 'W', 'U'], true ], 2)
test([['Y', 'B', 'U', 'R', 'B'], true ], 4)
test([['Y', 'B', 'U', 'Y', 'R'], false], 1)
test([['Y', 'B', 'R', 'Y', 'Y'], false], 1)
test([['Y', 'B', 'W', 'U', 'B'], true ], 4)
test([['Y', 'B', 'Y', 'R', 'R'], false], 1)
test([['Y', 'U', 'U', 'U', 'U'], false], 2)
test([['Y', 'U', 'R', 'W', 'B'], false], 1)
test([['Y', 'U', 'W', 'U', 'Y'], true ], 2)
test([['Y', 'U', 'Y', 'Y', 'W'], false], 2)
test([['Y', 'R', 'R', 'R', 'Y'], false], 2)
test([['Y', 'R', 'R', 'Y', 'R'], false], 2)
test([['Y', 'R', 'W', 'W', 'U'], false], 2)
test([['Y', 'W', 'B', 'R', 'U'], true ], 1)
test([['Y', 'W', 'U', 'U', 'W'], true ], 2)
test([['Y', 'W', 'U', 'R', 'B'], false], 1)
test([['Y', 'W', 'R', 'R', 'R'], true ], 2)
test([['Y', 'W', 'R', 'Y', 'R'], false], 2)
test([['Y', 'W', 'W', 'B', 'U'], true ], 1)
test([['Y', 'W', 'W', 'W', 'B'], false], 1)
test([['Y', 'Y', 'R', 'Y', 'U'], false], 1)
test([['B', 'B', 'B', 'B', 'R', 'U'], false], 4)
test([['B', 'B', 'B', 'R', 'R', 'R'], true ], 3)
test([['B', 'B', 'R', 'U', 'W', 'Y'], false], 4)
test([['B', 'B', 'R', 'R', 'R', 'B'], true ], 3)
test([['B', 'B', 'W', 'U', 'B', 'B'], false], 6)
test([['B', 'B', 'W', 'U', 'B', 'U'], true ], 3)
test([['B', 'B', 'W', 'W', 'B', 'R'], true ], 3)
test([['B', 'B', 'Y', 'Y', 'W', 'R'], false], 4)
test([['B', 'U', 'B', 'B', 'W', 'U'], false], 6)
test([['B', 'U', 'U', 'W', 'W', 'Y'], true ], 4)
test([['B', 'U', 'U', 'Y', 'Y', 'R'], false], 4)
test([['B', 'U', 'R', 'R', 'B', 'Y'], true ], 4)
test([['B', 'U', 'W', 'B', 'W', 'Y'], true ], 4)
test([['B', 'U', 'Y', 'R', 'R', 'R'], false], 4)
test([['B', 'U', 'Y', 'R', 'Y', 'B'], false], 4)
test([['B', 'R', 'U', 'B', 'U', 'B'], true ], 3)
test([['B', 'R', 'R', 'R', 'Y', 'B'], true ], 4)
test([['B', 'R', 'R', 'W', 'B', 'R'], true ], 3)
test([['B', 'R', 'Y', 'B', 'R', 'W'], false], 4)
test([['B', 'R', 'Y', 'W', 'B', 'Y'], false], 4)
test([['B', 'W', 'U', 'Y', 'U', 'W'], false], 4)
test([['B', 'W', 'R', 'U', 'Y', 'Y'], true ], 4)
test([['B', 'W', 'R', 'Y', 'U', 'W'], false], 4)
test([['B', 'W', 'W', 'Y', 'U', 'R'], false], 4)
test([['B', 'W', 'Y', 'R', 'B', 'R'], false], 4)
test([['B', 'W', 'Y', 'W', 'Y', 'U'], false], 6)
test([['B', 'Y', 'B', 'R', 'B', 'R'], true ], 4)
test([['B', 'Y', 'U', 'B', 'Y', 'U'], false], 6)
test([['B', 'Y', 'R', 'U', 'Y', 'U'], true ], 4)
test([['B', 'Y', 'R', 'R', 'W', 'W'], true ], 4)
test([['B', 'Y', 'W', 'W', 'U', 'B'], true ], 4)
test([['U', 'B', 'B', 'W', 'R', 'R'], true ], 3)
test([['U', 'B', 'W', 'B', 'W', 'U'], false], 6)
test([['U', 'B', 'Y', 'U', 'B', 'R'], false], 4)
test([['U', 'U', 'B', 'B', 'W', 'Y'], false], 6)
test([['U', 'U', 'B', 'W', 'B', 'B'], true ], 3)
test([['U', 'U', 'B', 'Y', 'Y', 'Y'], false], 6)
test([['U', 'U', 'U', 'B', 'U', 'Y'], true ], 6)
test([['U', 'U', 'U', 'B', 'Y', 'Y'], false], 6)
test([['U', 'U', 'U', 'Y', 'W', 'B'], false], 6)
test([['U', 'U', 'R', 'U', 'W', 'R'], true ], 3)
test([['U', 'U', 'Y', 'W', 'W', 'U'], true ], 4)
test([['U', 'U', 'Y', 'Y', 'B', 'R'], false], 4)
test([['U', 'R', 'B', 'R', 'Y', 'R'], false], 4)
test([['U', 'R', 'B', 'R', 'Y', 'Y'], true ], 4)
test([['U', 'R', 'R', 'B', 'U', 'R'], false], 4)
test([['U', 'R', 'W', 'B', 'B', 'B'], false], 4)
test([['U', 'R', 'W', 'Y', 'U', 'U'], true ], 4)
test([['U', 'R', 'Y', 'U', 'B', 'Y'], true ], 4)
test([['U', 'W', 'B', 'B', 'B', 'U'], false], 6)
test([['U', 'W', 'B', 'R', 'W', 'Y'], true ], 4)
test([['U', 'W', 'R', 'R', 'B', 'R'], true ], 3)
test([['U', 'W', 'R', 'W', 'Y', 'B'], true ], 4)
test([['U', 'W', 'W', 'B', 'Y', 'R'], true ], 4)
test([['U', 'W', 'W', 'W', 'R', 'W'], false], 4)
test([['U', 'W', 'W', 'W', 'R', 'Y'], true ], 4)
test([['U', 'Y', 'B', 'Y', 'R', 'W'], false], 4)
test([['U', 'Y', 'U', 'R', 'U', 'Y'], false], 4)
test([['U', 'Y', 'U', 'R', 'Y', 'W'], false], 4)
test([['U', 'Y', 'R', 'W', 'U', 'U'], false], 4)
test([['U', 'Y', 'R', 'Y', 'Y', 'U'], false], 4)
test([['U', 'Y', 'Y', 'B', 'W', 'Y'], true ], 6)
test([['U', 'Y', 'Y', 'R', 'R', 'Y'], true ], 4)
test([['R', 'B', 'B', 'U', 'U', 'W'], false], 4)
test([['R', 'B', 'B', 'Y', 'R', 'U'], false], 4)
test([['R', 'B', 'R', 'Y', 'B', 'R'], true ], 4)
test([['R', 'B', 'W', 'B', 'R', 'B'], false], 4)
test([['R', 'B', 'W', 'W', 'U', 'U'], true ], 3)
test([['R', 'B', 'Y', 'R', 'Y', 'W'], false], 4)
test([['R', 'U', 'B', 'B', 'B', 'W'], true ], 3)
test([['R', 'U', 'B', 'B', 'R', 'W'], false], 4)
test([['R', 'U', 'U', 'U', 'R', 'Y'], false], 4)
test([['R', 'U', 'U', 'Y', 'U', 'W'], false], 4)
test([['R', 'U', 'R', 'W', 'W', 'R'], false], 4)
test([['R', 'U', 'R', 'W', 'W', 'W'], false], 4)
test([['R', 'U', 'R', 'Y', 'R', 'U'], false], 4)
test([['R', 'U', 'W', 'U', 'Y', 'W'], false], 4)
test([['R', 'U', 'W', 'W', 'Y', 'Y'], true ], 4)
test([['R', 'U', 'W', 'Y', 'W', 'Y'], false], 4)
test([['R', 'R', 'B', 'W', 'U', 'W'], false], 4)
test([['R', 'R', 'B', 'W', 'W', 'U'], true ], 3)
test([['R', 'R', 'U', 'B', 'B', 'U'], false], 4)
test([['R', 'R', 'U', 'W', 'R', 'B'], true ], 3)
test([['R', 'R', 'U', 'Y', 'Y', 'R'], false], 4)
test([['R', 'R', 'W', 'U', 'W', 'W'], true ], 3)
test([['R', 'R', 'W', 'W', 'B', 'W'], false], 4)
test([['R', 'R', 'Y', 'U', 'B', 'W'], false], 4)
test([['R', 'R', 'Y', 'Y', 'U', 'Y'], true ], 4)
test([['R', 'W', 'B', 'Y', 'R', 'B'], true ], 4)
test([['R', 'W', 'U', 'B', 'U', 'R'], true ], 3)
test([['R', 'W', 'U', 'Y', 'U', 'Y'], false], 4)
test([['R', 'W', 'W', 'U', 'B', 'Y'], true ], 4)
test([['R', 'W', 'Y', 'B', 'W', 'Y'], false], 4)
test([['R', 'W', 'Y', 'U', 'B', 'Y'], false], 4)
test([['R', 'W', 'Y', 'W', 'U', 'U'], false], 4)
test([['R', 'Y', 'B', 'W', 'W', 'R'], false], 4)
test([['R', 'Y', 'U', 'R', 'B', 'W'], false], 4)
test([['R', 'Y', 'U', 'Y', 'R', 'U'], false], 4)
test([['R', 'Y', 'R', 'R', 'U', 'R'], true ], 4)
test([['R', 'Y', 'Y', 'B', 'U', 'R'], false], 4)
test([['R', 'Y', 'Y', 'B', 'R', 'W'], false], 4)
test([['R', 'Y', 'Y', 'B', 'Y', 'R'], true ], 4)
test([['R', 'Y', 'Y', 'Y', 'Y', 'R'], false], 4)
test([['W', 'B', 'B', 'B', 'R', 'U'], true ], 3)
test([['W', 'B', 'B', 'R', 'Y', 'Y'], false], 4)
test([['W', 'B', 'B', 'Y', 'Y', 'R'], false], 4)
test([['W', 'B', 'R', 'R', 'U', 'U'], true ], 3)
test([['W', 'B', 'R', 'W', 'R', 'Y'], false], 4)
test([['W', 'B', 'Y', 'U', 'Y', 'Y'], true ], 6)
test([['W', 'B', 'Y', 'R', 'R', 'U'], false], 4)
test([['W', 'U', 'U', 'B', 'R', 'W'], true ], 3)
test([['W', 'U', 'U', 'R', 'W', 'R'], false], 4)
test([['W', 'U', 'R', 'U', 'B', 'W'], true ], 3)
test([['W', 'U', 'R', 'U', 'U', 'Y'], true ], 4)
test([['W', 'U', 'R', 'U', 'R', 'W'], true ], 3)
test([['W', 'U', 'R', 'U', 'R', 'Y'], false], 4)
test([['W', 'U', 'R', 'R', 'U', 'R'], false], 4)
test([['W', 'U', 'W', 'U', 'U', 'Y'], true ], 4)
test([['W', 'U', 'W', 'Y', 'B', 'R'], true ], 4)
test([['W', 'U', 'Y', 'R', 'B', 'W'], true ], 4)
test([['W', 'R', 'B', 'B', 'U', 'W'], false], 4)
test([['W', 'R', 'B', 'B', 'U', 'Y'], true ], 4)
test([['W', 'R', 'B', 'Y', 'W', 'R'], false], 4)
test([['W', 'R', 'U', 'B', 'W', 'B'], true ], 3)
test([['W', 'R', 'U', 'Y', 'Y', 'Y'], true ], 4)
test([['W', 'R', 'R', 'B', 'W', 'Y'], false], 4)
test([['W', 'R', 'R', 'R', 'U', 'B'], false], 4)
test([['W', 'R', 'R', 'W', 'W', 'Y'], true ], 4)
test([['W', 'R', 'W', 'B', 'B', 'W'], true ], 3)
test([['W', 'R', 'Y', 'U', 'B', 'B'], true ], 4)
test([['W', 'R', 'Y', 'R', 'R', 'R'], true ], 4)
test([['W', 'W', 'B', 'R', 'R', 'Y'], true ], 4)
test([['W', 'W', 'B', 'Y', 'U', 'U'], true ], 4)
test([['W', 'W', 'U', 'W', 'R', 'U'], true ], 3)
test([['W', 'W', 'U', 'W', 'Y', 'B'], true ], 4)
test([['W', 'W', 'U', 'Y', 'Y', 'B'], true ], 6)
test([['W', 'W', 'R', 'R', 'R', 'W'], true ], 3)
test([['W', 'W', 'W', 'U', 'W', 'Y'], false], 4)
test([['W', 'Y', 'R', 'B', 'W', 'U'], false], 4)
test([['W', 'Y', 'R', 'W', 'U', 'W'], true ], 4)
test([['W', 'Y', 'R', 'Y', 'R', 'B'], true ], 4)
test([['W', 'Y', 'W', 'U', 'U', 'B'], true ], 4)
test([['W', 'Y', 'Y', 'Y', 'R', 'B'], false], 4)
test([['Y', 'B', 'B', 'R', 'W', 'R'], false], 4)
test([['Y', 'B', 'R', 'R', 'U', 'B'], true ], 4)
test([['Y', 'B', 'R', 'Y', 'W', 'R'], false], 4)
test([['Y', 'B', 'W', 'Y', 'B', 'R'], true ], 4)
test([['Y', 'B', 'Y', 'W', 'W', 'Y'], true ], 6)
test([['Y', 'U', 'B', 'U', 'B', 'U'], false], 6)
test([['Y', 'U', 'B', 'U', 'U', 'U'], false], 6)
test([['Y', 'U', 'B', 'U', 'Y', 'Y'], false], 6)
test([['Y', 'U', 'B', 'W', 'R', 'Y'], true ], 4)
test([['Y', 'U', 'U', 'B', 'R', 'W'], false], 4)
test([['Y', 'U', 'R', 'B', 'W', 'U'], false], 4)
test([['Y', 'U', 'Y', 'R', 'Y', 'Y'], true ], 4)
test([['Y', 'R', 'B', 'B', 'U', 'R'], false], 4)
test([['Y', 'R', 'B', 'B', 'U', 'W'], true ], 4)
test([['Y', 'R', 'B', 'B', 'R', 'B'], false], 4)
test([['Y', 'R', 'B', 'R', 'B', 'W'], false], 4)
test([['Y', 'R', 'U', 'U', 'U', 'R'], false], 4)
test([['Y', 'R', 'R', 'U', 'B', 'W'], true ], 4)
test([['Y', 'R', 'R', 'W', 'B', 'W'], true ], 4)
test([['Y', 'R', 'R', 'W', 'U', 'W'], false], 4)
test([['Y', 'R', 'W', 'B', 'Y', 'B'], true ], 4)
test([['Y', 'R', 'W', 'Y', 'Y', 'R'], false], 4)
test([['Y', 'R', 'Y', 'B', 'Y', 'B'], false], 4)
test([['Y', 'W', 'B', 'R', 'W', 'W'], false], 4)
test([['Y', 'W', 'U', 'R', 'W', 'W'], false], 4)
test([['Y', 'W', 'R', 'B', 'Y', 'U'], false], 4)
test([['Y', 'W', 'R', 'U', 'U', 'Y'], false], 4)
test([['Y', 'W', 'R', 'R', 'W', 'B'], true ], 4)
test([['Y', 'W', 'W', 'U', 'Y', 'W'], true ], 6)
test([['Y', 'W', 'Y', 'U', 'U', 'U'], true ], 6)
test([['Y', 'W', 'Y', 'R', 'B', 'B'], false], 4)
test([['Y', 'Y', 'B', 'B', 'B', 'B'], true ], 6)
test([['Y', 'Y', 'B', 'B', 'W', 'R'], true ], 4)
test([['Y', 'Y', 'B', 'R', 'W', 'Y'], false], 4)
test([['Y', 'Y', 'B', 'Y', 'Y', 'B'], false], 6)
test([['Y', 'Y', 'R', 'B', 'Y', 'W'], false], 4)
test([['Y', 'Y', 'R', 'Y', 'U', 'W'], true ], 4)

console.log('Passed: ' + passed + '/379')

All possible inputs

This should take one or two seconds to complete, mostly because eval() is slow.

const color = ['B', 'U', 'R', 'W', 'Y'];

let all = n => [...Array(5 ** n).keys()].map(c => [...Array(n).keys()].map(i => color[(c / 5 ** i | 0) % 5]))

let wire_to_cut = (wires, serial_odd) => {
    let num_wires = wires.length,
        last_wire = wires[num_wires - 1],
        wires_count = c => wires.reduce((s, x) => s += x == c, s = 0);

    if(num_wires == 3) {
        if(wires_count('R') == 0)
            return 2;
        else if(last_wire == 'W')
            return num_wires;
        else if(wires_count('U') > 1)
            // Last blue wire
            return wires.lastIndexOf('U') + 1;
        else
            return num_wires;
    }
    else if(num_wires == 4) {
        if(wires_count('R') > 1 && serial_odd)
            // Last red wire
            return wires.lastIndexOf('R') + 1;
        else if(last_wire == 'Y' && wires_count('R') == 0)
            return 1;
        else if(wires_count('U') == 1)
            return 1;
        else if(wires_count('Y') > 1)
            return num_wires;
        else
            return 2;
    }
    else if(num_wires == 5) {
        if(last_wire == 'B' && serial_odd)
            return 4;
        else if(wires_count('R') == 1 && wires_count('Y') > 1)
            return 1;
        else if(wires_count('B') == 0)
            return 2;
        else
            return 1;
    }
    else if(num_wires == 6) {
        if(wires_count('Y') == 0 && serial_odd)
            return 3;
        else if(wires_count('Y') == 1 && wires_count('W') > 1)
            return 4;
        else if(wires_count('R') == 0)
            return num_wires;
        else
            return 4;
    }
}

let f = w=>o=>(([,,C,D,E,F]=w).map(c=>eval(c+`=${++i}+`+c),i=B=U=R=W=Y=''),F?!Y&o?3:7/Y&7&&W>7||R?4:6:E?E<'C'&o?4:7/R&7&&Y>7||B?1:2:D?R>7&o?+R[0]:D>'X'&!R|7/U&7?1:Y>7?4:2:!R?2:U<7?3:+U[0])

for(let N = 3; N <= 6; N++) {
  let total = 0, passed = 0;
  
  all(N).forEach(l => {
    passed += f(l)(false) === wire_to_cut(l, false);
    passed += f(l)(true)  === wire_to_cut(l, true);
    total += 2;
  })
  console.log(N + ' wires: ' + passed + ' / ' + total);
}

Arnauld

Posted 2017-06-09T18:08:30.210

Reputation: 111 334

That currying syntax is quite a nice byte-saver. – Charlie – 2017-06-16T16:46:30.073

Would it be possible to test this on the full set of 39000 inputs generated by my TIO? – xnor – 2017-06-19T05:26:50.820

@xnor I've added an exhaustive test against a port of your Python code. – Arnauld – 2017-06-19T13:10:00.417

Congratulations, I awarded you the bounty for a well-optimized solution, with a clever efficiently queryable representation of the wires. It was a close decision between this and Jonathan Allan's Python solution.

– xnor – 2017-06-19T20:12:04.263

5

Python 2,  300  214 bytes

-12 bytes thanks to a comment made by nore (for length 3 the last wire white test is redundant!)

def f(w,o):e=w[-1];r,u,y,x=map(w.count,'RUYW');g=r>0;return[2+(u<2or'R'<e)*g,[1+(e<'Y'or g)*(1+2*(y>1))*(u!=1),`w`.rfind('R')/5+1][(r>1)*o],[2-(y>r==1or'B'in w),4][(e<'C')*o],3*o*(y<1)or[6-2*g,4][x>y==1]][len(w)-3]

Try it online! (Tries to assert all the tests and prints a message upon completion or error.)

There may be some nice way to use bit-twiddling and get a shorter solution.

Definitely harder to understand than the manual!

Jonathan Allan

Posted 2017-06-09T18:08:30.210

Reputation: 67 804

It looks like some of the length-3 results are wrong on the full set of possible inputs. – xnor – 2017-06-19T05:23:37.633

That fixes it for 7 with u<2 -> u<2or'R'<e (the four failure cases were when there were 2 blue wires and a red wire and the last was blue, when it would return 2 rather than 3). – Jonathan Allan – 2017-06-19T07:07:33.020

4

Haskell, 315 301 295 284 277 bytes

Thanks to nore for saving 7 bytes.

c o s|l<4=z|l<5=x|l<6=q|l<7=p where b:u:r:w:y:_="BURWY";t=last s;l=length s;m=n r;k=n y;f c=[i|(x,i)<-zip s[1..],x==c];n=length.f;z|m<1=2|n u>1,t/=u=2|1>0=3;x|o,m>1=last(f r)|t==y,m<1=1|n u==1=1|k>1=4|1>0=2;q|o,t==b=4|m==1,k>1=1|n b<1=2|1>0=1;p|o,k<1=3|k==1,n w>1=4|m<1=6|1>0=4

Try it online!

A totally uncreative solution in Haskell.

Ungolfed

c o s
    | l<4=z -- different function for each length; no funny business
    | l<5=x
    | l<6=q
    | l<7=p 
    where 
        b:u:r:w:y:_="BURWY" -- saving space on the quotes
        t=last s -- last is often used
        l=length s
        m=n r
        k=n y -- caching the number of reds and yellows
        f c=[i|(x,i)<-zip s[1..],x==c] -- find 1-indexed indices of the character c in the string s
        n=length.f
        z
            | m<1=2
            | n u>1,t/=u=2
            | 1>0=3
        x
            | o, m>1=last(f r)
            | t==y,m<1=1
            | n u==1=1
            | k>1=4
            | 1>0=2
        q
            | o, t==b=4
            | m==1,k>1=1
            | n b<1=2
            | 1>0=1
        p
            | o, k<1=3
            | k==1,n w>1=4
            | m<1=6
            | 1>0=4

vroomfondel

Posted 2017-06-09T18:08:30.210

Reputation: 2 094

4

JavaScript (ES6), 237 222 bytes

(a,o,[b,u,r,w,y]=[..."BURWY"].map(c=>a.filter(x=>x==c).length),q="lastIndexOf")=>[!r?2:a[2]=="W"|u<2?3:a[q]("U")+1,r>1&o?a[q]("R")+1:a[3]>"X"&!r|u==1?1:y>1?4:2,a[4]<"C"&o?4:r==1&y>1|b?1:2,!y&o?3:y==1&w>1|r?4:6][a.length-3]

Test Snippet

f=
(a,o,[b,u,r,w,y]=[..."BURWY"].map(c=>a.filter(x=>x==c).length),q="lastIndexOf")=>[!r?2:a[2]=="W"|u<2?3:a[q]("U")+1,r>1&o?a[q]("R")+1:a[3]>"X"&!r|u==1?1:y>1?4:2,a[4]<"C"&o?4:r==1&y>1|b?1:2,!y&o?3:y==1&w>1|r?4:6][a.length-3]
<div oninput="O.value=I.value.length>2?f([...I.value],C.checked):''">Wires: <input id="I"> Odd serial? <input type="checkbox" id="C"></div>
Result: <input id="O" disabled size="2">

Tests

Tests from challenge are emulated here.

Justin Mariner

Posted 2017-06-09T18:08:30.210

Reputation: 4 746

4

C (gcc), 264 237 233 bytes

s,i,r,u,y,b,w,z,x,t,q,p;f(S,o)char*S;{for(;s=S[i++];b+=t=s==66,r+=s==82&&(p=i),u+=s==85&&(q=i),y+=x=s==89,w+=z=s==87);S=i==4?r?z?3:u>1?q:3:2:i==5?r*o>1?p:x*!r||u==1?1:y<2?2:4:i==6?t*o?4:r==1&y>1||b?1:2:i==7?!y*o?3:y==1&w>1||r?4:6:0;}

Try it online!

Version without the globals and non-portable return trick, 264 bytes:

f(S,o)char*S;{int s,i,r,u,y,b,w,z,x,t,q,p=q=t=x=z=w=b=y=u=r=i=0;for(;s=S[i++];b+=t=s==66,r+=s==82&&(p=i),u+=s==85&&(q=i),y+=x=s==89,w+=z=s==87);return i==4?r?z?3:u>1?q:3:2:i==5?r*o>1?p:x*!r||u==1?1:y<2?2:4:i==6?t*o?4:r==1&y>1||b?1:2:i==7?!y*o?3:y==1&w>1||r?4:6:0;}

Variable reference (unless I made a mistake):

  • r: # of red wires
  • u: # of blue wires
  • y: # of yellow wires
  • b: # of black wires
  • w: # of white wires
  • z: last wire is white (0 or 1)
  • x: last wire is yellow (0 or 1)
  • t: last wire is black (0 or 1)
  • q: index of last blue wire (1-indexed)
  • p: index of last red wire (1-indexed)

dbandstra

Posted 2017-06-09T18:08:30.210

Reputation: 181

You can save 5 bytes by replacing return i==4... with S=i==4.... Try it online!

– Conor O'Brien – 2017-06-10T16:24:28.393

@ConorO'Brien That requires the less general language of C (gcc) instead. That won't work at all in, e.g. clang.

– FryAmTheEggman – 2017-06-10T16:58:38.210

1

@FryAmTheEggman Not only gcc, it works in tcc as well. Most compilers do it that way, I think that clang is just a weird compiler. But yeah, its not standard.

– Conor O'Brien – 2017-06-10T17:00:34.487

3

Python 2, 193 bytes

def f(s,o):b,u,w,r,y=map(s.count,'BUWRY');l=s[-1];return+[2+(r>0<s!='UUR'),[r<1<l>'X'or u==1or y/2*2+2,1+s.rfind('R')][o*r>1],[r==1<y or-~0**b,4][l<'C'*o],[4+2*(1>r+(y==1<w)),3][y<o]][len(s)-3]

Try it online!

Thanks to nore for pointing out that the second three-wire condition doesn't matter.

3 wires: 2+(r>0<s!='UUR')
4 wires: [r<1<l>'X'or u==1or y/2*2+2,1+s.rfind('R')][o*r>1]
5 wires: [r==1<y or-~0**b,4][l<'C'*o]
6 wires: [4+2*(1>r+(y==1<w)),3][y<o]

Many techniques are mixed together to do the condition logic:

xnor

Posted 2017-06-09T18:08:30.210

Reputation: 115 687

2

Retina, 297 bytes

^[^R]{3}\d
2
^..W\d
3
^BB.\d
2
^...\d
3
^(?=(.*R){2,})(?=.{4}\d)(.)+(?<2>R).*1
$#2
^[^R]{3}Y\d
1
^(?=.{4}\d)[^U]*U[^U]*\d
1
^(?=.{4}\d)(.*Y){2,}.*\d
4
^.{4}\d
2
^.{4}B1
4
^(?=.{5}\d)(?=[^R]*R[^R]*\d)(.*Y){2,}.*\d
1
^[^B]{5}\d
2
^.{5}\d
1
^[^Y]+1
3
^(?=[^Y]*Y[^Y]*\d)(.*W){2,}.*.
4
^[^R]+.
6
^.+.
4

Try it online!

A horrid mess! Takes input with the wires listed with no delimiter followed by a 1 for odd and a zero for even. I verified that it matches all the test cases given. Currently each stage (pair of lines) pretty much just encodes one rule. The main save is in the third stage where the cases where the last blue wire is the third wire can be saved by the "else" condition.

FryAmTheEggman

Posted 2017-06-09T18:08:30.210

Reputation: 16 206

Sorry, your 3-blue-wire bomb just blew up because you cut the middle wire instead of the right-hand wire. – Neil – 2017-06-09T19:19:53.927

@Neil A 3 blue wire bomb has no red wires, so the middle wire is correct! Unless I read something wrong? – FryAmTheEggman – 2017-06-09T19:20:30.427

I had only just come to that conclusion myself. Sorry for the false alarm! – Neil – 2017-06-09T19:22:27.540

1

Python 2, 486 474 464, 421 bytes

Started off as the reference example (1953 bytes originally!) Is now about as unreadable as python could possibly be.

-10 bytes by pre-joining array to string
-43 bytes thanks to ovs for helping get it all on one line.

def f(w,o):n=len(w);l=w[-1];s=''.join(w);return[2if not'R'in w else n if l=='W'else s.rindex('U')+1if'UU'in s else n,s.rindex('R')+1if w.count('R')>1and o else 1if l=='Y'and not'R'in w else 1if w.count('U')==1else n if w.count('Y')>1else 2,4if l=='B'and o else 1if w.count('R')==1and w.count('Y')>1else 2if not'B'in w else 1,3if not'Y'in w and o else 4if w.count('Y')==1and w.count('W')>1else n if not'R'in w else 4][n-3]

Try it online!

Notts90 supports Monica

Posted 2017-06-09T18:08:30.210

Reputation: 1 211

You can take off another 20 to 444 by bringing the cases into a dict and evaling them. TIO: https://goo.gl/pvSjoi

– vroomfondel – 2017-06-10T02:26:03.423

2Why is this noncompeting? – orlp – 2017-06-10T03:37:43.587

@orlp when I started all I'd done was remove white space and rearrange if statements from the example, I didn't feel at that point there was anything special or competitive about it but hoped to be able to do more. Now I have done the extra bits I guess it could be competing. – Notts90 supports Monica – 2017-06-10T05:50:42.997

1

R, 407 Bytes

Golfed Code

pryr::f(a,n,{r=a=='R';u=a=='U';l=paste(length(a));s=sum;R=s(r);Y=s(a=='Y');v=4;switch(l,'3'={v=3;if(R<1){v=2}else if(s(u)>1){v=tail(which(u),1)}else{}},'4'={if(R>1&&n){v=tail(which(r),1)}else if(a[4]=='Y'&&R<1||s(u)==1){v=1}else if(Y>1){}else{v=2}},'5'={v=1;if(a[5]=='B'&&n){v=4}else if(R==1&&Y>1){}else if((a=='B')<1){v=2}else{}},'6'={if(Y<1*1){v=3}else if(Y==1&&s(a=='W')<2){}else if(R<1){v=6}else{}});v})

Ungolfed

function(a,n){
   r=a=='R'
   u=a=='U'
   l=paste(length(a))
   s=sum
   R=s(r)
   Y=s(a=='Y')
   v=4
   switch(l,'3'={
   v=3
   if(R<1){
     v=2
   }else if(s(u)>1){
     v=tail(which(u),1)
   }else{}
   },'4'={
   if((R>1)*n){
      v=tail(which(r),1)
   }else if((a[4]=='Y')*(R<1)||s(u)==1){
      v=1
   }else if(Y>1){
   }else{
     v=2
   }
   },'5'={
     v=1
   if((a[5]=='B')*n){
     v=4
   }else if(R==1&&Y>1){
   }else if((a=='B')<1){
     v=2
   }else{}
   },{
   if((Y<1)*n){
     v=3
   }else if((Y==1)*(s(a=='W')<2)){
   }else if(R<1){
     v=6
   }else{}
 })
 v}

Explanation

This assumes that the input will be an ordered list.

Saved bytes by storing the logical array of where the Red and Blue wires were. The logical array can be summed to count the number of wires of that color and be used to find the last red and last blue wire index.

Storing the function sum as s saved bytes due to the number of times it was called.

Converting the number of wires to a character for the switch statement was done using paste, which is less bytes than as.character()

Initializing the variable v, the number of the wire to cut,to 4 before switch statement allows for the me not to have to add that statement each time the wire number is 4.

For length of 4 the second and third conditions can be combined with an or statement to reduce the numbers of ifelse since they evaluate to the same wire to cut.

Shayne03

Posted 2017-06-09T18:08:30.210

Reputation: 347

You may want to look at this tip. It should help shave off a number of bytes. eg "if"(R<1,v=2,"if"(s(u)>1,v=tail(which(u),1),)) vs if(R<1){v=2}else if(s(u)>1){v=tail(which(u),1)}else{}

– MickyT – 2017-06-18T20:14:36.623

I think you can get away with | and & rather than || and && in your conditionals. If you aren't using defaults in the switch, you can pass in an integer. Your ungolfed looks like it does use a default, but your golfed code doesn't – MickyT – 2017-06-18T20:35:45.383

1

Excel VBA, 521 446 419 Bytes

Full subroutine that takes input of expected type Array and Boolean (or Truthy/Falsy) and outputs which wire you should cut to range [A4].

Sub c(x,s)
i=UBound(x)
[A1].Resize(1,i+1)=x
[A2].Resize(1,5)=Split("B U R W Y")
[A3]="=COUNTIF(1:1,A2)"
[A3].AutoFill[A3:E3]
j=x(i)
B=[A3]
u=[B3]
R=[C3]
W=[D3]
y=[E3]
[A4]=Choose(i-1,IIf(R,IIf(j="U",i+1,IIf(U>1,InStrRev(Join(x,""),"U"),i)),2),IIf(s*(R>1),InStrRev(Join(x,""),"R"),IIf((j="Y")*(R=0)Or u=1,1,IIf(y>1,i,2))),IIf(s*(j=8),4,IIf((r=1)*(y>1),1,IIf(b,1,2))),IIf((y=0)*s,3,IIf((y=1)*(w>1),4,IIf(r,4,i))))
End Sub

Example I/O

Note: In order to pass an array in VBA it is necessary to use either an Array or split statement

c Array("B","R","R"),FALSE 
 3 

Taylor Scott

Posted 2017-06-09T18:08:30.210

Reputation: 6 709

0

JavaScript (ES6), 252 244 bytes

Test cases can be viewed in this codepen.

d=(a,o,[b,u,r,y,w]=[..."BURYW"].map(i=>a.filter(t=>t==i).length),q="lastIndexOf")=>[!r?2:a[2]=='W'?3:u>1?a[q]('U')+1:3,r>1&&o?a[q]('R')+1:a[3]=='Y'&&!r?1:u==1?1:y>1?4:2,a[4]=='B'&&o?4:r==1&&y>1?1:!b?2:1,!y&&o?3:y==1&&w>1?4:!r?6:4][a.length-3];

ajxs

Posted 2017-06-09T18:08:30.210

Reputation: 141