title: Which wire to cut

text:
# Question
Title: Which wire to cut
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>
```

# Answer
## 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);
}
```
> 10 votes

# Answer
# 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!
> 5 votes

# Answer
# 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

```
> 4 votes

# Answer
# 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.
> 4 votes

# Answer
# 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)
> 4 votes

# Answer
# 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:
> 3 votes

# Answer
# 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.
> 2 votes

# Answer
# 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!
> 1 votes

# Answer
# 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.
> 1 votes

# Answer
# Excel VBA, <sub>~~521~~ ~~446~~</sub> 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

```
> 1 votes

# Answer
## 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];

```
> 0 votes

---
Tags: code-golf, game, compression
---


title: Normalise my decision map

text:
# Question
Title: Normalise my decision map
Write a function or program that processes a block of text and returns the new text. Smallest valid program wins.

Each line in the block of text will have the following format:

```
12:34,56

```

The first number is the line ID, the other two comma separated numbers are references to other lines.

In the input text the numbers can be any integer greater than or equal to 0. All numbers will be in ASCII encoded decimal with no leading zeros. There will be no duplicate line IDs. There will be no references to non-existent line IDs, although there may be line IDs that are not referenced.

In the output text the lowest numbered line will be moved to the start of the text block and renumbered to 0. Any references to this line must be updated too. The first reference on that line must be 0 or 1. The second reference can only be 2 if the first reference is 1. Otherwise it must be 0 or 1.

All the lines must be in ascending, incrementing order (no skipped numbers). You can only have a reference to line n if there has been a previous reference to line n-1 or the current line ID is n. There must be no lines that are not referenced by lower line IDs except for line 0. Any such lines must be removed before the final output.

You may assume that the input text is always in the correct format.

Test input #1:

```
45:73,24
78:24,78
89:24,73
73:45,3
72:3,24
3:24,24
24:3,89

```

Reordered:

```
3:24,24
24:3,89
89:24,73
73:45,3
45:73,24
78:24,78
72:3,24

```

Renumbered:

```
0:1,1
1:0,2
2:1,3
3:4,0
4:3,1
78:1,78
72:0,1

```

Unreferenced lines removed for final output:

```
0:1,1
1:0,2
2:1,3
3:4,0
4:3,1

```

Of course, your program doesn't have to follow this order, just produce the correct output. The output must be a single block of text or the closest equivalent in your language, i.e. no character by character direct output. You may either return it (preferred) or output the whole block directly. Assume your output will be passed to another function or program.

Test input #2

```
5:2,3
7:3,2
2:4,2
4:2,3
3:4,3

```

Output:

```
0:1,0
1:0,2
2:1,2

```

Test input #3

```
7:6,3
3:9,7
9:7,3
2:9,6
6:6,7

```

Output:

```
0:1,2
1:3,4
2:2,3
3:2,4
4:1,3

```

# Answer
> 1 votes
# Python 3, ~~226~~ ~~215~~ ~~211~~ ~~209~~ 179 bytes

```
def f(s):
 S=dict(map(eval,i.split(":"))for i in s.split("\n"));r="";L=[min(S)];i=0
 while L[i:]:a=S[L[i]];L+=set(a)-set(L);r+="%%d:%d,%d\n"%tuple(map(L.index,a))%i;i+=1
 return r

```

Try it online!

---
Tags: code-golf
---


title: Output this sequence of numbers

text:
# Question
Title: Output this sequence of numbers
Your challenge is to write a program or function to output this sequence of numbers.

Output requirements:

* Must contain numbers specified above
* Must be in order as specified
* Numbers must be delimited by whitespace

Shortest code in bytes wins.

# Answer
> 5 votes
# Python, 76 bytes

```
from math import*
s=2**32-5
for i in range(6499):print((s-factorial(i+1))%s)

```

A033312.

# Python, 84 bytes

```
from math import*
k=s=2**32-6
for i in range(6499):s-=i*factorial(i);s%=k+1;print(s)

```

A001563.

# Answer
> 2 votes
# Python 3, 148 bytes

```
a=[1]
n=6499
N=2**32-5
exec("a+=[a[-1]*len(a)%N];"*n)
a=[i*a[i]for i in range(n)]
for i in range(1,n):a[i]=(a[i]+a[i-1])%N
print([N-i-1 for i in a])

```

Try it online!

Surely this can be golfed.

# Answer
> 1 votes
# Charcoal, 39 bytes

```
Ａ⁻Ｘ²¦³²¦⁵σＡ¹φＡ¹κＦ⁶⁴⁹⁹«⁺﹪⁻σφσ¶Ａ⁺κ¹κＡ×φκφ

```

Try it online!

## Explanation

```
Ａ         σ                                    Assign to s
  ⁻Ｘ²¦³²¦⁵                                     2 ** 32 - 5
            Ａ¹φ                                Assign 1 to f (factorial)
                Ａ¹κ                            Assign 1 to k
                    Ｆ⁶⁴⁹⁹«                     For i from 0 to 6498
                           ⁺﹪⁻σφσ¶             (s - f) % s + "\n" (implicitly printed)
                                   Ａ⁺κ¹κ       k = k + 1
                                         Ａ×φκφ Assign f * k to f

```

# Answer
> 0 votes
# 05AB1E, 19 bytes

```
žJ5-6499FDN>!-sDŠ%,

```

Try it online!

Only checked first and last numbers. If you run it TIO will probably take a while to print them all

---

Explanation

```
žJ5-                # (2 ** 32) - 5; call this x
    6499F           # for N in 0 .. 6498. Stack entering loop: [x]
         D          # duplicate. Stack: [x, x]
          N>        # N + 1. Stack: [x, x, N + 1]
            !       # factorial. Stack: [x, x, (N + 1)!]
             -      # subtract. Stack: [x, x - (N + 1)!]
              s     # swap. Stack: [x - (N + 1)!, x]
               D    # duplicate. Stack: [x - (N + 1)!, x, x]
                Š   # cycle last three items. Stack: [x, x - (N + 1)!, x]
                 %  # modulus. Stack [x, (x - (N + 1)!) % x)]
                  , # pop and print. Stack: [x]

```

---
Tags: code-golf, math, sequence
---


title: Combinatory Conundrum!

text:
# Question
Title: Combinatory Conundrum!
## Introduction: Combinatory Logic

Combinatory logic (CL) is based off of things called *combinators*, which are basically functions. There are two basic "built-in" combinators, `S` and `K`, which will be explained later.

### Left-associativity

CL is *left-associative*, which means brackets (containing stuff) which are at the far-left of anther pair of brackets containing it can be removed, with its stuff released. For example, something like this:

```
((a b) c)

```

Can be reduced to

```
(a b c)

```

Where the `(a b)` is on the far-left of the bigger bracket `((a b) c)`, so it can be removed.

A much bigger example of left-association (square brackets are explanations):

```
  ((a b) c ((d e) f (((g h) i) j)))
= (a b c ((d e) f (((g h) i) j)))   [((a b) c...) = (a b c...)]
= (a b c (d e f (((g h) i) j)))     [((d e) f...) = (d e f...)]
= (a b c (d e f ((g h i) j)))       [((g h) i) = (g h i)]
= (a b c (d e f (g h i j)))         [((g h i) j) = (g h i j)]

```

Brackets can also be reduced when more than one pair wrap around the same objects. Examples:

```
((((a)))) -> a
a ((((b)))) -> a b
a (((b c))) -> a (b c) [(b c) is still a group, and therefore need brackets.
                        Note that this doesn't reduce to `a b c`, because
                        `(b c)` is not on the left.]

```

### Builtins

CL has two "built-in" combinators, `S` and `K`, which can switch objects (single combinators, or a group of combinators / groups wrapped around brackets) around like so:

```
K x y = x
S x y z = x z (y z)

```

Where `x`, `y` and `z` can be stand-ins for anything.

An example of `S` and `K` are as follows:

```
  (S K K) x [x is a stand-in for anything]
= S K K x   [left-associativity]
= K x (K x) [S combinator]
= x         [K combinator]

```

Another example:

```
  S a b c d
= a c (b c) d [combinators only work on the n objects to the right of it,
               where n is the number of "arguments" n is defined to have -
               S takes 3 arguments, so it only works on 3 terms]

```

The above are examples of normal CL statements, where the statement cannot be evaluated further and achieves an end result in a finite amount of time. There are non-normal statements (which are CL statements that do not terminate and will keep being evaluated forever), but they aren't within the scopes of the challenge and won't need to be covered.

If you want to learn more about CL, read this Wikipedia page.

## Task:

Your task is to make extra combinators, given the number of arguments, and what it evaluates to as input, which is given like so:

```
{amount_of_args} = {evaluated}

```

Where `{amount_of_args}` is a positive integer equal to the number of args, and `{evaluated}` consists of:

* arguments up to the amount of args, with `1` being the first argument, `2` being the second, etcetera.
    + You are guaranteed that argument numbers above the amount of args (so a `4` when `{amount_of_args}` is only `3`) won't appear in `{evaluated}`.
* brackets `()`

So examples of inputs are:

```
3 = 2 3 1
4 = 1 (2 (3 4))

```

The first input is asking for a combinator (say, `R`) with three arguments (`R 1 2 3`), which then evaluates into:

```
R 1 2 3 -> 2 3 1

```

The second input asks for this (with a combinator name `A`):

```
A 1 2 3 4 -> 1 (2 (3 4))

```

Given the input in this format, you must return a string of `S`, `K` and `()`, which when substituted with a combinator name and run with arguments, returns the same evaluated statement as the `{evaluated}` block when the command block is substituted back for that combinator name.

The output combinator statement may have its whitespace removed and the outer brackets removed, so something like `(S K K (S S))` can be turned into `SKK(SS)`.

If you want to test your program's outputs, @aditsu has made a combinatory logic parser (which includes `S`, `K`, `I` and even other ones like `B` and `C`) here.

## Score:

Since this is a , the aim of this challenge is to achieve the smallest amount of bytes in the output possible, given these 50 test-cases. Please put your results for the 50 test-cases in the answer, or make a pastebin (or something similar) and post a link to that pastebin.

In the event of a tie, the earliest solution wins.

## Rules:

* Your answer must return CORRECT output - so given an input, it must return the correct output as per the definition in the task.
* Your answer must output within an hour on a modern laptop for each test case.
* Any hard-coding of solutions is disallowed. However, you are allowed to hard-code up to 10 combinators.
* Your program must return the same solution every time for the same input.
* Your program must return a valid result for any input given, not just test-cases.

# Answer
> 8 votes
# Haskell, score 5017

This combines the dumbest possible algorithm for abstraction elimination ((λ*x*. *x*) = I; (λ*x*. *y*) = K*y*; (λ*x*. *M* *N*) = S(λ*x*. *M*)(λ*x*. *N*)) with a peephole optimizer used after every application. The most important optimization rule is S(K*x*)(K*y*) ↦ K(*xy*), which stops the algorithm from always blowing up exponentially.

The set of rules is configured as a list of string pairs so it’s easy to play around with new rules. As a special bonus of reusing the input parser for this purpose, S, K, and I are also accepted within the input combinators.

Rules are not applied unconditionally; rather, both the old and new versions are kept, and suboptimal versions are pruned only when their length exceeds that of the best version by more than some constant (currently 3 bytes).

The score is slightly improved by treating I as a fundamental combinator until the output stage rewrites it to SKK. That way KI = K(SKK) can be shortened by 4 bytes to SK on output without confusing the rest of the optimizations.

```
{-# LANGUAGE ViewPatterns #-}

import qualified Data.IntMap as I
import qualified Data.List.NonEmpty as N
import System.IO

data Term
  = V Int
  | S
  | K
  | I
  | A (N.NonEmpty (Int, Term, Term))
  deriving (Show, Eq, Ord)

parse :: String -> (Term, String)
parse = parseApp . parse1

parseApp :: (Term, String) -> (Term, String)
parseApp (t, ' ':s) = parseApp (t, s)
parseApp (t, "") = (t, "")
parseApp (t, ')':s) = (t, ')' : s)
parseApp (t1, parse1 -> (t2, s)) =
  parseApp (A (pure (appLen (t1, t2), t1, t2)), s)

parse1 :: String -> (Term, String)
parse1 (' ':s) = parse1 s
parse1 ('(':(parse -> (t, ')':s))) = (t, s)
parse1 ('S':s) = (S, s)
parse1 ('K':s) = (K, s)
parse1 ('I':s) = (I, s)
parse1 (lex -> [(i, s)]) = (V (read i), s)

ruleStrings :: [(String, String)]
ruleStrings =
  [ ("1 3(2 3)", "S1 2 3")
  , ("S(K(S(K1)))(S(K(S(K2)))3)", "S(K(S(K(S(K1)2))))3")
  , ("S(K(S(K1)))(S(K2))", "S(K(S(K1)2))")
  , ("S(K1)(K2)", "K(1 2)")
  , ("S(K1)I", "1")
  , ("S(S(K1)2)(K3)", "S(K(S1(K3)))2")
  , ("S(SI1)I", "S(SSK)1")
  ]

rules :: [(Term, Term)]
rules = [(a, b) | (parse -> (a, ""), parse -> (b, "")) <- ruleStrings]

len :: Term -> Int
len (V _) = 1
len S = 1
len K = 1
len I = 3
len (A ((l, _, _) N.:| _)) = l

appLen :: (Term, Term) -> Int
appLen (t1, S) = len t1 + 1
appLen (t1, K) = len t1 + 1
appLen (K, I) = 2
appLen (t1, t2) = len t1 + len t2 + 2

notA :: Term -> Bool
notA (A _) = False
notA _ = True

alt :: N.NonEmpty Term -> Term
alt ts =
  head $
  N.filter notA ts ++
  [A (N.nub (a N.:| filter (\(l, _, _) -> l <= minLen + 3) aa))]
  where
    a@(minLen, _, _) N.:| aa =
      N.sort $ do
        A b <- ts
        b

match :: Term -> Term -> I.IntMap Term -> [I.IntMap Term]
match (V i) t m =
  case I.lookup i m of
    Just ((/= t) -> True) -> []
    _ -> [I.insert i t m]
match S S m = [m]
match K K m = [m]
match I I m = [m]
match (A a) (A a') m = do
  (_, t1, t2) <- N.toList a
  (_, t1', t2') <- N.toList a'
  m1 <- match t1 t1' m
  match t2 t2' m1
match _ _ _ = []

sub :: I.IntMap Term -> Term -> Term
sub _ S = S
sub _ K = K
sub _ I = I
sub m (V i) = m I.! i
sub m (A a) =
  alt $ do
    (_, t1, t2) <- a
    pure (sub m t1 & sub m t2)

optimize :: Term -> Term
optimize t = alt $ t N.:| [sub m b | (a, b) <- rules, m <- match a t I.empty]

infixl 5 &

(&) :: Term -> Term -> Term
t1 & t2 = optimize (A (pure (appLen (t1, t2), t1, t2)))

elim :: Int -> Term -> Term
elim n (V ((== n) -> True)) = I
elim n (A a) =
  alt $ do
    (_, t1, t2) <- a
    pure (S & elim n t1 & elim n t2)
elim _ t = K & t

paren :: String -> Bool -> String
paren s True = "(" ++ s ++ ")"
paren s False = s

output :: Term -> Bool -> String
output S = const "S"
output K = const "K"
output I = paren "SKK"
output (V i) = \_ -> show i ++ " "
output (A ((_, K, I) N.:| _)) = paren "SK"
output (A ((_, t1, t2) N.:| _)) = paren (output t1 False ++ output t2 True)

convert :: Int -> Term -> Term
convert 0 t = t
convert n t = convert (n - 1) (elim n t)

process :: String -> String
process (lex -> [(n, lex -> [((`elem` ["=", "->"]) -> True, parse -> (t, ""))])]) =
  output (convert (read n) t) False

main :: IO ()
main = do
  line <- getLine
  putStrLn (process line)
  hFlush stdout
  main

```

Try it online!

### Output

1. S(KS)K
2. S(K(SS(KK)))(S(KK)S)
3. S(K(SS))(S(KK)K)
4. S(K(SS(KK)))(S(KK)(S(KS)(S(K(S(SKK)))K)))
5. S(K(S(K(SS(SK)))))(S(K(SS(SK)))(S(SKK)(SKK)))
6. KK
7. S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)
8. S(K(SS(K(S(KK)(S(SKK)(SKK))))))(S(SSK(KS))(S(S(KS)(S(KK)(S(KS)K)))(K(S(K(S(SSK)))K))))
9. S(K(S(KK)))(S(K(S(S(SKK)(SKK))))K)
10. SK
11. S(KS)(S(KK)(S(K(SS))(S(KK)K)))
12. S(K(SS(K(S(KK)K))))(S(KK)(S(KS)(S(SSK(KS))(S(K(SS))(S(KK)K)))))
13. S(K(S(K(S(K(SS(KK)))(S(KK)S)))))(S(K(SS(KK)))(S(KK)(S(KS)(S(K(S(SKK)))K))))
14. S(K(S(K(S(K(SS(KK)))(S(KK)S)))))(S(K(S(SKK)))K)
15. S(K(S(K(S(KS)K))))(S(KS)K)
16. S(K(S(KS)K))
17. S(K(S(K(S(K(SS(K(S(S(KS)(S(KK)(SSK)))(K(S(SKK)(SKK)))))))(S(KK)(S(KS)K))))))(S(K(SS(K(SSK))))(S(KK)(S(KS)(S(KK)(SSK)))))
18. SSS(KK)
19. KK
20. S(KK)(S(KK)(S(S(KS)K)(S(K(S(SKK)))(S(K(S(SKK)))K))))
21. S(S(KS)(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))
22. S(KK)
23. S(KS)(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K)))))
24. S(K(S(K(S(KS)K))))(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(S(KK)(S(K(SS))(S(KK)K))))
25. S(KS)(S(KK)(S(KS)K))
26. S(S(KS)(S(KK)(S(KS)(S(KK)(S(K(S(K(SS(KK)))))(S(KS)(S(KK)(S(SSK(KS))(S(KS)(S(SKK)(SKK)))))))))))(K(S(S(KS)(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(SKK)))K))))))))(S(K(S(SKK)))K)))(S(K(S(K(S(KK)K))))(S(K(S(SKK)))K))))
27. S(K(S(K(S(K(SS(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))(S(KK)(S(KS)K))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))
28. K(S(KK))
29. S(K(S(K(S(K(S(K(S(KS)K))))(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))K)))))(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(S(KK)(S(K(SS))(S(KK)K))))
30. S(KK)(S(K(SSS(KK))))
31. K(SSS(KK))
32. S(K(SS(K(S(S(KS)(S(KK)(S(KS)K)))(K(S(K(S(SKK)))K))))))(S(KK)(S(KS)(SS(S(S(KS)(S(KK)(S(KS)(S(K(S(KS)(S(KK)(S(KS)K)))))))))(KK))))
33. S(K(S(K(S(K(S(K(SS(KK)))(S(KK)S)))))))(S(K(SS(K(S(KK)K))))(S(KK)(SSS(KS))))
34. S(K(S(K(S(KK)K))))
35. S(K(S(K(S(K(S(K(SS(K(S(K(S(SKK)))K))))(S(KK)(S(KS)(S(KK)(S(K(SS(K(S(K(S(SKK)))K))))(S(KK)(S(K(SS))(S(KK)K)))))))))))))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))
36. S(K(SS(K(S(K(SS(K(S(K(S(SKK)))K))))(S(KK)(S(KS)(SS(S(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K)))))(KK))))))))(S(KK)(S(KS)(S(KK)(S(K(S(K(S(K(S(K(S(K(SS(KK)))(S(KK)S)))))))))(S(K(SS(KK)))(S(KK)(S(KS)(S(K(S(KS)(S(KK)(S(KS)K))))))))))))
37. S(KK)(S(K(S(K(S(KK)(S(KK)K)))))(SS(SK)))
38. K(S(K(SSS(KK))))
39. S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))(S(KK)(S(KS)K))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))
40. S(K(S(KK)))(S(KS)(S(KK)(S(K(S(KK)(S(KK)K))))))
41. S(K(SS(K(S(S(KS)(S(KK)(S(KS)K)))(K(S(K(S(SKK)))K))))))(S(KK)(S(KS)(S(KK)(S(K(S(K(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K))))))(S(K(SS(K(S(KK)(S(K(SS))K)))))(S(KK)(S(K(SS))(S(KK)(S(K(S(K(S(KK)(S(KS)K)))))(S(KS)K))))))))))
42. S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))(S(KK)(S(KS)K))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))
43. K(K(K(K(K(S(KK)(S(KK)(S(K(SS(SK)))(SSK))))))))
44. S(KK)(S(K(S(KK)(S(KK)(S(KK)(S(KK)K))))))
45. S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))(S(KK)(S(KS)K))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))
46. S(K(S(K(S(K(S(K(S(K(SS(K(S(K(SS(K(S(K(SS(KK)))(S(KK)(S(KS)(S(K(S(SKK)))K)))))))(S(KK)(S(KS)(S(KK)(S(SSK(KS))(S(K(SS))(S(KK)K))))))))))(S(KK)(S(KS)(S(KK)(S(K(SS(K(S(KK)(S(KS)(S(KK)(S(K(SS(K(S(KK)(S(KS)K)))))(S(KK)(S(K(SS))(S(KK)(S(K(SS(K(S(KK)K))))(S(KK)S))))))))))))(S(KK)(S(K(SS))K))))))))))(S(K(SS(K(S(KK)(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(S(KK)(S(K(SS))(S(KK)K))))))))(S(KK)S))))))(S(K(SS(K(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(S(KK)(S(K(SS))(S(KK)K)))))))(S(KK)(S(KS)(S(KK)(S(K(S(K(S(KS)(S(KK)(S(KS)K))))))(S(KS)(S(KK)(S(K(SS))(S(KK)K)))))))))
47. S(K(SS(K(SS(S(S(KS)(S(KK)S)))(KK)))))(S(KK)(S(KS)(S(K(S(K(S(KS)(S(KK)(S(KS)(S(KK)(S(K(S(K(S(K(SS(K(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)K))))))(S(KK)(S(K(SS))(S(KK)K)))))))(S(KK)(S(KS)K))))))))))))))(S(K(S(S(KS)(S(KK)(S(K(SS))(S(KK)(S(K(S(K(S(KS)K))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(SS))(S(KK)K)))))))))))))(S(KK)(S(K(S(K(S(KK)(S(KS)(S(KK)(S(K(SS(K(S(KK)(S(KS)K)))))(S(KK)(S(K(SS))K)))))))))(S(KS)(S(KK)(S(K(SS(K(S(KK)K))))(S(KK)(S(KS)(S(SSK(KS))(S(K(SS(KK)))(S(KK)(S(KS)(S(K(S(SKK)))K))))))))))))))))
48. K(S(K(S(KK)(S(K(S(KK)(S(K(S(KK)(S(KK)K))))))))))
49. S(KK)(S(K(S(K(S(KK)(S(K(S(K(S(KK)(S(K(S(K(S(KK)(S(K(S(K(S(KK)(S(K(S(KK)))(S(K(S(SKK)))K))))))(S(K(S(SKK)))K))))))(S(K(S(SKK)))K))))))(S(K(S(SKK)))K))))))(S(K(S(SKK)))K))
50. S(K(S(K(S(K(S(K(S(K(S(KK)))(S(K(SS(K(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)K)))))))(S(K(SS(K(S(K(SS))(S(KK)K)))))(S(KK)(S(KS)(S(KK)(S(K(S(K(S(KK)(S(KK)(S(KK)(S(KK)K)))))))(S(K(SS))(S(KK)K)))))))

# Answer
> 5 votes
## Sum of solution lengths: ~~12945 8508~~ 5872

Haskell code that takes input lines from stdin and doesn't care if the separator is `=` or `->`:

```
data E=S|K|V Int|A E E deriving Eq

instance Show E where
  showsPrec _ S = showChar 'S'
  showsPrec _ K = showChar 'K'
  showsPrec _ (V i) = shows i
  showsPrec p (A e f) = showParen (p>0) $ showsPrec 0 e . showsPrec 1 f

type SRead a = String -> (a,String) -- a simpler variation of ReadS

parse :: String -> E
parse s = let (e,"")=parseList (s++")") in e
parseList :: SRead E
parseList s = let (l,s')=parseL s in (foldl1 A l,s')
parseL :: SRead [E]
parseL (c:s) | c==' ' = parseL s
             | c==')' = ([],s)
parseL s = let (p,s')=parseExp s; (l,s'')=parseL s' in (p:l,s'')
parseExp :: SRead E
parseExp ('(':s) = parseList s
parseExp s = let [(n,s')]=reads s in (V n,s')

k e = A K e
s e f = A (A S e) f
i = s K K
s3 e f g = A (s e f) g
sk = A S K
ssk e f = A (s3 S K e) f

n `invars` (A e f) = n `invars` e || n `invars` f
n `invars` (V m)   = n==m
_ `invars` _       = False

comb (A e f) = comb e && comb f
comb (V _)   = False
comb _       = True

abstract _ (A (A S K) _) = sk
abstract n e | not (n `invars` e) = k e
abstract n (A e (V _)) | not (n `invars` e) = e
abstract n (A (A (V i) e) (V j)) | n==i && n==j =
                                   abstract n (ssk (V i) e)
abstract n (A e (A f g)) | comb e && comb f =
                                   abstract n (s3 (abstract n e) f g)
abstract n (A (A e f) g) | comb e && comb g =
                                   abstract n (s3 e (abstract n g) f)
abstract n (A (A e f) (A g h)) | comb e && comb g && f==h =
                                   abstract n (s3 e g f)
abstract n (A e f) = s (abstract n e) (abstract n f)
abstract n _ = i

abstractAll 0 e = e
abstractAll n e = abstractAll (n-1) $ abstract n e

parseLine :: String -> (Int,E)
parseLine s = let [(n,s')] = reads s
                  s''=dropWhile(`elem` " =->") s'
              in (n, parse s'')

solveLine :: String -> E
solveLine s = let (n,e) = parseLine s in abstractAll n e

main = interact $ unlines . map (show . solveLine) . lines

```

It implements the improved bracket abstraction from section 3.2 of John Tromp: *Binary Lambda Calculus and Combinatory Logic* which is linked to from the Wikipedia article on combinatory logic. The most useful special cases only use the `S` combinator to suffle subterms around in order to avoid deep nesting of variables. The case that checks for equality of some subterms is not needed for any of the test cases. While there is no special case for handling the `W` combinator (see Peter's answer), the rules work together to find the shorter `SS(SK)` expression. (I first made a mistake by trying to optimize away the inner calls to `abstract`, then this `W` optimization did not happen and the overall result was 16 bytes longer.)

And here are the results from the test cases:

```
S(KS)K
S(K(S(K(SS(KK)))K))S
S(K(S(K(SS))K))K
S(K(S(K(S(K(S(K(SS(KK)))K))S))(S(SKK))))K
S(K(S(K(SS(SK)))))(S(K(SS(SK)))(S(SKK)(SKK)))
KK
S(K(S(K(S(S(K(S(KS)(S(SKK))))K)))K))K
S(K(S(K(SS(K(S(KK)(S(SKK)(SKK))))))(S(KS))))(S(K(S(K(S(K(SS(K(S(K(S(SSK)))K))))K))S))K)
S(K(S(K(S(KK)))(S(S(SKK)(SKK)))))K
SK
S(K(S(K(S(K(S(S(KS)(S(KS)))))K))K))K
S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(KK)K))))K))S))(S(KS))))(SS)))K))K
S(K(S(K(S(K(S(K(SS(KK)))K))S))))(S(K(S(K(S(K(S(K(SS(KK)))K))S))(S(SKK))))K)
S(K(S(K(S(K(S(K(S(K(SS(KK)))K))S))))(S(SKK))))K
S(K(S(K(S(K(S(K(S(K(SS(K(S(KS)K))))K))S))K))S))K
S(K(S(KS)K))
S(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(K(S(K(S(K(S(K(S(K(SS(K(S(S(K(S(KS)(S(KS))))(S(K(S(K(S(K(SS(K(S(S(KS)(S(K(SS(K(S(SKK)(SKK)))))K))K))))K))S))K))(S(KK)K)))))K))S))K))S))K))(S(K(S(KK)K))K)
S(KK)(S(KK))
KK
S(K(S(KK)K))(S(S(KS)K)(S(K(S(K(S(SKK)))(S(SKK))))K))
S(K(S(K(S(K(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K)))K))K))K
S(KK)
S(K(S(K(S(K(S(K(S(S(KS)(S(K(S(KS)(S(KS))))))))K))K))K))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))(S(S(KS)(S(KS))))))K))K))K
S(K(S(K(S(KS)K))S))K
S(K(S(K(S(K(S(K(SS(KK)))(S(KS))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(K(S(KS)(S(SKK))))K))))))))(S(SKK))))K)(S(K(S(K(S(K(S(KK)K))))(S(SKK))))K)))))K))S))K))S))K))S))(S(SKK)(SKK)))
S(K(S(K(S(K(S(K(S(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K))))K))K))K)))K))K))K))K
K(S(KK))
S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))(S(S(KS)(S(KS))))))K))K))K)
S(KK)(S(K(S(KK)(S(KK)))))
K(S(KK)(S(KK)))
S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)))(S(K(S(K(S(K(SS(K(S(K(S(SKK)))K))))K))S))K)))))K))S))K))S))(S(K(S(K(S(K(S(KS)K))S))K)))
S(K(S(K(S(K(S(K(S(K(SS(KK)))K))S))))))(S(K(S(K(S(K(SS(K(S(KK)K))))K))S))(S(KS)))
S(K(S(K(S(KK)K))))
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(S(K(S(K(S(KS)(S(K(S(K(S(KS)(S(SKK))))K)))))(S(SKK))))K)))K))K))K))))))(S(S(K(S(KS)(S(SKK))))K))))K))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(KK)))K))S))))))))))(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)))(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(KK)))(S(SKK))))K))))K))S))K))S))(S(SKK))))K)))))K))S))K))S))(S(K(S(K(S(K(S(KS)K))S))K))))
S(K(S(KK)(S(K(S(K(S(KK)K))K)))))(SS(SK))
K(S(K(S(KK)(S(KK)))))
S(K(S(K(S(K(S(K(S(K(S(K(S(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))(S(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K))))K))K))K))))K))K))K))K))))K))K))K))K))K)))K))K))K))K))K))K
S(K(S(K(S(K(S(KK)(S(K(S(K(S(KK)K))K)))))))S))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(KS)K))S))K))))K))))(S(K(S(K(S(K(SS(K(S(K(S(SKK)))K))))K))S))K)))))K))S))K))S))K))S))K))S))K))S))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))(S(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K))))K))K))K))))K))K))K))K))))K))K))K))K))K))))K))K))K))K))K))K)))K))K))K))K))K))K))K
K(K(K(K(K(S(K(S(KK)K))(S(K(SS(SK)))(SSK)))))))
S(KK)(S(K(S(K(S(K(S(K(S(KK)K))K))K))K)))
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))(S(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K))))K))K))K))))K))K))K))K))))K))K))K))K))K))))K))K))K))K))K))K))))K))K))K))K))K))K))K)))K))K))K))K))K))K))K))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))))))))(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(KK)K))K))K))))K))S))(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))(S(K(S(K(S(K(S(K(SS(K(S(KK)K))))K))S))(S(KS)))))))(S(S(K(S(KS)(S(KS))))(S(K(S(K(S(K(S(K(SS(KK)))K))S))(S(SKK))))K)))))K))K))K))))K))K))K))K))K))))K))K))K))K))K))K))K)))K))K))K))K))K))K))K))K))K
S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KK)K))K))K))K))K))K))K))))K))S))(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))))))))(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(K(S(KK)K))K))K))K))))K))S))(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS)))))))))))))(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))))))))(S(K(S(K(S(K(S(K(SS(K(S(K(S(KK)K))K))))K))S))(S(K(S(KS)(S(KS)))))))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(SS(K(S(K(S(K(S(K(S(K(S(KK)K))))))))(S(K(S(K(SS(KK)))K))S)))))K))S))K))S))K))S))K))S))(S(KS))))(S(K(S(K(S(K(S(K(SS(KK)))K))S))(S(SKK))))K))
K(S(K(S(KK)(S(K(S(KK)(S(K(S(K(S(KK)K))K)))))))))
S(K(S(K(S(KK)(S(K(S(K(S(K(S(KK)(S(K(S(K(S(K(S(KK)(S(K(S(K(S(K(S(KK)(S(K(S(K(S(K(S(KK)(S(KK))))(S(SKK))))K)))))(S(SKK))))K)))))(S(SKK))))K)))))(S(SKK))))K)))))(S(SKK))))K
S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KK)(S(K(S(K(S(K(S(K(S(KK)K))K))K))K)))))))))))(S(S(K(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(K(S(KS)(S(KS))))))))))(S(S(K(S(K(S(K(S(K(S(KS)(S(K(S(KS)(S(KS)))))))(S(S(K(S(K(S(K(S(KS)(S(KS))))(S(S(K(S(KS)(S(SKK))))K))))K))K))))K))K))K))))K))K))K))K))))K))K))K))K))K

```

# Answer
> 3 votes
## 8486 using S, K, I, W

### Explanation

The standard algorithm (as described e.g. in chapter 18 of *To Mock a Mockingbird*) uses four cases, corresponding to the combinators `S`, `K`, `I = SKK`, and simple left-association. I think this is what Christian's answer implements. This is sufficient, but not necessarily optimal, and since we're allowed to hard-code up to 10 combinators it leaves 7 options.

Other well-known combinatorial combinators are

```
B x y z = x (y z)
C x y z = x z y
W x y = x y y

```

which, together with `K`, make a complete basis. In SK these are

```
B = S (K S) K
C = S (S (K (S (K S) K)) S) (K K)
W = S S (S K)

```

and the `SKI` rules derive those same expressions for `B` and `C`, but for `W` they derive `S S (K (S K K))`. I therefore chose to implement `W` as a special case.

### Program (CJam)

```
e# A tests whether argument is an array
{W=!!}:A;

e# F "flattens" an expression by removing unnecessary parentheses, although if the expression is a primitive
e# it actually wraps it in an array
{
  e# A primitive is already flat, so we only need to process arrays
  _A{
    ee{
      ~
      e# Stack: ... index elt
      e# First recurse to see how far that simplifies the element
      F
      e# If it's an array...
      _A{
        e# ... we can drop a level of nesting if either it's the first one (since combinator application
        e# is left-associative) or if it's a one-element array
        _,1=@!|{
          e# The tricky bit is that it might be a string, so we can't just use ~
          {}/
        }*
      }{
        \;
      }?
    }%
  }{a}?
}:F;

qN%{

e# Parse line of input
"->=()"" [[[]"er']+~
e# Eliminate the appropriate variables in reverse order. E eliminates the variable currently stored in V.
\,:)W%{
  e# Flatten current expression
  F

  e# Identify cases; X holds the eXpression and is guaranteed to be non-primitive
  :X
  [
    XVa=                  e# [V]
    Xe_V&!                e# case V-free expression
    X)_A0{V=}?\e_V&!*     e# case array with exactly one V, which is the last element
    X_e_Ve=~)>[VV]=X,2>*  e# case array with exactly two Vs, which are the last two elements
  ]
  1#
  e# Corresponding combinators
  [
    {;"SKK"}              e# I
    {['K\]}               e# K
    {);}                  e# X (less that final V)
    {););['S 'S "SK"]\a+} e# W special-cased as SS(SK) because the general-case algorithm derives SS(K(SKK))
    {['S\)E\E\]}          e# S (catch-all case)
  ]=~
}:EfV

e# Format for output
F
{
  _A{
    '(\{P}%')
  }*
}:P%

oNo}/

```

Online test suite

### Generated outputs:

```
S(KS)K
S(S(KS)(S(KK)S))(KK)
S(K(SS))(S(KK)K)
S(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K))))(KK)
S(K(S(K(SS(SK)))))(S(K(SS(SK)))(S(SKK)(SKK)))
KK
S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)
S(S(KS)(S(K(S(KS)))(S(S(KS)(S(KK)(S(KS)K)))(K(S(S(KS)(S(K(S(SKK)))K))(K(SKK)))))))(K(S(KK)(S(SKK)(SKK))))
S(K(S(KK)))(S(K(S(S(SKK)(SKK))))K)
K(SKK)
S(K(S(S(KS)(S(KS)))))(S(KK)(S(KK)K))
S(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(K(SS))(S(KK)K))))))(K(S(KK)K))
S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K))))(KK))))))(K(KK))
S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(K(S(SKK)))K)))))(K(KK))
S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)K)))))(K(S(KS)K))
S(K(S(KS)))(S(KK))
S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)K)))))(K(S(S(KS)(S(K(S(KS)))(S(S(KS)(S(KK)(S(KS)K)))(K(S(S(KS)(S(S(KS)K)(K(S(SKK)(SKK)))))K)))))(S(KK)K)))))))(S(KK)(S(KK)K))
S(KK)(S(KK))
KK
S(KK)(S(KK)(S(S(KS)K)(S(K(S(SKK)))(S(K(S(SKK)))K))))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))
S(KK)
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(KS))))))))(S(KK)(S(KK)(S(KK)K)))
S(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(KS)))))(S(KK)(S(KK)K))))))))(K(S(KK)(S(KK)K)))
S(KS)(S(KK)(S(KS)K))
S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(SKK)(SKK)))))))))(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(SKK)))))))(S(K(S(K(S(KK)))))(S(K(S(SKK)))K))))))(S(K(S(KK)))(S(K(S(KK)))(S(K(S(SKK)))K))))))))))(K(K(KK)))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))))))))(S(KK)(S(KK)(S(KK)K)))
K(S(KK))
S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(KS)))))(S(KK)(S(KK)K))))))))(K(S(KK)(S(KK)K))))))))))(K(K(S(KK)(S(KK)K))))
S(KK)(S(K(S(KK)))(S(K(S(KK)))))
K(S(KK)(S(KK)))
S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(KK))))))))))(K(S(K(S(KK)))(S(S(KS)(S(KK)(S(KS)K)))(K(S(K(S(SKK)))K)))))
S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(KK)))))(S(K(S(K(S(KS)))))(S(S(KS)(S(KK)(S(KS)(S(KS)))))(K(S(KK)K))))))))(K(K(KK)))
S(K(S(K(S(KK)))))(S(K(S(KK))))
S(K(S(K(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(SKK)))))(S(K(S(KK)))(S(K(S(SKK)))K)))))))))))(S(K(S(K(S(KK)))))(S(K(S(K(S(KK)))))(S(K(S(K(S(KK)))))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K)))))
S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(KK))))))))))(K(S(K(S(KK)))(S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K))))))(K(S(K(S(KK)))(S(K(S(SKK)))K))))))))))))))(K(K(K(K(KK)))))
S(KK)(S(K(S(KK)))(S(K(S(KK)))(S(K(S(KK)))(SS(SK)))))
K(S(K(S(KK)))(S(K(S(KK)))))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))))))))(S(KK)(S(KK)(S(KK)K))))))))))(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K)))))
S(K(S(KK)))(S(K(S(K(S(KK)))))(S(K(S(K(S(KK)))))(S(K(S(K(S(KK)))))(S(KS)K))))
S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)K)))))))))))(K(S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(S(KS)(S(KK)(S(KS)K)))))))(S(K(S(KK)))(S(S(KS)(S(KK)(S(KS)K)))(K(S(K(S(SKK)))K)))))))))))(K(K(S(KK)(S(KK)K))))))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))))))))(S(KK)(S(KK)(S(KK)K))))))))))(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))
K(K(K(K(K(S(KK)(S(KK)(S(S(KS)(SSK))(K(SKK)))))))))
S(KK)(S(K(S(KK)))(S(K(S(KK)))(S(K(S(KK)))(S(K(S(KK)))(S(KK))))))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))))))))(S(KK)(S(KK)(S(KK)K))))))))))(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K)))))))
S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))))(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(KK)))))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K))))(KK))))))(S(KK)(S(KK)K))))))))(K(K(S(KK)K))))))))))(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))(K(K(K(S(KK)(S(KK)K))))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))))))(K(K(K(K(S(KK)(S(KK)(S(KK)K))))))))))))))))))(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K))))))))
S(S(KS)(S(K(S(KS)))(S(K(S(KK)))(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))))))(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))))(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(K(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))))(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(K(S(K(S(K(S(KS)))))))))(S(K(S(K(S(K(S(K(S(K(S(KS)))))))))))(S(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(KK)(S(KS)(S(K(S(KS)))(S(S(KS)(S(KK)(S(KS)(S(K(S(SKK)))K))))(KK))))))))))))(K(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(KK)))))))(S(S(KS)(S(KK)S))(KK))))))))))))))(K(K(K(K(S(KK)(S(KK)K))))))))))))))))(K(K(K(K(K(S(KK)(S(KK)K))))))))))))))))))(K(K(K(K(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))))))))))))(K(K(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)(S(KK)K)))))))))
K(S(K(S(KK)))(S(K(S(K(S(KK)))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(KK))))))))))
S(KK)(S(K(S(KK)))(S(K(S(K(S(KK)))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(K(S(KK)))))))))))(S(K(S(K(S(K(S(K(S(K(S(SKK)))))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(SKK)))))))))(S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(SKK)))))))(S(K(S(K(S(KK)))))(S(K(S(K(S(SKK)))))(S(K(S(KK)))(S(K(S(SKK)))K))))))))))))))
S(K(S(K(S(K(S(KK)))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(K(S(K(S(K(S(KK)))))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(K(S(K(S(KS)))))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(K(S(KS)))))(S(K(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(S(K(S(SKK)))K))))(S(KK)K))))))(S(KK)(S(KK)K))))))))(S(KK)(S(KK)(S(KK)K))))))))))(S(KK)(S(KK)(S(KK)(S(KK)K))))))))))

```

---
Tags: metagolf, test-battery, combinatory-logic
---