Build an adding machine using NAND logic gates This is "programming" at its most fundamental.

Build a diagram of (two-wire) NAND logic gates that will take the input wires A1, A2, A4, A8, B1, B2, B4, B8, representing two binary numbers A to B from 0 to 15, and return values on the output wires C1, C2, C4, and C8 representing C, which is the sum of A and B modulo 16.

Your score is determined by the number of NAND gates (1 point per gate). To simplify things, you may use AND, OR, NOT, and XOR gates in your diagram, with the following corresponding scores:


NOT: 1
AND: 2
OR: 3
XOR: 4


Each of these scores corresponds to the number of NAND gates that it takes to construct the corresponding gate.

Lowest score wins.



Updates:

03-07 19:09: Due to discussions with Jan Dvorak, it came to my attention that the total number of gates, constants and splits is always determined solely by the number of gates. Because of this, I have simplified the score requirement back down to simply the number of gates required.
31 gates

[created in Logisim]

The standard XOR-gate can be built with four NANDs. Tapping the first NAND provides us with a fairly compact half-adder with inverted carry:
hadder(A,B) =&gt; (sum, iCarry):

X = A^B   

sum = (A^X)^(B^X)
iCarry = X


These can be combined into a 9-gate full adder:
fadder(A,B,C) =&gt; (sum, carry):

DD1 = hadder(A,B)
DD2 = hadder(DD1.sum, C)

sum = DD2.sum
carry = DD1.iCarry ^ DD2.iCarry

These combined yield a naive 36-gate four-bit adder. Not bad.
Now, we inline all modules and perform these optimizations:
delete gates with no output (only the final gate)
0^x =&gt; 1                    (one gate, twice)
1^(1^x) =&gt; 0                (two gates, once)

for a grand total of 31 gates.

Note that the count of splits and NAND-gates are intimately connected: Each 2-input NAND (larger are explicitly forbidden) takes two inputs and produces one output. Each 2-way split (let's assume larger splits are forbidden) takes one input and produces two outputs. Also, there are no unused outputs or floating inputs (let's assume constants are not allowed). If there are N more inputs than outputs, there must be N more NANDs than splits.
Thus, a 31-NAND 8=&gt;4 module will neccessarily have 27 2-way splits.
Here are the split cardinalities for the above adder just in case larger splits are allowed:
18x wire (no split)
  4x output
  14x XOR 2nd stage
16x 2-way split
  8x input
  6x H2 input
  2x last bit XOR 1st stage
4x  3-way split
  4x XOR 1st stage
1x  4-way split
  1x 1st bit XOR 1st stage; if constants are alloweed; this is a three-way split