Logic Gates Manually
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Make a program that simulates the basic logic gates.
Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.
Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.
Examples:
AND 1 0 becomes 0
XOR 0 1 becomes 1
OR 1 1 becomes 1
NAND 1 1 becomes 0
This is codegolf, so the shortest code wins.
code-golf
add a comment |Â
up vote
3
down vote
favorite
Make a program that simulates the basic logic gates.
Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.
Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.
Examples:
AND 1 0 becomes 0
XOR 0 1 becomes 1
OR 1 1 becomes 1
NAND 1 1 becomes 0
This is codegolf, so the shortest code wins.
code-golf
Can we take in an array as input?
â Quintec
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
1
Can we output as True/False?
â xnor
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Make a program that simulates the basic logic gates.
Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.
Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.
Examples:
AND 1 0 becomes 0
XOR 0 1 becomes 1
OR 1 1 becomes 1
NAND 1 1 becomes 0
This is codegolf, so the shortest code wins.
code-golf
Make a program that simulates the basic logic gates.
Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.
Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.
Examples:
AND 1 0 becomes 0
XOR 0 1 becomes 1
OR 1 1 becomes 1
NAND 1 1 becomes 0
This is codegolf, so the shortest code wins.
code-golf
code-golf
asked 2 hours ago
qazwsx
5151410
5151410
Can we take in an array as input?
â Quintec
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
1
Can we output as True/False?
â xnor
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago
add a comment |Â
Can we take in an array as input?
â Quintec
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
1
Can we output as True/False?
â xnor
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago
Can we take in an array as input?
â Quintec
2 hours ago
Can we take in an array as input?
â Quintec
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
1
1
Can we output as True/False?
â xnor
1 hour ago
Can we output as True/False?
â xnor
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
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.
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
add a comment |Â
up vote
1
down vote
JavaScript (Node.js), 106 94 bytes
x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)
Try it online!
Link to the code and all 24 cases.
+9 for forgotten to map the XNOR case.
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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.
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
add a comment |Â
up vote
2
down vote
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.
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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.
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.
answered 15 mins ago
xnor
87.3k17181432
87.3k17181432
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
add a comment |Â
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
â Jo King
22 secs ago
add a comment |Â
up vote
1
down vote
JavaScript (Node.js), 106 94 bytes
x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)
Try it online!
Link to the code and all 24 cases.
+9 for forgotten to map the XNOR case.
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
 |Â
show 2 more comments
up vote
1
down vote
JavaScript (Node.js), 106 94 bytes
x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)
Try it online!
Link to the code and all 24 cases.
+9 for forgotten to map the XNOR case.
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
 |Â
show 2 more comments
up vote
1
down vote
up vote
1
down vote
JavaScript (Node.js), 106 94 bytes
x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)
Try it online!
Link to the code and all 24 cases.
+9 for forgotten to map the XNOR case.
JavaScript (Node.js), 106 94 bytes
x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)
Try it online!
Link to the code and all 24 cases.
+9 for forgotten to map the XNOR case.
edited 2 hours ago
answered 2 hours ago
Shieru Asakoto
1,790311
1,790311
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
 |Â
show 2 more comments
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
â qazwsx
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
@qazwsx This is a lambda function, which by default is allowed.
â Shieru Asakoto
2 hours ago
1
1
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.
console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g.
console.log(f("AND", 1, 1));
â Megoâ¦
2 hours ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
@qazwsx And please revert the downvote. This is not invalid after all.
â Shieru Asakoto
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
oh I see. i reverted the downvote
â qazwsx
1 hour ago
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173492%2flogic-gates-manually%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can we take in an array as input?
â Quintec
2 hours ago
no @Quintec you cant
â qazwsx
2 hours ago
1
Can we output as True/False?
â xnor
1 hour ago
sure @xnor (also relevant username)
â qazwsx
1 hour ago