def loop(instructions):
    """
    Returns tuple of
     - acc valu when either loop detected or program terminated
     - set of possible corrupt instruction indexes
     - boolean, true if loop detected
    """

    current_instruction = 0
    acc = 0

    corrupt_instruction_candidates = set()
    visited_instructions = set()

    while 0 <= current_instruction < len(instructions):

        if current_instruction in visited_instructions:
            return (acc, corrupt_instruction_candidates, True)
        visited_instructions.add(current_instruction)


        words = instructions[current_instruction].split()

        if words[0] == "nop":
            corrupt_instruction_candidates.add(current_instruction)
            current_instruction += 1
        elif words[0] == "acc":
            acc += int(words[1])
            current_instruction += 1
        elif words[0] == "jmp":
            corrupt_instruction_candidates.add(current_instruction)
            current_instruction += int(words[1])
        else:
            print("wtf")
            exit(1)

    return (acc, corrupt_instruction_candidates, False)