1809A - Garland - 800

Updated: 2024-03-12
1 min read

1809A - Garland (implementation, 800)

Explanation

  1. If all the light bulbs have the same color, it is impossible to turn them all on, as you can’t perform the operation on the same color consecutively. In this case, print -1.
  2. If there are 3 light bulbs of the same color and one light bulb of a different color, it takes 6 operations to turn them all on:
    1. Turn on the different colored light bulb
    2. Turn on one of the other colored light bulbs
    3. Turn off the different colored light bulb
    4. Turn on the second light bulb of the same color
    5. Turn on the different colored light bulb
    6. Turn on the third light bulb of the same color
  3. In all other cases, it takes 4 operations to turn all the light bulbs on, as you can switch the light bulbs on in a sequence without violating the color restriction.

Solution

def solve(s):
    if all(c == s[0] for c in s):
        return -1
    elif s.count(s[0]) == 3 or s.count(s[1]) == 3:
        return 6
    else:
        return 4

t = int(input())
for _ in range(t):
    s = input()
    print(solve(s))