# Logic Gates Manually

## Python 2, 38 bytes

```
lambda s:sum(map(ord,s))*3%61%37%9%7%2
```

Try it online!

A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with `0 1`

and `1 0`

give the same result, which is works out because all the logic gates used are symmetric.

The `*3`

separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.

A shorter solution is surely possible using `hash(s)`

or `id(s)`

, but I avoided these because they are system-dependent.

## Python 2, 50 bytes

```
lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1
```

Try it online!

A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking `(s*9)[35]`

, which are all distinct. For `OR`

, this winds up reading one of the bits so the character could be `0`

or `1`

, but it turns out to work to check if it's `0`

, and a `1`

will correctly give a `1`

result anyway.

## JavaScript (ES6), 39 bytes

```
s=>341139>>parseInt(btoa(s),34)%86%23&1
```

Try it online!

### How?

We can't parse spaces with `parseInt()`

, no matter which base we're working with. So we inject a base-64 representation of the input string instead. This may generate `=`

padding characters (which can't be parsed with `parseInt()`

either), but these ones are guaranteed to be located at the end of the string and can be safely ignored.

We parse as base \$34\$ and apply a modulo \$86\$, followed by a modulo \$23\$, which gives the following results. This includes inaccuracies due to loss of precision. The final result is in \$[0..19]\$, with the highest truthy index being at \$18\$, leading to a 19-bit lookup bitmask.

```
input | to base-64 | parsed as base-34 | mod 86 | mod 23 | output
------------+----------------+-------------------+--------+--------+--------
"AND 0 0" | "QU5EIDAgMA==" | 1632500708709782 | 26 | 3 | 0
"AND 0 1" | "QU5EIDAgMQ==" | 1632500708709798 | 42 | 19 | 0
"AND 1 0" | "QU5EIDEgMA==" | 1632500708866998 | 34 | 11 | 0
"AND 1 1" | "QU5EIDEgMQ==" | 1632500708867014 | 50 | 4 | 1
"OR 0 0" | "T1IgMCAw" | 1525562056532 | 52 | 6 | 0
"OR 0 1" | "T1IgMCAx" | 1525562056533 | 53 | 7 | 1
"OR 1 0" | "T1IgMSAw" | 1525562075028 | 58 | 12 | 1
"OR 1 1" | "T1IgMSAx" | 1525562075029 | 59 | 13 | 1
"XOR 0 0" | "WE9SIDAgMA==" | 1968461683492630 | 48 | 2 | 0
"XOR 0 1" | "WE9SIDAgMQ==" | 1968461683492646 | 64 | 18 | 1
"XOR 1 0" | "WE9SIDEgMA==" | 1968461683649846 | 56 | 10 | 1
"XOR 1 1" | "WE9SIDEgMQ==" | 1968461683649862 | 72 | 3 | 0
"NAND 0 0" | "TkFORCAwIDA=" | 61109384461626344 | 62 | 16 | 1
"NAND 0 1" | "TkFORCAwIDE=" | 61109384461626350 | 70 | 1 | 1
"NAND 1 0" | "TkFORCAxIDA=" | 61109384461665650 | 64 | 18 | 1
"NAND 1 1" | "TkFORCAxIDE=" | 61109384461665656 | 72 | 3 | 0
"NOR 0 0" | "Tk9SIDAgMA==" | 1797025468622614 | 76 | 7 | 1
"NOR 0 1" | "Tk9SIDAgMQ==" | 1797025468622630 | 6 | 6 | 0
"NOR 1 0" | "Tk9SIDEgMA==" | 1797025468779830 | 84 | 15 | 0
"NOR 1 1" | "Tk9SIDEgMQ==" | 1797025468779846 | 14 | 14 | 0
"XNOR 0 0" | "WE5PUiAwIDA=" | 66920415258533864 | 0 | 0 | 1
"XNOR 0 1" | "WE5PUiAwIDE=" | 66920415258533870 | 8 | 8 | 0
"XNOR 1 0" | "WE5PUiAxIDA=" | 66920415258573170 | 2 | 2 | 0
"XNOR 1 1" | "WE5PUiAxIDE=" | 66920415258573176 | 10 | 10 | 1
```

### CJam (13 bytes)

```
q1bH%86825Yb=
```

Assumes that input is without a trailing newline.

Online test suite

This is just a simple hash which maps the 24 possible inputs into 17 distinct but consistent values and then looks them up in a compressed table.

### Python 2 (36 bytes)

```
lambda s:76165>>sum(map(ord,s))%17&1
```

This is just a port of the CJam answer above. Test suite using xnor's testing framework.