Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using list comprehensions + comments solution in Clear category for Rotate Hole by H0r4c3
def rotate(state, pipe_numbers):
rotations = list()
pipes_list = list()
result = list()
# make a list with all rotations of state to the right (with step = 1, step = 2, ..., step = len(state))
rotations = [state[-i:] + state[:-i] for i in range(len(state))]
# make a list with only the items in positions of pipe_numbers
pipes_list = [[item[idx] for idx in pipe_numbers] for item in rotations]
# find the indexes for the tuples with all elements = 1
result = [idx for idx, item in enumerate(pipes_list) if set(item) == {1}]
return result
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8], "Example"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == [], "Mission impossible"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0], "Don't touch it"
assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5], "Two cannonballs in the same pipe"
July 24, 2022