import json

def load_data(file_path):
    with open(file_path, 'r') as file:
        return json.load(file)

def is_valid_sequence(sequence, constraints):
    for constraint in constraints:
        if constraint[0] in sequence and constraint[1] in sequence:
            if sequence.index(constraint[0]) > sequence.index(constraint[1]):
                return False
    return True

def save_sequences(sequences, file_path):
    with open(file_path, 'w') as file:
        json.dump(sequences, file, indent=4)

# adjacency_matrix = load_data('adjacency_matrix.json')
# constraints = load_data('constraints.json')
# sequences = load_data('sequences.json')

# all_parts = adjacency_matrix['allParts']
# first_detail = adjacency_matrix['firstDetail']

# valid_sequences = []
# for sequence in sequences:
#     if len(set(sequence)) == len(set(all_parts)): #and is_valid_sequence(sequence, constraints):
#         valid_sequences.append(sequence)

# save_sequences(valid_sequences, 'valid_sequences.json')

# print(f"Найдено {len(valid_sequences)} допустимых последовательностей сборки.")

def filter_valid_sequences(adjacency_matrix, sequences, constraints):
    all_parts = adjacency_matrix['allParts']
    first_detail = adjacency_matrix['firstDetail']

    valid_sequences = []
    for sequence in sequences:
        if len(set(sequence)) == len(set(all_parts)):  
            valid_sequences.append(sequence)

    return valid_sequences