Bug in nand() fixed.
This is how you solve a logic puzzle.

(Not as much fun as by hand but still impressive.)