is there a way to have some restriction when adding product to cart ?

let's say i have 3 types of product: A, B and C
i can add A and C to the same cart, but if i have A or C, i can"t add B

any idea on how to acheive this ?