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)

# Load data from files
adjacency_matrix = load_data('adjacency_matrix.json')
constraints = load_data('constraints.json')
sequences = load_data('sequences.json')

# Get all parts and first detail
all_parts = adjacency_matrix['allParts']
first_detail = adjacency_matrix['firstDetail']

# Filter valid sequences
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 valid sequences to file
save_sequences(valid_sequences, 'valid_sequences.json')

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