# Exercice 2 : ET à quatre entrées, deux bits de travail remis à 0
from itertools import product

# Chaque étape est un Toffoli : (cible, contrôle 1, contrôle 2)
STEPS = [
    ("w1", "a", "b"),    # w1 = a ET b
    ("w2", "w1", "c"),   # w2 = a ET b ET c
    ("out", "w2", "d"),  # out = a ET b ET c ET d
    ("w2", "w1", "c"),   # décalcul de w2
    ("w1", "a", "b"),    # décalcul de w1
]

all_ok = True
for a, b, c, d in product((0, 1), repeat=4):
    bits = {"a": a, "b": b, "c": c, "d": d, "w1": 0, "w2": 0, "out": 0}
    for target, x, y in STEPS:
        bits[target] ^= bits[x] & bits[y]
    all_ok &= bits["out"] == (a & b & c & d) and bits["w1"] == 0 and bits["w2"] == 0

print("Toffoli utilisés :", len(STEPS))
print("16 entrées : out correct et w1 = w2 = 0 :", all_ok)
