62
\$\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\$
  • 2
    \$\begingroup\$ Your functions/programs may share code. What does this mean? Also, may the programs be in different languages? \$\endgroup\$ – Lynn Jun 14 '16 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 '16 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\$ – Mego Jun 15 '16 at 1:33
  • 14
    \$\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 '16 at 17:25
  • 4
    \$\begingroup\$ @dorukayhan See: vacuous truth \$\endgroup\$ – Sp3000 Jun 15 '16 at 21:19

77 Answers 77

1
\$\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\$
  • \$\begingroup\$ @Riker Good point, thank you \$\endgroup\$ – Conor O'Brien Dec 17 '17 at 21:08
1
\$\begingroup\$

R, 50 bytes

 1. F
 2. &
 3. >
 4. print
 5. <
 6. "[<-"
 7. !=
 8. |
 9. <!
10. ==
11. !"[<-"
12. ^
13. function(a,b)!a
14. <=
15. !all
16. T

All functions (except 1 and 16 full programs), some infix, a and b taking values in (F,T). 4, 6 and 11 are R specific (TIO link below). I wish I had a good answer also for 13...

Try it online!

\$\endgroup\$
  • \$\begingroup\$ pryr::f(x,y,!x) ties 13 at 15 bytes, but I know virtually no R and can't shorten it further. Maybe you can manage to do so... \$\endgroup\$ – Khuldraeseth na'Barya May 30 '18 at 19:53
  • \$\begingroup\$ @Scrooble Thanks! all my attempts to use pryr::fare failing as I can't get rid of the second argument... \$\endgroup\$ – JayCe May 30 '18 at 20:18
1
\$\begingroup\$

Excel: 84B

0
=a1*b3
=a1>b3
=a1
=a1<b3
=b3
=a1-b3
=a1+b3
=a1+b3=0
=a1=b3
=1-b3
=a1>=b3
=1-a1
=a1<=b3
=a1*b3<1
1
\$\endgroup\$
  • \$\begingroup\$ Not sure if allowed to write FALSE as `` to save 1 byte \$\endgroup\$ – l4m2 Dec 7 '17 at 5:52
  • \$\begingroup\$ The use of named sources is not standard for Excel or Google Sheets, you should use the standard input ranges of [A1] and [B1] instead, that said, great submission \$\endgroup\$ – Taylor Scott Dec 18 '17 at 14:43
  • \$\begingroup\$ @Taylor Scott It's like banning to input via arguments and require to use fixed given name in JavaScript, totally going against the rule \$\endgroup\$ – l4m2 Dec 18 '17 at 19:11
  • \$\begingroup\$ ok not that same but both named source and A1 are similar \$\endgroup\$ – l4m2 Dec 18 '17 at 19:13
  • \$\begingroup\$ As was decided by the community, this is invalid. please consider correcting it \$\endgroup\$ – Taylor Scott Jul 13 '18 at 16:26
0
\$\begingroup\$

Java, 695 bytes

class L{boolean f(){return false;}boolean d(boolean... x){return a(x)==t()?b(x):f();}boolean q(boolean...x){return d(a(x),n(b(x)));}boolean n(boolean x){return x==t()?f():t();}boolean a(boolean... x){return x[0];}boolean w(boolean...x){return d(n(a(x)),b(x));}boolean b(boolean...x){return x[1];}boolean x(boolean...x){return d(o(x),n(d(x)));}boolean o(boolean...x){return n(d(n(a(x)),n(b(x))));}boolean s(boolean...x){return n(o(x));}boolean r(boolean...x){return n(x(x));}boolean l(boolean...x){return n(b(x));}boolean j(boolean...x){return n(w(x));}boolean c(boolean...x){return n(a(x));}boolean v(boolean...x){return n(q(x));}boolean m(boolean...x){return n(d(x));}boolean t(){return !f();}}

Ungolfed

public class LogicGate
{
    public static boolean f(boolean... x)
    {
        return false;
    }

    public static boolean and(boolean... x)
    {
        return a(x)==t() ? b(x) : f();
    }

    public static boolean aandnotb(boolean... x)
    {
        return and( a(x), not(b(x)) );
    }

    private static boolean not(boolean x)
    {
        return x==t()?f():t();
    }

    public static boolean a(boolean... x)
    {
        return x[0];
    }

    public static boolean notaandb(boolean... x)
    {
        return and( not(a(x)), b(x));
    }

    public static boolean b(boolean... x)
    {
        return x[1];
    }

    public static boolean xor(boolean... x)
    {
        return and( or(x), not(and(x)) );
    }

    public static boolean or(boolean... x)
    {
        return not( and( not(a(x)), not(b(x))) );//see de morgan's law
    }

    public static boolean nor(boolean... x)
    {
        return not(or(x));
    }

    public static boolean xnor(boolean... x)//it is understandable when written nxor. See https://en.wikipedia.org/wiki/XNOR_gate
    {
        return not(xor(x));
    }

    public static boolean notb(boolean... x)
    {
        return not(b(x));
    }

    public static boolean bimpliesa(boolean... x) //A ⇒ B is true only in the case that either A is false o B is true.
    {
        return not(notaandb(x)) ;// == o( n(b(x)), a(x));
    }

    public static boolean nota(boolean... x)
    {
        return not(a(x));
    }

    public static boolean aimpliesb(boolean... x)
    {
        return not(aandnotb(x));
    }

    public static boolean nand(boolean... x)
    {
        return not( and(x) );
    }

    public static boolean t(boolean... x)
    {
        return !f();
    }
}

Test case to try above code.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can just write the functions without declaring class. \$\endgroup\$ – Leaky Nun Jun 17 '16 at 11:25
  • 1
    \$\begingroup\$ Also, we decided that the programs cannot share code. \$\endgroup\$ – Leaky Nun Jun 17 '16 at 11:26
  • 1
    \$\begingroup\$ Also, x==t()? can be replaced with x?, not with !, and you can use the && and || operators. \$\endgroup\$ – Leaky Nun Jun 17 '16 at 11:29
  • 1
    \$\begingroup\$ Also, all the boolean in the function type declarations can be changed to Object. \$\endgroup\$ – Leaky Nun Jun 17 '16 at 11:30
  • 3
    \$\begingroup\$ @LeakyNun That's a lot of comments. O_O \$\endgroup\$ – user48538 Jul 22 '16 at 14:00
0
\$\begingroup\$

Sesos, 56 55 bytes (non-competing)

; shared by all programs, counted for each
set numin,set numout

; 0,0,0,0, 1 byte
put
; 0,0,0,1, 3 bytes
get,jmp,get,fwd 1,jnz,rwd 1,put
; 0,0,1,0, 4 bytes
get,fwd 1,get,sub 1,jmp,rwd 1,jnz,fwd 2,put
; 0,0,1,1, 1 byte
get,put
; 0,1,0,0, 4 bytes
get,sub 1,fwd 1,get,jmp,rwd 1,jnz,fwd 2,put
; 0,1,0,1, 2 bytes
get,get,put
; 0,1,1,0, 5 bytes
get,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,rwd 1,put
; 0,1,1,1, 5 bytes
get,fwd 1,get,jmp,rwd 1,add 1,fwd 1,sub 1,jnz,rwd 1,put
; 1,0,0,0, 5 bytes
add 1,fwd 1,get,jmp,rwd 1,jnz,get,jmp,rwd 1,jnz,rwd 1,put
; 1,0,0,1, 7 bytes
get,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,add 1,rwd 1,jmp,fwd 1,jnz,fwd 1,put
; 1,0,1,0, 2 bytes
get,get,sub 1,put
; 1,0,1,1, 4 bytes
add 2,fwd 1,get,fwd 1,get,jmp,rwd 1,jnz,fwd 1,sub 1,put
; 1,1,0,0, 2 bytes
get,sub 1,put
; 1,1,0,1, 5 bytes
get,sub 1,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,rwd 1,put
; 1,1,1,0, 4 bytes
get,fwd 1,get,jmp,rwd 1,jnz,fwd 2,sub 1,put
; 1,1,1,1, 1 byte
add 1,put

Input is 0 or 1, output is zero (falsy) or non-zero (truthy).

Thanks to @MartinEnder for golfing off 1 byte!

\$\endgroup\$
  • \$\begingroup\$ Can you include all the hexdumps? \$\endgroup\$ – Leaky Nun Jul 28 '16 at 3:45
  • \$\begingroup\$ Can you mark this answer as non-competing? \$\endgroup\$ – Leaky Nun Jul 28 '16 at 3:45
  • \$\begingroup\$ Ah, forgot it was non-competing. I'll add the hexdumps (and permalinks) after trying to golf it a bit more. \$\endgroup\$ – Dennis Jul 28 '16 at 3:52
0
\$\begingroup\$

Golfscript, 36 bytes

0000 ;
0001 ~*
0010 ~>
0011 ~;
0100 ~<
0101 ~\;
0110 ~^
0111 ~|
1000 ~*!
1001 ~=
1010 ~\;!
1011 ~?
1100 ~;!
1101 ~>!
1110 ~*!
1111 

Online interpreter.

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

RProgN, 52 Bytes Non-Competing

0000    [ [ 0   # Pop A and B off reg, push 0 (Falsey)
0001    &       # Push Logical And of A AND B
0010    ! &     # Invert B, Logical AND
0011    [       # Pop B
0100    \ ! &   # Push B under A, Invert A, Logical AND.
0101    \ [     # Push B under A, pop A.
0110    == !    # Pop and And B and push truthy if they're equal. Logical not the output.
0111    |       # Logical Or of A OR B
1000    | !     # Logical Or of A OR B, Inverted.
1001    ==      # Pop and And B and push truthy if they're equal, falsy if they're not.
1010    \ [ !   # Flip B under A, Pop A, Pop B and return it's logical Not.
1011    ! |     # Pop B and push its logical Not, Logical Or of A OR (NOT B)
1100    [ !     # Pop B, pop A then Push the not value of A
1101    \ ! |   # Swap B under A, Pop A and Push it's logical Not, Push the Logical Or of A OR B
1110    & !     # Push Logical And of A AND B, pop and push the logical not.
1111    [ [ 1   # Push A and B off reg, Push 1 (Truthy)

ALL of these are full programs. Four digits behind and text after and including the # are all for commentary.

\$\endgroup\$
  • \$\begingroup\$ If the date on the github is correct you should mark this as non-competing \$\endgroup\$ – Sriotchilism O'Zaic Oct 21 '16 at 5:48
0
\$\begingroup\$

Java 8, tested in Java 7 and written over directly, 565 bytes

This is mainly as a proof of concept, because I wanted to make a program for this where every gate could be written by only changing a number. It's not nearly as short as some of the other answers, but I felt like posting it here regardless.

interface G{int g(boolean a,boolean b);G[]g={(a,b)->0&(1<<((a?0:2)+(b?0:1))),(a,b)->1&(1<<((a?0:2)+(b?0:1))),(a,b)->2&(1<<((a?0:2)+(b?0:1))),(a,b)->3&(1<<((a?0:2)+(b?0:1))),(a,b)->4&(1<<((a?0:2)+(b?0:1))),(a,b)->5&(1<<((a?0:2)+(b?0:1))),(a,b)->6&(1<<((a?0:2)+(b?0:1))),(a,b)->7&(1<<((a?0:2)+(b?0:1))),(a,b)->8&(1<<((a?0:2)+(b?0:1))),(a,b)->9&(1<<((a?0:2)+(b?0:1))),(a,b)->10&(1<<((a?0:2)+(b?0:1))),(a,b)->11&(1<<((a?0:2)+(b?0:1))),(a,b)->12&(1<<((a?0:2)+(b?0:1))),(a,b)->13&(1<<((a?0:2)+(b?0:1))),(a,b)->14&(1<<((a?0:2)+(b?0:1))),(a,b)->15&(1<<((a?0:2)+(b?0:1)))};}

Behold this disgusting mess. Ungolfed:

interface G{
    int g(boolean a, boolean b);
    G[] g = {
        (a, b) -> 0 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 1 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 2 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 3 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 4 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 5 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 6 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 7 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 8 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 9 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 10 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 11 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 12 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 13 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 14 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
        (a, b) -> 15 & (1 << ((a ? 0 : 2) + (b ? 0 : 1)))
               // ^ this number + 1 is the logic gate being programmed
    };
}
\$\endgroup\$
0
\$\begingroup\$

BitCycle, 140 134 bytes (non-competing)

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\$
0
\$\begingroup\$

Ruby, 246 bytes, non-competing

Not really golfed, and somewhat boring. But I can't leave this question without a Ruby answer.

1    ->a,b{0[a+a+b]}
2    ->a,b{1[a+a+b]}
3    ->a,b{2[a+a+b]}
4    ->a,b{3[a+a+b]}
5    ->a,b{4[a+a+b]}
6    ->a,b{5[a+a+b]}
7    ->a,b{6[a+a+b]}
8    ->a,b{7[a+a+b]}
9    ->a,b{8[a+a+b]}
10   ->a,b{9[a+a+b]}
11   ->a,b{10[a+a+b]}
12   ->a,b{11[a+a+b]}
13   ->a,b{12[a+a+b]}
14   ->a,b{13[a+a+b]}
15   ->a,b{14[a+a+b]}
16   ->a,b{15[a+a+b]}

A lot of improvements are possible, for example rewriting the first function as ->a,b{} but even assuming the function body is 1 byte for each answer, the minimum byte count is 64.

Based on the python non-competing lambda, this could be written as:

->n{->a,b{n[a+a+b]}}
\$\endgroup\$
0
\$\begingroup\$

Pyth, 33 bytes

0000: 0
0001: &E
0010: <E
0011: Q
0100: >E
0101: E 
0110: xE
0111: |E
1000: !|E
1001: qE
1010: !E 
1011: !>E
1100: !
1101: !<E
1110: !&E
1111: 1

Mind the trailing spaces! Test here, putting each program in the Code box.

\$\endgroup\$
  • \$\begingroup\$ I'm surprised there isn't an answer in Pyth. \$\endgroup\$ – Leaky Nun Jun 18 '17 at 14:08
  • \$\begingroup\$ I think you can drop out those Es on those two-byte solutions, since they are actually functions. \$\endgroup\$ – Leaky Nun Jun 18 '17 at 14:09
  • \$\begingroup\$ Also a bare E wouldn't work. \$\endgroup\$ – Leaky Nun Jun 18 '17 at 14:09
  • \$\begingroup\$ Nor would !E. \$\endgroup\$ – Leaky Nun Jun 18 '17 at 14:10
  • \$\begingroup\$ @LeakyNun Not sure what would a function be like in Pyth. \$\endgroup\$ – Erik the Outgolfer Jun 18 '17 at 14:16
0
\$\begingroup\$

Wise, 46 bytes (not-competing)

False

^:^

And

&

A and not B

?~&

A

?:^|

not A and B

~&

B

:^|

Xor

^

Or

|

Nor

|~

Xnor

^~

Not B

:^|~

B implies A

~&~

Not A

?:^^~

A implies B

?~&~

Nand

~?~&

True

^:~^
\$\endgroup\$
0
\$\begingroup\$

Add++, 135 bytes

D,0000,,0
D,0001,@@,&
D,0010,@@,!$&
D,0011,@,
D,0100,@@,$!&
D,0101,@@,
D,0110,@@,Bx
D,0111,@@,Bo
D,1000,@@,Bo!
D,1001,@@,Bx!
D,1010,@@,!
D,1011,@@,!Bo
D,1100,@,!
D,1101,@@,!&!
D,1110,@@,&!
D,1111,,1

The byte count assumes single byte names, such as a or b

How they work

0000

D,0000,,     - Create a niladic function
          0  - Return 0

0001

D,0001,@@,   - Create a dyadic function
          &  - Return A and B

0010

D,0010,@@,   - Create a dyadic function
          !  - NOT B
          $  - Swap
          &  - Return A and NOT B

0011

D,0011,@,    - Create a monadic function
             - Return A

0100

D,0100,@@,   - Create a dyadic function
          $  - Swap
          !  - NOT A
          &  - Return NOT A and B

0101

D,0101,@@,   - Create a dyadic function
             - Return B

0110

D,0110,@@,   - Create a dyadic function
          Bx - Return A xor B

0111

D,0111,@@,   - Create a dyadic function
          Bo - Return A or B

1000

D,1000,@@,   - Create a dyadic function
          Bo - A or B
          !  - Return NOT A or NOT B

1001

D,1001,@@,   - Create a dyadic function
          Bx - A xor B
          !  - Return NOT A xor NOT B

1010

D,1010,@@,   - Create a dyadic function
          !  - Return NOT B

1011

D,1011,@@,   - Create a dyadic function
          !  - NOT B
          Bo - Return A or NOT B

1100

D,1100,@,    - Create a monadic function
         !   - Return NOT A

1101

D,1101,@@,   - Create a dyadic function
          !  - NOT B
          &  - A and NOT B
          !  - Return NOT A and B

1110

D,1110,@@,   - Create a dyadic function
          &  - A and B
          !  - Return NOT A and NOT B

1111

D,1111,,     - Create a niladic function
        1    - Return 1
\$\endgroup\$
  • \$\begingroup\$ "each logic gate takes two inputs" - can the niladic functions take arguments? Because if not, this seems invalid \$\endgroup\$ – FlipTack Nov 18 '17 at 17:56
  • \$\begingroup\$ @FlipTack A niladic function can be given as many arguments as it wants, it'll just ignore them. For instance, try adding $f>1>2>3\nO to the bottom of the last one on TIO (\n is a literal new line). The output doesn't change. \$\endgroup\$ – caird coinheringaahing Nov 18 '17 at 17:59
  • \$\begingroup\$ Hm that's what I presumed, just wanted to check \$\endgroup\$ – FlipTack Nov 18 '17 at 18:17
0
\$\begingroup\$

Implicit, 35 28 bytes

0000 false              #      push length of stack
0001 p and q            *      2 implicit int inputs, multiply
0010 p and not q        <      2 implicit int inputs, less than
0011 p                  $      read input
0100 not p and q        >      2 implicit int inputs, greater than
0101 q                  $$     read input twice
0110 xor                ·      2 implicit int inputs, XOR
0111 p or q             +      2 implicit int inputs, add
1000 not p and not q    ñ$ñ=   implicit input NOT-equals self, input not-equals self, equality
1001 eq                 =      2 implicit int inputs, equality
1010 not q              $$ñ    read two integers, second NOT-equals self
1011 p or not q         $$ñ+   read two integers, second NOT-equals self, add
1100 not p              ñ      implicit input, NOT-equals self
1101 not p or q         ñ$+    implicit input NOT-equals self, read input, add
1110 not p or not q     *ñ     2 implicit int inputs, multiply, logical-NOT
1111 true               1      push 1

All of these rely on implicit output.

Links: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

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

Pyth - 75 bytes

False

0

And

AQ&GH

A and not B

AQ&G!H

A

AQG

Not A and B

AQ&!GH

B

AQH

XOR

AQxGH

OR

AQ+GH

NOR

AQ!+GH

XNOR

AQ!xGH

Not B

AQ!H

B implies A

AQ!&!GH

not A

AQ!G

A implies b

AQ!&G!H

nand

AQ!&GH

True

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

Pyt, 46 bytes

0000   0
0001   ←←∧
0010   ←←¬∧
0011   ←
0100   ←¬←∧
0101   ←ŕ←
0110   ←←⊻
0111   ←←∨
1000   ←←⊽
1001   ←←⊙
1010   ←ŕ←¬
1011   ←¬←⊼
1100   ←¬
1101   ←←¬⊼
1110   ←←⊼
1111   1
\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 114 Bytes

Series of binary Boolean operators which give the individually specified commented to the right of the function. All take input, if any, as A from [A1] and B from [B1] on the ActiveSheet object

Interestingly, despite the presence of OR, AND, NOT, XOR and IMP operators, using any of these directly is actually longer than using math.

?0          ' 0,0,0,0 (false)
?[A1*B1]    ' 0,0,0,1 (and)
?[A1>B1]    ' 0,0,1,0 (A and not B)
?[A1]       ' 0,0,1,1 (A)
?[A1<B1]    ' 0,1,0,0 (not A and B)
?[B1]       ' 0,1,0,1 (B)
?[A1-B1]    ' 0,1,1,0 (xor)
?[A1+B1]    ' 0,1,1,1 (or)
?[A1+B1=0]  ' 1,0,0,0 (nor)
?[A1=B1]    ' 1,0,0,1 (xnor)
?[1-B1]     ' 1,0,1,0 (not B)
?[A1>=B1]   ' 1,0,1,1 (B implies A)
?[1-A1]     ' 1,1,0,0 (not A)
?[A1<=B1]   ' 1,1,0,1 (A implies B)
?1>[A1*B1]  ' 1,1,1,0 (nand)
?1          ' 1,1,1,1 (true)

Uncommented Version

For Byte Counting

?0
?[A1*B1]
?[A1>B1]
?[A1]
?[A1<B1]
?[B1]
?[A1-B1]
?[A1+B1]
?[A1+B1=0]
?[A1=B1]
?[1-B1]
?[A1>=B1]
?[1-A1]
?[A1<=B1]
?1>[A1*B1]
?1
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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