SWI-Prolog, 62 4747 41 bytes
*(X,Y)X*Y:-X+Y;Y+X;X==Y.
?(X,?Y):-not(X*Y),X*Z,Y*Z.
Prolog isn't too often useful, but when it is it's just beautiful. We'll use a+b
to notate that a
is friends with b
, a*b
that a
knows b
and a?b
that b
should be suggested to a
or not. The first line simply says that X*Y
is true if either X+Y
, Y+X
or X == Y
is true. This implements the symmetry of knowing eachother. Asking if there should be a suggestion is incredibly simple. We just ask if there is a Z
such that X*Y
is false and X*Z
and Y*Z
is true. Exactly as described in the challenge.
If you save this as a file (e.g. friends.pl
) and open SWI-Prolog with this file (prolog -l friends.pl
) you get dropped into a REPL.
You can assert friendships like this:
assert('a' + 'b').
assert('a' + 'c').
assert('b' + 'd').
You can check if people know eachother or suggestion should be made:
'a'*'b'.
'a'?'d'.