from itertools import product answers = ['A', 'B', 'C', 'D'] all_possible_combinations = list(product(answers, repeat=10)) def check_conditions(combination): if combination[1] == 'A' and combination[4] != 'C': return False if combination[1] == 'B' and combination[4] != 'D': return False if combination[1] == 'C' and combination[4] != 'A': return False if combination[1] == 'D' and combination[4] != 'B': return False if combination[2] == 'A': if combination[5] != combination[2]: return False if combination[2] == 'B': if combination[4] != combination[5]: return False if combination[2] == 'C': if combination[3] != combination[4]: return False if combination[2] == 'D': if combination[2] != combination[3]: return False if combination[3] == 'A': index_a = answers.index(combination[2]) index_b = answers.index(combination[9]) if abs(index_a - index_b) != 1: return False if combination[3] == 'B': index_a = answers.index(combination[5]) index_b = answers.index(combination[7]) if abs(index_a - index_b) != 1: return False if combination[3] == 'C': index_a = answers.index(combination[0]) index_b = answers.index(combination[4]) if abs(index_a - index_b) != 1: return False if combination[3] == 'D': index_a = answers.index(combination[1]) index_b = answers.index(combination[8]) if abs(index_a - index_b) != 1: return False if combination[4] == 'A': if combination[4] != combination[6]: return False if combination[4] == 'B': if combination[4] != combination[5]: return False if combination[4] == 'C': if combination[4] != combination[3]: return False if combination[4] == 'D': if combination[4] != combination[2]: return False if combination[5] == 'A': if combination[9] != combination[1]: return False if combination[5] == 'B': if combination[7] != combination[9]: return False if combination[5] == 'C': if combination[5] != combination[7]: return False if combination[5] == 'D': if combination[3] != combination[5]: return False answer_counts = {answer: combination.count(answer) for answer in answers} least_selected = min(answer_counts, key=answer_counts.get) if combination[6] == 'A' and least_selected != 'B': return False if combination[6] == 'B' and least_selected != 'A': return False if combination[6] == 'C' and least_selected != 'D': return False if combination[6] == 'D' and least_selected != 'C': return False most_selected = max(answer_counts, key=answer_counts.get) if combination[7] == 'A': for i in range(0, 9): if combination[i] == most_selected: if i == 2: return False if i == 4: return False if combination[7] == 'B': for i in range(0, 9): if combination[i] == most_selected: if i == 0: return False if i == 2: return False if combination[7] == 'C': for i in range(0, 9): if combination[i] == most_selected: if i == 4: return False if i == 6: return False if combination[7] == 'D': for i in range(0, 9): if combination[i] == most_selected: if i == 6: return False if i == 8: return False x2 = answers.index(combination[1]) x4 = answers.index(combination[3]) x10 = answers.index(combination[9]) X = x2 - (x4 - x10) if combination[8] == 'A': if X != answers.index(combination[7]): return False if combination[8] == 'B': if X != answers.index(combination[8]): return False if combination[8] == 'C': if X != answers.index(combination[1]): return False if combination[8] == 'D': if X != answers.index(combination[4]): return False frequency_difference = answer_counts[most_selected] - answer_counts[least_selected] if combination[9] == 'A' and frequency_difference != 4: return False if combination[9] == 'B' and frequency_difference != 3: return False if combination[9] == 'C' and frequency_difference != 2: return False if combination[9] == 'D' and frequency_difference != 1: return False return True for combination in all_possible_combinations: if check_conditions(combination): valid_answer = combination print(valid_answer)