80
\$\begingroup\$

For example, the gate A and B is a logic gate with 2 inputs and 1 output.

There are exactly 16 of them, because:

  • each logic gate takes two inputs, which can be truthy or falsey, giving us 4 possible inputs
  • of the 4 possible inputs, each can have an output of truthy and falsey
  • therefore, there are 2^4 possible logic gates, which is 16.

Your task is to write 16 programs/functions which implement all of them separately.

Your functions/programs must be independent.

They are valid as long as they output truthy/falsey values, meaning that you can implement A or B in Python as lambda a,b:a+b, even if 2 is produced for A=True and B=True.

Score is total bytes used for each function/program.

List of logic gates

  1. 0,0,0,0 (false)
  2. 0,0,0,1 (and)
  3. 0,0,1,0 (A and not B)
  4. 0,0,1,1 (A)
  5. 0,1,0,0 (not A and B)
  6. 0,1,0,1 (B)
  7. 0,1,1,0 (xor)
  8. 0,1,1,1 (or)
  9. 1,0,0,0 (nor)
  10. 1,0,0,1 (xnor)
  11. 1,0,1,0 (not B)
  12. 1,0,1,1 (B implies A)
  13. 1,1,0,0 (not A)
  14. 1,1,0,1 (A implies B)
  15. 1,1,1,0 (nand)
  16. 1,1,1,1 (true)

Where the first number is the output for A=false, B=false, the second number is the output for A=false, B=true, the third number is the output for A=true, B=false, the fourth number is the output for A=true, B=true.

Leaderboard

var QUESTION_ID=82938,OVERRIDE_USER=48934;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+(?:\.\d+)?)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
26
  • 2
    \$\begingroup\$ Your functions/programs may share code. What does this mean? Also, may the programs be in different languages? \$\endgroup\$
    – Lynn
    Jun 14, 2016 at 23:36
  • 2
    \$\begingroup\$ I find the explanation confusing: "of the 4 possible inputs each can have and output of truthy and falsy". Doesn't this imply 8 (4*2) states? \$\endgroup\$
    – DavidC
    Jun 14, 2016 at 23:50
  • 4
    \$\begingroup\$ The names you're missing are the AND-NOT gates (A AND NOT B and B AND NOT A). \$\endgroup\$
    – user45941
    Jun 15, 2016 at 1:33
  • 20
    \$\begingroup\$ So it happened again. There are 18 answer, mostly simple and correct, then out of nowhere the question became "unclear what you're asking". I you don't like a challenge, go on, take another, do not close it! \$\endgroup\$
    – edc65
    Jun 15, 2016 at 17:25
  • 5
    \$\begingroup\$ @dorukayhan See: vacuous truth \$\endgroup\$
    – Sp3000
    Jun 15, 2016 at 21:19

94 Answers 94

4
\$\begingroup\$

Actually, 24 bytes

These programs take input as A\nB (with \n representing a newline), which leaves B on top of the stack, with A below. False is represented by 0, and True is represented by any positive integer.

é0  (false: clear stack, push 0)
*   (and: multiply)
<   (A and not B: less-than)
X   (A: discard B)
>   (B and not A: greater-than)
@X  (B: discard A)
^   (A xor B: xor)
|   (A or B: or)
|Y  (A nor B: or, boolean negate)
=   (A xnor B: equals)
@XY (not B: discard A, boolean negate B)
≤   (if B then A: less-than-or-equal)
XY  (not A: discard B, boolean negate)
≥   (if A then B: greater-than-or-equal)
*Y  (A nand B: multiply, boolean negate)
é1  (true: clear stack, push 1)

Thanks to Leaky Nun for 3 bytes

\$\endgroup\$
0
4
\$\begingroup\$

C, 144 bytes

This builds on @Emmanuel's idea to use the function number as the array of bits containing the required output. However, it adds some preprocessor abuse to actually define those 16 freestanding functions required by the question. It is also this preprocessor abuse that makes this the shortest C answer to date.

#define z(n)(a,b){return (n>>3-b-2*a)&1;}
a z(0)b z(1)c z(2)d z(3)e z(4)f z(5)g z(6)h z(7)i z(8)j z(9)k z(10)l z(11)m z(12)n z(13)o z(14)p z(15)

Using the preprocessor, the token sequence a z(0) expands to

a (a,b){return (0>>3-b-2*a)&1;}

which is the required free-standing function a(a,b) returning false. The other functions are defined analogously.

Test the code with

#include <stdio.h>

#define z(n)(a,b){return (n>>3-b-2*a)&1;}
a z(0)b z(1)c z(2)d z(3)e z(4)f z(5)g z(6)h z(7)i z(8)j z(9)k z(10)l z(11)m z(12)n z(13)o z(14)p z(15)

void showFunction(int(*function)(int,int)) {
    printf("%d %d %d %d\n", function(0,0), function(0,1), function(1,0), function(1,1));
}

int main() {
    showFunction(a);
    showFunction(b);
    showFunction(c);
    showFunction(d);
    showFunction(e);
    showFunction(f);
    showFunction(g);
    showFunction(h);
    showFunction(i);
    showFunction(j);
    showFunction(k);
    showFunction(l);
    showFunction(m);
    showFunction(n);
    showFunction(o);
    showFunction(p);
}
\$\endgroup\$
4
\$\begingroup\$

Nibbles, 28 nibbles = 14 bytes

Edit: -1 nibble (0.5 bytes) thanks to xigoi

false % = 1 nibble screenshot
and * = 1 nibble screenshot
a not b - = 1 nibble screenshot
b not a -@ = 2 nibbles screenshot
a = 0 nibbles screenshot
b @ = 1 nibble screenshot
xor `^ = 2 nibbles screenshot
or + = 1 nibble screenshot
nor -~+ = 3 nibbles screenshot
xnor -~`^ = 4 nibbles screenshot
not b -~@ = 3 nibbles screenshot
not a -~ = 2 nibbles screenshot
b imp a ^ = 1 nibbles screenshot
a imp b ^@ = 2 nibbles screenshot
nand -~* = 3 nibbles screenshot
true _ = 1 nibble screenshot

total 28 nibbles = 14 bytes

In nibbles, positive integers are truthy, zero and negative integers are falsy.

All of the solutions work as full programs, and all except 'true' also work as final functions within a program. Note that the screenshots (except 'true') illustrate each solution used as a function mapped over all 4 possible inputs. The code for the map/fold to do this (.$/$) is itself 4 nibbles (=2 bytes), so the code-size indicated in the screenshots in these cases is always 2 bytes more than the size of the function itself.

Explanation

General points

  • The two inputs to each function/program are automatically stored in the variables $ (input 1) and @ (input 2). If we don't specify the input(s) to use, Nibbles implicitly appends $ and then, if required @ to the end of the program. So, for instance 'and' (*) automatically expands to *$@, meaning $ multiplied by @ in prefix notation.
  • Decimal numbers are expensive in Nibbles, since they cannot readily be compressed, and so count 2 or 3 nibbles each. So we try to avoid them wherever possible. This can be by using a function that always evaluates to a single value (see 'false'), but most functions also have a 'default' argument value designated by the 1-nibble ~ code: the default value for 'minus' (-) is 1, which means we can conveniently use -~ (one minus the argument) to negate without needing to include the explicit digit 1.

Each logic gate

  • false % = A modulo B = obviously any integer modulo 1 is zero, and fortunately Nibbles also returns zero for any number modulo 0. So this function always returns zero, and is shorter than the 2-nibble but more-intuitive program of simply 0.
  • and * = A multiplied by B.
  • a not b - = A minus B = exploits the fact that zero and negative integers are all falsy in Nibbles.
  • b not a -@ = B minus A.
  • a = A = $ (input 1) is implicitly added to the end of the function/program.
  • b @ = B = input 2.
  • xor `^ = A bitwise xor B.
  • or + = A plus B = exploits the fact that all positive integers are truthy in Nibbles. A more-intuitive program might be ] (maximum of two values), but this shares the same 1-nibble opcode as +: since both functions are commutative the choice of function is determined by the order of its operands, and we'd need ]@$ to specify 'max', costing 2 nibbles more.
  • nor -~+ = one minus (A plus B) = uses ~ to specify the default value of minus, which is 1.
  • xnor -~`^ = one minus (bitwise xor of A and B).
  • not a -~ = one minus A (implicit $ added to the end of the function/program).
  • not b -~@ = one minus B.
  • b imp a ^ = A to the power of B = fortunately Nibbles assigns a value of 1 to zero-to-the-power-of-zero.
  • a imp b ^@ = B to the power of A.
  • nand -~* = one minus (A times B).
  • true _ = third argument, or STDIN if only two arguments are given, or (used here) value of 100 if STDIN is empty.
\$\endgroup\$
2
  • \$\begingroup\$ For TRUE, you can use _, which always outputs 100 if given two arguments and STDIN is empty. \$\endgroup\$
    – xigoi
    Jan 26, 2023 at 23:25
  • \$\begingroup\$ @xigoi - Thanks a lot! That's brilliant! \$\endgroup\$ Jan 26, 2023 at 23:55
3
\$\begingroup\$

Forth-83, 43 37 bytes

These are all complete programs. The inputs begin on the stack, and the result of each program is the top value of the stack afterwards. Not all of these programs are stack-safe. I wish there was a shorter way to NOT (1+ is shorter.)

Note: -1 is TRUE in this language (and many older languages,) because a bitwise NOT of 0 is -1. Any non-zero value is truthy.

0000    0
0001    *
0010    <
0011    DROP
0100    >
0101    
0110    -
0111    +
1000    + 1+
1001    =
1010    1+
1011    > 1+
1100    DROP 1+
1101    < 1+
1110    * 1+
1111    1

Test code:

Replace DROP on line 2 with your program of choice. This program runs the above programs for each combination of inputs and prints whether it was interpreted to be true or false (some of the truthy values are different, but this test program shows that they are still interpreted as true.) Any occurrence of NOT must be replaced with INVERT for this interpreter, because this isn't a Forth‑83 interpreter. The new lines here are for readability, and can be replaced with spaces.

Try it online

: f 
DROP
IF -1 . ELSE 0 . THEN ;

0 0 f
0 -1 f
-1 0 f
-1 -1 f
\$\endgroup\$
6
  • \$\begingroup\$ Replace DROP with 0*+ and NOT with 1-, if I am correct. \$\endgroup\$
    – Leaky Nun
    Jun 16, 2016 at 20:24
  • \$\begingroup\$ Also, why do you use -1 instead of 1 as inputs? \$\endgroup\$
    – Leaky Nun
    Jun 16, 2016 at 20:26
  • \$\begingroup\$ Spaces are required, so that's not shorter. The language uses -1 as true, as was standard in older languages, since a bitwise NOT of 0 is -1. I tested most of them with 1 as well, and I think they work, though < and > might be backwards in that case. \$\endgroup\$
    – mbomb007
    Jun 16, 2016 at 20:33
  • \$\begingroup\$ I can use 1+ instead of NOT, though. Thanks. \$\endgroup\$
    – mbomb007
    Jun 16, 2016 at 20:35
  • \$\begingroup\$ Does 0*+ work as DROP? \$\endgroup\$
    – Leaky Nun
    Jun 17, 2016 at 1:57
3
\$\begingroup\$

Stackylogic, 106 105 bytes (non-competing)

Sort of what this language is made for.

It was made after the challenge, so non-competing

  • 1 moves the pointer down one, and removes itself

  • 0 moves the pointer up one, and removes itself

  • ? is substituted for a bit from input, which is executed as above.

  • < just denotes the start of the pointer

0000 false              2 bytes:0<

0001 p and q            4 bytes:?<
                                ?

0010 p and not q        7 bytes:1?<
                                ?
                                0

0011 p                  2 bytes:?<

0100 not p and q        6 bytes:?
                                ?<
                                0

0101 q                  6 bytes:?
                                ?<
                                ?

0110 xor               11 bytes:?
                                ?<
                                11
                                ?
                                0

0111 p or q             4 bytes:?
                                ?<

1000 not p and not q   11 bytes:1
                                ?
                                0?<
                                1
                                0

1001 xnor              11 bytes:1
                                ?
                                0?<
                                1
                                ?

1010 not q              9 bytes:11
                                ??<
                                00

1011 p or not q         7 bytes:1
                                ?
                                0?<

1100 not p              6 bytes:1
                                ?<
                                0

1101 not p or q         6 bytes:1
                                ?<
                                ?

1110 not p or not q    11 bytes:1
                                ?<
                                11
                                ?
                                0

1111 true               2 bytes: 1<
\$\endgroup\$
2
  • \$\begingroup\$ I think not q can be shortened by a byte: 11/??</00 \$\endgroup\$ Jul 22, 2016 at 15:11
  • \$\begingroup\$ why do I have more upvotes on this than this? ಠ____ಠ \$\endgroup\$ Jul 22, 2016 at 15:28
3
\$\begingroup\$

PowerShell, 231 bytes

These are all full scripts, with arguments passed as either Boolean values ($true or $false) or integers (0 or 1 for false and true, respectively) on the command line.

  1. 0000 is pretty simple, by which I mean it's completely blank. Null coerces to $false.

     
    
  2. 0001 multiplies the two values together; $true coerces to 1, $false to 0.

    $args[0]*$args[1]
    
  3. 0010 inverts B by subtracting 1 from it, then does the same as the previous. The integer -1 coerces to $true.

    $args[0]*($args[1]-1)
    
  4. 0011 just returns A.

    $args[0]
    
  5. 0100 does the same as #3, just flipped.

    ($args[0]-1)*$args[1]
    
  6. 0101 just returns B.

    $args[1]
    
  7. 0110 subtracts B from A, again using the fact that any nonzero value becomes $true.

    $args[0]-$args[1]
    
  8. 0111 adds the two arguments together.

    $args[0]+$args[1]
    
  9. 1000 adds the two and makes sure the result is still zero.

    $args[0]+$args[1]-eq0
    
  10. 1001 makes sure the values are the same.

    $args[0]-eq$args[1]
    
  11. 1010 subtracts 1 from B.

    $args[1]-1
    
  12. 1011 shifts B to the left, adds A, and subtracts two. If the result is zero, we found the one unacceptable configuration.

    $args[1]*2+$args[0]-2
    
  13. 1100 inverts A with the same trick as #11.

    $args[0]-1
    
  14. 1101 is the same as #12 but with the arguments flipped.

    $args[0]*2+$args[1]-2
    
  15. 1110 does "and" and then inverts it.

    $args[0]*$args[1]-1
    
  16. 1111 is just $true.

    1
    
\$\endgroup\$
3
\$\begingroup\$

TI-Basic, 108 66 bytes

With no arguments, Input gets input into X and Y.

0000 false            1 0
0001 and              4 Input :XY
0010 x and not y      5 Input :Xnot(Y
0011 x                3 Input :X
0100 not x and y      5 Input :Ynot(X
0101 y                3 Input :Y
0110 xor              5 Input :X-Y
0111 or               5 Input :X+Y
1000 nor              6 Input :not(X+Y
1001 xnor             5 Input :X=Y
1010 not y            4 Input :not(Y
1011 x or not y       5 Input :X≥Y
1100 not x            4 Input :not(X
1101 not x or y       5 Input :Y≤X
1110 nand             5 Input :not(XY
1111 true             1 1
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 31 27 26 bytes

First input is p.
Second input is q.

0000 false              0          # ignore input, output 0 (false)
0001 p and q            &          # p and q
0010 p and not q        s±&        # (not q) and p
0011 p                  ¹          # p
0100 not p and q        ±&         # (not p) and q
0101 q                  ²          # q
0110 xor                ^          # p xor q
0111 p or q             ~          # p or q
1000 not p and not q    +_         # (p + q) == 0
1001 eq                 Q          # p == q
1010 not q              ²_         # not q
1011 p or not q         s_~        # (not q) or p
1100 not p              _          # not p
1101 not p or q         _~         # (not p) or q
1110 not p or not q     +2‹        # (p + q) < 2
1111 true               1          # ignore input, output 1 (true)

Saved 4 bytes thanks to Adnan
Saved 1 byte thanks to Magic Octopus Urn

\$\endgroup\$
0
3
\$\begingroup\$

Cubically, 317 304 295 269 257 219 187 128 119 110 bytes

-13, 11, 12 thanks to TehPers
-~200 realizing output must be truthy, not 1
-66 thanks to language updates

0000 false              %
0001 p and q            $:$·7%
0010 p and not q        $?7{$=%}!%7
0011 p                  $%7
0100 not p and q        $?7%0!$%7
0101 q                  $$%7
0110 xor                $:$⊕7%
0111 p or q             $:$|7%
1000 not p and not q    $?7{%&}$?7{%&}%1
1001 eq                 $:$=%
1010 not q              $$!7B%0
1011 p or not q         $?7B$!7B%0
1100 not p              $=%
1101 not p or q         $!7B$?7B%0
1110 not p or not q     $!7B$!7B%0
1111 true               %1

0000, 2 bytes - TIO

%0

Simply prints the sum of the UP face, which is initialized to all zero's. Prints zero.

0001, 24 11 8 7 6 bytes - TIO

$:$·7%
$          read input
 7         set notepad to input
   $       read input
    ·7     set notepad to notepad AND input
      %    print notepad as integer

Older version:

$!7{%0&}$!7{%0&}R3D1R1%0
$                            read input
 !7{...}                     if falsy
    %0                        print 0th face sum (0)
      &                       exit
        $                    read input
         !7{...}             if falsy
            %0&               print 0th face sum (0) and exit
                R3D1R1       get the 0th face sum to 1
                      %0     print as integer (1)

Credit to TehPers for the shortened algorithm to get the 0th face sum to 1; before today it had been about 12 bytes.

0010, 24 13 12 11 bytes - TIO

$?7{$=%}!%7
$            read input
 ?7{...}     if truthy
    $         read input
     =        compare to previous
      %      print result
        !    otherwise
         %7  print input (falsy)

0011, 16 3 bytes - TIO

$%7
$    read input
 %7  and print it

0100, 24 12 11 9 bytes - TIO

$?7%0!$%7
$           read input (into 7th face)
 ?7         if 7th face truthy
   %0        print 0
     !      otherwise
      $      read input
       %7   print input

I don't actually have any idea how this works...

0101, 17 4 bytes - TIO

$$%7
$$    read input, discard, read input
  %7  and print

0110, 21 8 7 6 bytes - TIO

$:7$⊕7%
$         read input
 :7       set notepad to input
   $      read input
    ⊕7    set notepad to notepad XOR input
      %   print notepad

Old version:

$:7$=7R3D1R1?6-0!+0%6
$:7                    read input, set notepad to input
   $=7                 read input, compare with previous input, store result in notepad
      R3D1R1           get 0th face sum to 1
            ?6-0!+0    swap result
            ?6         if notepad is truthy (implicit curly-braces)
              -0        subtract one from notepad
                !      else
                 +0     add one to notepad
                   %6  print notepad

0111, 30 8 7 6 bytes - TIO

$:$|7%
$        read input
 :       set notepad to input
  $      read input
   |7    set notepad to notepad OR input
     %   print notepad

Old version:

R3D1R1$?7{%0&}$?7{%0&}R3D3R1%0
R3D1R1                          get 0th face sum to 1
      $                         read input
       ?7{...}                  if truthy
          %0&                    print 1 and exit
              $?7{%0&}          read input - if truthy, print 1 and exit
                      R3D3R1    get 0th face sum to 0
                            %0  print 0

1000, 24 18 bytes - TIO

Pretty much the same as the other 24-byte gates.

1001, 8 7 bytes - TIO

$:$=%
$:       read input, set notepad to input
  $=     read input, compare to previous
    %    print comparison result

1010, 17 7 bytes - TIO

$$!7B%0
$$        read input, discard, read again
  ?7.     if truthy
    B      get top face to nonzero
      %0  print top face sum

1011, 24 18 10 bytes - TIO

$?7B$!7B%0
$           read input
 ?7.        if truthy
   B         get top face sum to truthy
    $       read input
     !7.    if falsy
       B     get top face sum to truthy
        %0  print top face sum

Old version:

$?7{%1&}$!7{%1&}%0
$                    read input
 ?7{...}             if truthy
    %1                print 9
      &              exit
        $            read input
         !7{...}     if falsy
            %1&       print 9 and exit
                %0   print 0

1100, 13 5 4 bytes - TIO

$=%
$      read input
 =     set notepad to (input == 0)
  %    print notepad

Old version:

$!7{R3D1R1}%0
$               read input
 !7{......}     if falsy
    R3D1R1       get 0th face sum to 1
           %0   print 0th face (1 if input was falsy, 0 if input was truthy)

1101, 24 10 bytes - TIO

$!7B$?7B%0
$           read input
 !7.        if falsy
   B         get top face sum to truthy
    $       read input
     ?7.    if truthy
       B     get top face sum to truthy
        %0  print top face sum

1110, 24 10 bytes - TIO

Pretty much the same as 1101, but the second if-statement is an if falsy.

1111, 8 4 2 bytes - TIO

%1  print face sum of left face (9)

Old versions:

=6%6
=6    set notepad to (0 == 0)
  %6  print notepad

and...

R3D1R1%0
R3D1R1    get 0th face sum to 1
      %0  print 1
\$\endgroup\$
1
  • \$\begingroup\$ Why don't you do not q similar to not p? $$=7%6 (6 bytes) and not p and q similar to not p and p and q? $=7$·7%6 (8 bytes) \$\endgroup\$
    – DELETE_ME
    Sep 6, 2017 at 10:15
3
\$\begingroup\$

MMIX, 128 bytes (32 instrs)

(v0: only 0/1 input, 0/1 output; uncommented lines are "return a")

false   POP  0,0        // return 0
and     ZSNZ $0,$1,$0   // if(!b) a = 0
        POP  1,0
nimp    ZSZ  $0,$1,$0   // if(b) a = 0
        POP  1,0
a       POP  1,0
ncimp   ZSZ  $0,$0,$1   // a = a? 0 : b
        POP  1,0
b       POP  2,0        // return b
xor     XOR  $0,$0,$1   // a ^= b
        POP  1,0
or      CSNZ $0,$1,1    // if(b) a = 1
        POP  1,0
nor     ZSZ  $0,$0,1    // a = !a
        ZSZ  $0,$1,$0   // if(b) a = 0
        POP  1,0
xnor    ZSZ  $255,$0,1  // t = !a
        CSZ  $0,$1,$255 // if(!b) a = t
        POP  1,0
notb    ZSZ  $0,$1,1    // a = !b
        POP  1,0
cimp    CSZ  $0,$1,1    // if(!b) a = 1
        POP  1,0
nota    ZSZ  $0,$0,1    // a = !a
        POP  1,0
impl    CSZ  $1,$0,1    // if(!a) b = 1
        POP  2,0        // return b
nand    ZSZ  $0,$0,1    // a = !a
        CSZ  $0,$1,1    // if(!b) a = 1
        POP  1,0
true    SET  $0,1       // a = 1
        POP  1,0

(v1: unrestricted input; 132 bytes [33 instrs])
Generally same as v0, except:

xor     ZSZ  $255,$0,1  // t = !a
        CSNZ $0,$1,$255 // if(b) a = t
        POP  1,0        // return a

(v2: 64 bits in parallel; 118 bytes [29 instrs])

false   POP  0,0        // return 0
and     AND  $0,$0,$1   // a &= b
        POP  1,0
nimp    ANDN $0,$0,$1   // a &= ~b
        POP  1,0
a       POP  1,0
ncimp   ANDN $0,$1,$0   // a = b & ~a
        POP  1,0
b       POP  2,0        // return b
xor     XOR  $0,$0,$1   // a ^= b
        POP  1,0
or      OR   $0,$0,$1   // a |= b
        POP  1,0
nor     NOR  $0,$0,$1   // a = ~(a | b)
        POP  1,0
xnor    NXOR $0,$0,$1   // a ^= ~b
        POP  1,0
notb    NXOR $0,$1,0    // a = ~b
        POP  1,0
cimp    ORN  $0,$0,$1   // a |= ~b
        POP  1,0
nota    NXOR $0,$1,0    // a = ~a
        POP  1,0
impl    ORN  $0,$1,$0   // a = b | ~a
        POP  1,0
nand    NAND $0,$0,$1   // a = ~(b & a)
        POP  1,0
true    NEG  $0,0,1     // a = -1
        POP  1,0
\$\endgroup\$
3
\$\begingroup\$

Piet + ascii-piet, 180 bytes (180 codels)

All Programs take two space separated inputs in the form: falsey = 0, truthy = 1

All Programs output in the form: falsey <= 0, truthy >= 1

  1. false - Try Online! - 8 bytes (2×4=8 codels)
tlrN  nn
  1. and - Try Online! - 10 bytes (2×5=10 codels)
tajsJ   jj
  1. A and not B - Try Online! - 10 bytes (2×5=10 codels)
tajcR   rr
  1. A - Try Online! - 6 bytes (2×3=6 codels)
taS ss
  1. not A and B - Try Online! - 16 bytes (2×8=16 codels)
tajbrlfT  j   tt
  1. B - Try Online! - 8 bytes (2×4=8 codels)
tajF  ff
  1. xor - Try Online! - 14 bytes (2×7=14 codels)
tajkcuI   k ii
  1. or - Try Online! - 10 bytes (2×5=10 codels)
tajkB   bb
  1. nor - Try Online! - 12 bytes (2×6=12 codels)
tajkuI    ii
  1. xnor - Try Online! - 16 bytes (2×8=16 codels)
tajkcufT   k  tt
  1. not B - Try Online! - 10 bytes (2×5=10 codels)
tajqK   kk
  1. B implies A - Try Online! - 14 bytes (2×7=14 codels)
tajbstM     mm
  1. not A - Try Online! - 8 bytes (2×4=8 codels)
talE  ee
  1. A implies B - Try Online! - 20 bytes (2×10=20 codels)
tajbrldvqK  j     kk
  1. nand - Try Online! - 12 bytes (2×6=12 codels)
tajseQ    qq
  1. true - Try Online! - 6 bytes (2×3=6 codels)
tlE ee

Explanations

  1. false
0               []
1   push    1   [1]
2   !           [0]
3   outN        []
  1. and
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   *           [r]
4   outN        []
  1. A and not B
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   -           [r]
4   outN        []
  1. A
0               []
1   inN     a   [a]
2   outN        []
  1. not A and B
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   push    2   [a b 2]
4   push    1   [a b 2 1]
5   roll        [b a]
6   -           [r]
7   outN        []
  1. B
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   outN        [a]
  1. xor
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   +           [x]
4   push    2   [x 2]
5   %           [r]
6   outN        []
  1. or
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   +           [r]
4   outN        []
  1. nor
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   +           [x]
4   !           [r]
5   outN        []
  1. xnor
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   +           [x]
4   push    2   [x 2]
5   %           [y]
6   !           [r]
7   outN        []
  1. not B
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   !           [a r]
4   outN        [a]
  1. B implies A
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   push    1   [a b 1]
4   -           [a x]
5   >           [r]
6   outN        []
  1. not A
0               []
1   inN     a   [a]
2   !           [r]
3   outN        []
  1. A implies B
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   push    2   [a b 2]
4   push    1   [a b 2 1]
5   roll        [b a]
6   push    1   [b a 1]
7   -           [b x]
8   >           [r]
9   outN        []
  1. nand
0               []
1   inN     a   [a]
2   inN     b   [a b]
3   *           [x]
4   !           [r]
5   outN        []
  1. true
0               []
1   push    1   [1]
2   outN        []
\$\endgroup\$
2
  • \$\begingroup\$ I suggest adding an answer with individual programs appearing in a single (common) rectangle. What is the minimal rectangle which can contain all programs? Can you overlap them? What if you are required to pack them into a square? \$\endgroup\$
    – anatolyg
    Oct 18, 2022 at 10:43
  • 2
    \$\begingroup\$ @anatolyg I don't see an advantage (besides some readability) in putting them all in a single rectangle, as the programs should be independent. Most programs share the first 3 codels for input, but for example in 16 I rely on having the first codel of size 1. Feel free to post your own answer with a different approach ;) \$\endgroup\$
    – sfieger
    Oct 18, 2022 at 10:52
2
\$\begingroup\$

Julia, 65 63 bytes

x\y=0>1
&
>
x\y=x
<
x\y=y
$
|
x\y=!x>y
==
x\y=!y
^
x\y=!x
<=
x\y=!x|!y
x\y=0<1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Brainfuck, 1757 bytes

This is my first golfing solution, please be gentle. :)

Inputs may be '0' (ASCII 48) or '1' (ASCII 49). Inputs are entered as AB (eg. 01 or 11). Truthy output values are 'T' (ASCII 84) or 'F' (ASCII 70).

1.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,<<<.

2.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>--[<]<<<.

3.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]+<[->-<]>[<+>-]<<[->>+<<]>[->+<]>--[<]<<<.

4.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,[-]++++++[-<-------->]+<[<.>->-<]>[<<<.>>>->]<<

5.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]+<<[->>-<<]>>[<<+>>-]<<[->>+<<]>[->+<]>--[<]<<<.

6.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],,>[-]++++++[-<-------->]+<[<.>->-<]>[<<<.>>>->]<<

7.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>-[<]<<<.

8.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>[<]<<<.

9.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>[<]<<<.

10.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>-[<]<<<.

11.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],,>[-]++++++[-<-------->]+<[<.>->-<]>[<<<.>>>->]<<

12.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]+<<[->>-<<]>>[<<+>>-]<<[->>+<<]>[->+<]>--[<]<<<.

13.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,[-]++++++[-<-------->]+<[<.>->-<]>[<<<.>>>->]<<

14.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]+<[->-<]>[<+>-]<<[->>+<<]>[->+<]>--[<]<<<.

15.

>+++++++[-<++++++++++++>]>+++++++[-<++++++++++>],>,>++++++[-<<-------->>]++++++[-<-------->]<<[->>+<<]>[->+<]>--[<]<<<.

16.

>+++++++[-<++++++++++>]>+++++++[-<++++++++++++>],>,<<.
\$\endgroup\$
4
  • \$\begingroup\$ I would advise you to write a function and assume 0 and 1 are already stored, and return 0 or 1 instead... \$\endgroup\$
    – Leaky Nun
    Jun 17, 2016 at 13:50
  • 2
    \$\begingroup\$ Nice answer! You could drastically shorten this by using ASCII 0 and 1 instead of '0' (48) and '1' (49). \$\endgroup\$
    – DJMcMayhem
    Jun 17, 2016 at 13:54
  • \$\begingroup\$ You could do something like ,>,<[->-<]+>[<->[-]]++++++[-<++++++++>]<. for 2. Similar simplification can most likely be made for all cases. \$\endgroup\$
    – Emigna
    Jun 17, 2016 at 14:09
  • 1
    \$\begingroup\$ @LeakyNun that is not what this community considers a "function", but just a snippet. The code cannot be reused without writing the same code again. Using byte I/O instead of ASCII is definitely fine though (and is actually closer to what brainfuck itself considers truthy or falsy). \$\endgroup\$ Jun 19, 2016 at 14:12
2
\$\begingroup\$

Java 8, 165 bytes

x->y->1<0;
x->y->x&y;
x->y->!y&x;
x->y->x;
x->y->!x&y;
x->y->y;
x->y->x!=y;
x->y->x|y;
x->y->!(x|y);
x->y->x==y;
x->y->!y;
x->y->!y|x;
x->y->!x;
x->y->!x|y;
x->y->!x|!y;
x->y->1>0;

Verify it! (Click "Compile" > Click "Execute")

\$\endgroup\$
4
  • \$\begingroup\$ Is currying in Java allowed? \$\endgroup\$ Aug 28, 2016 at 3:48
  • \$\begingroup\$ @NonlinearFruit Why not? \$\endgroup\$
    – Leaky Nun
    Aug 28, 2016 at 3:48
  • \$\begingroup\$ Since Java currying isn't simply f(a)(b) , does it count? You have to do something like curriedAdd.apply(4).applyAsInt(5) (example). I wasn't sure ¯_(ツ)_/¯ \$\endgroup\$ Aug 28, 2016 at 3:58
  • 1
    \$\begingroup\$ @NonlinearFruit Lambdas are allowed in Java and they use apply as well; as long as they are reusable then I don't see any problem \$\endgroup\$
    – Leaky Nun
    Aug 28, 2016 at 4:01
2
\$\begingroup\$

MarioLANG, 265 263 bytes

This is going to be a fairly long answer since this is a 2D language. Trailing newlines are significant wherever they appear.

Note that these programs were written and tested on Try it Online!. There are some things that can be done on the Ruby interpreter but not in TIO's. See Martin Ender's answer for the code golfed further making use of that.

False (2 bytes)

:
​

Outputs the initial value of the first cell, which is 0.

A and B (15 14 bytes)

;-
=[
+<;
:=:
​

Takes in A and check if it's 0. If it is, it outputs 0 and ends. Otherwise, it takes in B and outputs it.

A and not B (25 bytes)

;-
=[
 <;
 =-
++[<:
:===
​

Check if A is 1. If it is, outputs 0 and ends. Otherwise, takes in B and check if it's 1. If it is, outputs 0, if it isn't, outputs 1.

A (4 bytes)

;
:
​

Takes in A and outputs it.

not A and B (14 13 bytes)

;
=[
-<;
:=:
​

Takes in A and checks if it's 0. If it is, takes in B outputs it. Otherwise, outputs 0.

B (6 bytes)

;
;
:
​

Takes in A, then B, then outputs B.

A xor B (34 bytes)

;);[!(
====#:
+![(<
:#=="
 >-:
 "=

Takes in A and B. If B is 1, output NOT A. If B is 0, output A.

A or B (13 bytes)

;
=[
:<;:
 ==

If A is 1, output A. Otherwise, output B.

A nor B (23 bytes)

;
=[
 <;
-=[
:-<+:
 ===

If A is 1, output (0). Otherwise, output NOT B.

A xnor B (27 bytes)

;
)
;
[!(
=#=[
(<-<+
:":=:
​

Takes in A and B. If B is 0, output NOT A. Otherwise, output A.

not B (18 bytes)

;;
==[
:-<+:
 ===

Takes in B. If B is 0, add 1 and output. Otherwise, subtract 1 and output. This same construct is used for negation in the other gates.

B implies A (23 bytes)

;
=[
 <;
 =[
:-<+:
 ===

If A is 1, output 1. Otherwise, output NOT B.

not A (17 bytes)

;
==[
:-<+:
 ===

Same as the NOT B answer, except only takes the first the input, not the second.

A implies B (16 bytes)

;-
=[
+<;
+=:
:
​

If A is 0, output 1. Otherwise, output B.

A nand B (24 bytes)

;-
=[
+<;
+=[
:-<+:
 ===

If A is 0, output 1. Otherwise, output NOT B.

True (3 bytes)

+
:
​

Increments the first cell and outputs it, giving 1.

\$\endgroup\$
0
2
\$\begingroup\$

Fourier, 94 bytes

Fourier uses 0 for falsey and 1 truthy. Many thanks to @LeakyNun for golfing help.

False, 1 bytes

o

Outputs the value of the accumulator (0), then takes input.

Try it online!

AND, 4 bytes

I*Io

Multiplies the two numbers together.

Try it online!

a AND NOT b, 6 bytes

I*1-Io

Similar to above, except the second input is subtracted from one, simulating a NOT.

Try it online!

a, 2 bytes

Io

Only outputs the first input.

Try it online!

NOT a AND b, 6 bytes

1-I+Io

Pretty mich the same as a AND NOT b.

Try it online!

b, 3 bytes

IIo

Outputs the second input.

Try it online!

XOR, 6 bytes

I+I=1o

Checks if the sum of the inputs is equal to one.

Try it online!

OR, 6 bytes

I+I>0o

Similar to AND, this checks to see if the sum of the inputs is greater than zero.

Try it online!

NOR, 6 bytes

I+I<1o

Just a reverse of the OR.

Try it online!

XNOR, 8 bytes

1-I+I=1o

Adds a NOT in front of the XOR gate.

Try it online!

NOT b, 5 bytes

I1-Io

Puts a NOT in front of the second input.

Try it online!

b implies a, 13 bytes

1-IoI{1}{@1o}

Outputs NOT a then, if b equals 1, clears the screen and outputs 1.

Try it online!

NOT a, 4 bytes

1-Io

Pretty much NOT b, just slightly different.

Try it online!

a implies b, 16 bytes

I~A1-IoA{0}{@1o}

Stores the first input in the variable A, then outputs the value of NOT b. If a is 0, the program clears the screen and outputs 1.

Try it online!

NAND, 6 bytes

I+I<2o

Again, just the inverse of the AND program.

Try it online!

True, 2 bytes

1o

Outputs 1 and ignores the inputs.

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

Husk, 24 bytes

μ0     take two arguments, return 0
&      and
<      second argument is less than first
¹      return first of two arguments
>      second argument is greater than first
μ⁰     take two arguments, return the last one
≠      arguments are not equal
|      or
¬|     not or
=      arguments are equal
μ¬⁰    take two arguments, return the negation of the last one
≤      second argument is less or equal than the first
¬¹     negate the first of two arguments
≥      second argument is greater or equal than the first
¬&     not and
μ1     take two arguments, return 1

Try it online! (this test suite calls each function with each combination of inputs)

\$\endgroup\$
4
  • \$\begingroup\$ ¬& can be - for -1. \$\endgroup\$ Sep 9, 2017 at 17:30
  • \$\begingroup\$ @EriktheOutgolfer Uhm... It doesn't seem to work \$\endgroup\$
    – Leo
    Sep 11, 2017 at 4:08
  • \$\begingroup\$ I don't think the output must be consistent? -0 0 -> 0, -0 1 -> 1, -1 0 -> -1, -1 1 -> 0 \$\endgroup\$ Sep 11, 2017 at 6:37
  • \$\begingroup\$ @EriktheOutgolfer That's right, but that could at most replace (which returns [0,1,1,0]) and not ¬& (which returns [1,1,1,0]) \$\endgroup\$
    – Leo
    Sep 11, 2017 at 23:39
2
\$\begingroup\$

Alumin, 34 bytes

Try it online!

0 -> b
1 -> g
2 -> c
3 -> k
4 -> rc
5 -> hw
6 -> ahe
7 -> a
8 -> aze
9 -> e
10 -> we
11 -> cha
12 -> dce
13 -> chyc
14 -> tze
15 -> ade

Input is through the top of the stack, and output is through STDOUT or STDERR.

Explanations

0: false

b

This pushes the modulus of the top two members. Anything mod 0 raises an error. 0 % 1 and 1 % 1 are both zero, so this suffices.

1: and

g

This is division: anything divided by 0 raises an error, 0 / 1 is zero, and 1 / 1 is 1. t also works, being multiplication, and so does v, minimum.

2: and-not

c

Numbers in Alumin are truthy iff they are positive (1 or greater). So, subtraction works nicely here:

0 - 0 = 0    (FALSEY)
0 - 1 = -1   (FALSEY)
1 - 0 = 1    (TRUTHY)
1 - 1 = 0    (FALSEY)

3: a

k

k simply pops the top member of the stack, leaving the first argument intact.

4: not-and

yc

Swap, then subtract.

5: B

hw

Create a stack using the first member of the stack. yk (swap then pop) also works.

6: xor

ahe

This adds the two arguments together and checks for equality with one. That is, this is true if only one of its arguments is one.

7: or

a

This adds the two arguments, and is only zero when both of its arguments are zero.

8: nor

aze

This checks if the sum is zero.

9: xnor

e

This checks for equality.

10: not B

we

w pops the TOS and creates a stack using the top TOS elements. When B is zero, this will create a stack with 0 elements; thus, the top two members are always equal, both being nil. When there is one element on the stack (when B is one), it will be 0 or 1, which is always unequal to nil.

11: then-if

cha

c subtracts, and ha adds 1. This is only false for values less than 0, thus is only false for (0, 1). See this table:

0, 0 -> [1] (0)
0, 1 -> [0] (-1)
1, 0 -> [2] (1)
1, 1 -> [1] (0)

12: not A

dce

dc replaces the TOS with 0, and e checks for equality of A with 0, which negates it.

13: if-then

chyc

This performs subtractions, then computes one minus that. This inverts #11:

0, 0 -> [1]
0, 1 -> [2]
1, 0 -> [0]
1, 1 -> [1]

14: nand

tze

Multiplies (t), then checks for equality with 0.

15: true

ade

a adds them so that only one value is on the stack. de checks if its equal to itself, which is always true.

Appendix A: Brute force over A4

Where A is the list of symbols:

[a...z] \ {i, j, q, p, o, n, x}

The alphabet is all of the commands of Alumin without the above characters. i and j are STDIN input (unnecessary, since all input is present), q and p are loop delineators (unnecessary since it would always leave a false value on the top of the stack), o and n are output commands, and x is nondeterministic. (If we could assume x would yield a float from 0 to 1 non-inclusive, it could shorten up many snippets, but as it stands, since there is a chance of it returning a falsey value, it cannot be used.)

The following table shows arrays of all possible solutions for each number of the same length.

0 -> ["b", "d", "f", "h", "l", "m", "r", "s", "y", "z"]
1 -> ["g", "t", "v", "w"]
2 -> ["c"]
3 -> ["k"]
4 -> ["rc", "yc"]
5 -> ["hw", "rk", "yk"]
6 -> ["ahe", "ale", "cdt", "eze", "zee"]
7 -> ["a", "u"]
8 -> ["aze", "dae", "lte", "uze"]
9 -> ["e"]
10 -> ["we"]
11 -> ["cha", "cla", "hcc", "whv", "zea", "zeu"]
12 -> ["dce", "hbe", "kze", "lee", "lge", "rwe", "ywe", "zte", "zve"]
13 -> ["chrc", "chyc", "clrc", "clyc", "drea", "dreu", "drue", "harc", "hayc", "hrca", "rcha", "rcla", "rhcc", "rwhv", "rzea", "rzeu", "ycha", "ycla", "yhcc", "ywhv", "yzea", "yzeu"]
14 -> ["tze", "vze"]
15 -> ["ade", "aha", "ahu", "ake", "akh", "ala", "alu", "awe", "awh", "cde", "chu", "cke", "ckh", "clu", "dea", "deu", "ede", "eha", "ehu", "eke", "ekh", "ela", "elu", "ewe", "ewh", "fhf", "flf", "haa", "hau", "hdw", "hua", "huu", "kde", "kha", "khu", "kke", "kkh", "kla", "klu", "kwe", "kwh", "laa", "lau", "lcc", "lhw", "lua", "luu", "tde", "tha", "thu", "tke", "tkh", "tla", "tlu", "twe", "twh", "ude", "uha", "uhu", "uke", "ukh", "ula", "ulu", "uwe", "uwh", "vde", "vha", "vhu", "vke", "vkh", "vla", "vlu", "vwe", "vwh", "wde", "whu", "wke", "wkh", "zwe", "zwh"]

This was generated using the following ruby program:

#!/usr/bin/ruby
require_relative 'alumin'

def alu(prog, inputs = [])
    inst = Alumin.new prog
    inst.stack.push *inputs
    inst.run rescue return nil
    return nil if inst.stack.size > 1
    return inst.stack[-1]
end


$truthy = "ddzycudzeaghe"
def tru(prog)
    bin = ""
    fp = prog + $truthy
    # p fp
    [[0,0],[0,1],[1,0],[1,1]].each { |arr|
        begin
            res = alu(fp, arr)
            bin += res.to_s
        rescue
            return nil
        end
    }
    bin.to_i(2)
end

max_len = 5

iter = "a"

hash = {}
(0..15).each { |e| hash[e] = [] }
while iter.size != max_len
    unless /[ijqponx]/ === iter
        res = tru(iter)
        if hash[res] && (hash[res].first.nil? || hash[res].first.size == iter.size)
            hash[res] << iter.dup
        end
    end
    iter.next!
end

(0..15).each { |e|
    puts "#{e} -> #{hash[e]}"
}
\$\endgroup\$
0
2
\$\begingroup\$

Japt, 26 bytes

0000 False:              Empty program outputs `undefined`
0001 A&B  : ×     r*1    Reduce by multiply, starting with 1
0010 A&!B : r>           Reduce by greater than
0011 A    : g            Get element at index 0
0100 !A&B : r<           Reduce by less than
0101 B    : o            Pop from the end
0110 A^B  : r^           Reduce by xor
0111 A|B  : d            Some
1000 !A&!B: ev           Map with not, then Every
1001 A==B : r¥    r==    Reduce by equal
1010 !B   : Ìv    gJ v   Take last item, apply not
1011 A|!B : r¨    r>=    Reduce by greater or equal
1100 !A   : v v          Pop first item, apply not
1101 !A|B : r§    r<=    Reduce by less or equal
1110 !A|!B: dv           Map with not, then Some
1111 True : 1

Try it online: False A&B A&!B A !A&B B A^B A|B !A&!B A==B !B A|!B !A !A|B !A|!B True

The input is an array of two values A and B, each being 0 or 1.

Number.v is somewhat abused here. It originally means to return 1 if the given number is divisible by 2, otherwise 0. When it is applied to 0 and 1, the result is 1 and 0 respectively, effectively being not of itself.

It's a shame that I couldn't find any 2-byte solution for !A, while I have one for !B.

\$\endgroup\$
2
\$\begingroup\$

Pyramid Scheme, 325 bytes

I've added A and B to the code to indicate where the inputs are (always A left of B); these are not included in the bytecount. All 16 assume A and B both exist and are either 0 or 1.

False, 15 bytes:

   ^
  /!\
 ^---
A-B

Try it online!

A and B, 10 bytes:

  ^
 /*\
A---B

Try it online!

A and not B, 23 bytes:

  ^
 /*\
A---^
   /!\
  B---

Try it online!

A, 10 bytes:

  ^
 /[\
A---B

Try it online!

Not A and B, 24 bytes:

   ^
  /*\
 ^---B
/!\
---A

Try it online!

B, 10 bytes:

  ^
 /]\
A---B

Try it online!

A xor B, 20 bytes:

   ^
  / \
 /<=>\
A-----B

Try it online!

A or B, 10 bytes:

  ^
 /+\
A---B

Try it online!

Neither A nor B, 23 bytes:

 ^
/!\
---^
  /+\
 A---B

Try it online!

A xnor B, 10 bytes:

  ^
 /=\
A---B

Try it online!

Not B, 23 bytes:

 ^
/!\
---^
  /]\
 A---B

Try it online!

B implies A, 35 bytes:

    ^
   /=\
  ^---^
 /*\ ^-
A---B-

Try it online!

Not A, 23 btyes:

 ^
/!\
---^
  /[\
 A---B

Try it online!

A implies B, 28 bytes:

  ^
 /=\
^---^
-^ /*\
 -A---B

Try it online!

A nand B, 23 bytes:

 ^
/!\
---^
  /*\
 A---B

Try it online!

True, 28 bytes:

   ^
  /]\
 ^---^
A-B /1\
    ---

Try it online!

There are some possibly cheaty ways I could work that bytecount down, like simply detaching one or both inputs, but I've stayed away from those. Both inputs are accepted, and any necessary ignoring is handled by the code.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 17 bytes

Gate Name        L Code
---- ----------- - ----
0000 FALSE       1 0
0001 AND         1 &
0010 A AND NOT B 1 >
0011 A           0 
0100 NOT A AND B 1 <
0101 B           1 ⁹
0110 XOR         1 ^
0111 OR          1 |
1000 NOR         2 |C
1001 XNOR        1 ⁼
1010 NOT B       1 %
1011 B IMPLIES A 1 :
1100 NOT A       1 C
1101 A IMPLIES B 1 ọ
1110 NAND        2 &C
1111 TRUE        1 1

Test suite.

\$\endgroup\$
2
\$\begingroup\$

BitCycle, 140 134 bytes

BitCycle is a language that operates on bits, which makes it good for this challenge. However, it's a 2D language and doesn't have any boolean operators, which makes it less good for this challenge.

false, 2 bytes

0!

and, 11 bytes

 !+
?=/!
?^

A and not B, 11 bytes

 !+
?=/!
?~

A, 2 bytes

?!

not A and B, 14 11 bytes

+~
!=
?^
?^

B, 3 bytes

??!

xor, 13 10 bytes

?v
?v!
!=~

or, 12 bytes

?v
/=/!
!
?^

nor, 13 bytes

?\!+
?\
 ~=/!

xnor, 11 bytes

?v
~=\!
!?^

not B, 6 bytes

 !?
?~

B implies A, 13 bytes

?v
?=\!
  +~

not A, 5 bytes

 !
?~

A implies B, 9 bytes

?=\!
?/+~

nand, 13 bytes

!~
?+
~\\
!?^

true, 2 bytes

1!

General explanation

Bits move around the playfield, interacting with various devices. If they exit the playfield, they are destroyed. Here are the devices used in the following programs:

  • ! - send bits to output
  • ? - get bits from input. Multiple instances of ? are assigned to inputs top-to-bottom, left-to-right. So in these programs, the topmost ? is the A input and the bottommost is B.
  • > < ^ v - redirect bits unconditionally
  • / and \ - reflect the first bit they encounter, pass all subsequent bits straight through
  • = - pass first bit straight through; if first bit was 0, send all subsequent bits west; if first bit was 1 send all subsequent bits east
  • + - 0 turns left, 1 turns right
  • ~ - bit turns right; a negated copy is generated and turns left

If you want in-depth explanation for specific gate(s), leave a comment and I'll add it.

\$\endgroup\$
1
\$\begingroup\$

TI-BASIC, 106 bytes

0000. 0 (1 byte)
0001.*Prompt A:prod(∟A (7 bytes)
0010. Prompt A,B:A>B (8 bytes)
0011. Prompt A,B:A (6 bytes)
0100. Prompt A,B:A<B (8 bytes)
0101. Prompt A,B:B (6 bytes)
0110. Prompt A,B:A xor B (8 bytes)
0111.*Prompt A:sum(∟A (7 bytes)
1000.*Prompt A:not(sum(∟A (8 bytes)
1001. Prompt A,B:A=B (8 bytes)
1010. Prompt A,B:not(B (7 bytes)
1011. Prompt A,B:A≥B (8 bytes)
1100. Prompt A,B:not(A (7 bytes)
1101. Prompt A,B:A≤B (8 bytes)
1110.*Prompt A:not(prod(∟A (8 bytes)
1111. 1 (1 byte)

Starred numbers take input as a list. Answering on my phone again :P

\$\endgroup\$
1
\$\begingroup\$

J, 27 bytes

Some credits to the official page.

0000 false              0:
0001 p and q            *
0010 p and not q        >
0011 p                  [
0100 not p and q        <
0101 q                  ]
0110 xor                ~:
0111 p or q             +.
1000 not p and not q    +:
1001 xnor               =
1010 not q              1-]
1011 p or not q         >:
1100 not p              1-[
1101 not p or q         !
1110 not p or not q     *:
1111 true               1:

I am still not sure which values are truthy/falsey in J.

If -1 is truthy, xor can be golfed to - (subtraction).

\$\endgroup\$
2
  • \$\begingroup\$ For 1011, you can use ^ to save a byte \$\endgroup\$ Jun 15, 2016 at 17:14
  • 1
    \$\begingroup\$ And -1 is truthy \$\endgroup\$ Jun 15, 2016 at 17:22
1
\$\begingroup\$

Python, 215 bytes

lambda a,b:0
int.__mul__
lambda a,b:a>b
lambda a,b:a
lambda a,b:a<b
lambda a,b:b
int.__xor__
int.__or__
lambda a,b:not a|b
lambda a,b:a==b
lambda a,b:1-b
lambda a,b:a>=b
lambda a,b:1-a
lambda a,b:a<=b
lambda a,b:1-a*b
lambda a,b:1

Ideone it!

\$\endgroup\$
3
  • \$\begingroup\$ For nor you could do a<1>b. \$\endgroup\$ Jun 15, 2016 at 13:41
  • \$\begingroup\$ You can shave off one byte from each of the two constant functions by writing them like this: lambda *x:0. Actually it's ok not to consume the inputs, so save three more and write lambda:0. \$\endgroup\$
    – alexis
    Jun 15, 2016 at 20:42
  • 2
    \$\begingroup\$ Going through the builtins: AND, OR, XOR, >=, <, TRUE could be min, max, cmp, pow, range, slice. \$\endgroup\$ Jun 16, 2016 at 19:00
1
\$\begingroup\$

Pyke, 22 bytes

0000 false              0 
0001 p and q            &
0010 p and not q        >
0011 p                  Kz
0100 not p and q        <
0101 q                  Q
0110 xor                N
0111 p or q             |
1000 not p and not q    |!
1001 eq                 q
1010 not q              !
1011 p or not q         !&
1100 not p              K!
1101 not p or q         !|
1110 not p or not q     &!
1111 true               1
\$\endgroup\$
1
\$\begingroup\$

Yup, 109 + 80 = 189 bytes

+80 bytes for 16 -x 0 flags. For removing the imaginary parts.

0000 false           0#
0001 and             *|0*|--e#
0010 A and not B     **-#
0011 A               *#
0100 not A and B     0**--#
0101 B               **#
0110 xor             **-:|~|0~--e#
0111 or              *0*--#
1000 nor             0e*0*---#
1001 xnor            0e**-:|~|0~--e-#
1010 not B           *0e*-#
1011 B implies A     **|-#
1100 not A           0e*-#
1101 A implies B     0e**--
1110                 0e00e--*0*---#
1111 true            0e#

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MarioLANG, 176 132 bytes

These solutions were all tested in Ruby interpreter and make substantial use of the implementation-specific feature that > and < can move Mario in mid-air, avoiding the need for ground tiles in many cases.

MarioLANG's only conditional is [ which skips the next command if the current cell is zero. Therefore all non-zero values are truthy and zero is falsy. I'm making use of this to shorten the code substantially by occasionally outputting -1 as truthy.

0000 (1 byte)

:

0001 (7 bytes)

;[;
==:

0010 (12 bytes)

;
[
>;
:-
 :

0011 (3 bytes)

;
:

0100 (9 bytes)

;-[;
===:

0101 (5 bytes)

;;
=:

0110 (12 bytes)

;
[
>;
;-
::

0111 (10 bytes)

;
[
>:
;
:

1000 (13 bytes)

;
[
>-
;:
-
:

1001 (13 bytes)

;
[
>;
;:
-
:

1010 (7 bytes)

;;-
==:

1011 (12 bytes)

;
[
>:
;
-
:

1100 (5 bytes)

;-
=:

1101 (11 bytes)

;
[
>;
+:
:

1110 (9 bytes)

;[;-
===:

1111 (3 bytes)

+
:
\$\endgroup\$
1
\$\begingroup\$

Coconut, 88 bytes

0000 [].sort
0001 (*)
0010 (>)
0011 round
0100 (<)
0101 range
0110 (^)
0111 (|)
1000 (not)..(|)
1001 (==)
1010 (a,b)->b<1
1011 pow
1100 (a,b)->a<1
1101 (<=)
1110 (not)..(*)
1111 slice

Coconut is an extension of Python. Every valid program in Python is also valid in Coconut.

Credits to xnor for some of the functions.

\$\endgroup\$
1
\$\begingroup\$

C, 164 Bytes

#define r (a,b){return
c r 0;}d r a&b;}e r a>b;}f r a;}g r a<b;}h r b;}i r a^b;}j r a|b;}k r !b>a;}l r a==b;}m r !b;}n r !b|a;}o r !a;}p r !a|b;}q r !b|!a;}s r 1;}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.