Skip to main content
1 of 8
Veskah
  • 4.1k
  • 13
  • 29

Invert Some Switches on a Switchboard

Inspired by this challenge.

##Goal: Given a pre-configured switchboard and a list of indexes, invert the switches at the given indexes.

A switchboard is made up of some number of switches (v or ^) wrapped in -s and arranged into rows of varying length. Here is an example switchboard:

-v-^-v-
-^-v-
-v-^-v-

##Input:

  • A string (or list of strings) representing the switchboard. It is guaranteed to match the regex ((-[v^])+-)(\n(-[v^])+-)*.
  • A possibly empty list of numbers representing indexes, may be 0 or 1 indexed. These are the switches that need to flipped from v to ^ or vice-versa. The switches are indexed from left-to-right, top-to-bottom. E.g., in the example above, the last v in the first row would be in position 3 and the ^ in the middle row would be at 4 (using 1-indexing).

##Output:

  • A switchboard in the same shape as the input with the specified switches inverted. Any unspecified switches should retain their initial state.

##Rules:

  • Input will always be correctly formatted and no given indexes will be out of bounds.
  • The list of indexes will be sorted and will have no duplicates.
  • This is so shortest code wins.

##Examples:

#Using 1-indexing
input:
[]
-v-^-v-

output:
-v-^-v-

input:
[1]
-v-

output:
-^-

input:
[3,4,6]
-^-v-v-
-v-
-^-^-

output:
-^-v-^-
-^-
-^-v-

input:
[1,2,3,4,5,6]
-^-v-v-
-v-
-^-^-

output:
-v-^-^-
-^-
-v-v-
Veskah
  • 4.1k
  • 13
  • 29