5
\$\begingroup\$

Background

With the success of Simple Boolean Algebra Calculator, I present a more difficult problem.

Boolean Algebra concerns representing values with letters and simplifying expressions. The following is a chart for the standard notation used: Notation

Above is what actual boolean algebra looks like. For the purposes of this code golf, this is not the exact syntax that will be used.

Your Task

Given a short boolean expression, return the solution to the expression.

There will be at most two values and one function involved in the calculator. The values may be any of 0, 1, or a capital letter(s). The functions may be /, +, or .

Any of the following may be an input: (values are demonstrated with X and Y in these examples.)

One function, One value

  • There is only one possibility for this, which is /X. Return NOT X. If X is a variable, return /X, otherwise compute and output. This is the only scenario where /, the backslash, will be used.

Two values

  • There is only one possibility for this, which is XY. Return X AND Y. If X and Y are different variables, return XY, otherwise compute and output. This is the only scenario where two values are next to each other.

One value, One function, Another value

  • If you've done Simple Boolean Algebra Calculator, this is exactly the same except that it now must be able to output two variable calculations.
  • Return the value for X (function) Y. If X and Y are different variables, output X (function) Y.
  • (function), the second character in this scenario, will be either "+" or ".". If "+", treat as OR. If ".", treat as AND.

Input: Either two characters or three characters.

Output: Either one character, two characters, or three characters.

Explained Examples

Input => Output
/0 => 1
/1 => 0
/P => /P

These expressions output the NOT value for the input. /P outputs /P because it cannot be simplified further.

Input => Output
0Q => 0

0Q evaluates to 0 because 0 AND Q will always be 0, regardless of what value Q takes.

Input => Output
Z1 => Z

Z1 evaluates to Z because the output for Z AND 1 depends entirely on the value of Z.

Input => Output
AH => AH (or A.H)

AH evaluates to AH because it cannot be simplified further. A.H is also a valid output.

Input => Output
A+1 => 1

A+1 evaluates to 1 because the OR statement is overridden by 1. That is, no matter what value A takes, the presence of 1 means that the statement will always evaluate to 1.

Input => Output
B+B => B

B+B evaluates to B because the OR statement is dependent on either Bs being true. If both Bs are false, then the output would also be false. So, the statement returns B since whatever value B takes, the output would return that.

Input => Output
W+L => W+L

W+L evaluates to W+L because it cannot be simplified further.

Input => Output
0.H => 0

0.H evaluates to 0 because the AND statement is overridden by 0. That is, no matter what value H takes, the presence of 0 means that the statement will always evaluate to 0.

Input => Output
1.1 => 1

1.1 evaluates 1 because the AND statement requires both inputs to be 1, and since they are both 1, 1 is returned.

Input => Output
G.L => G.L

G.L evaluates to G.L because it cannot be simplified further.

Examples/Test cases

Input => Output

/0 => 1
/1 => 0
/P => /P

00 => 0
11 => 1
0Q => 0
Z1 => Z
NN => N
AH => AH (or A.H)

A+1 => 1
B+B => B
R+0 => R
1+1 => 1
0+0 => 0
A+B => A+B
Y+Z => Y+Z

0.0 => 0
Q.Q => Q
0.A => 0
1.C => C
1.1 => 1
R.S => R.S
N.W => N.W

This is , so shortest answer wins. Good luck.

\$\endgroup\$
2
  • \$\begingroup\$ Is A.H an acceptable output for AH? \$\endgroup\$
    – Arnauld
    Nov 20, 2022 at 12:22
  • \$\begingroup\$ @Arnauld A.H and AH are both acceptable for AH. \$\endgroup\$ Nov 20, 2022 at 15:01

3 Answers 3

1
\$\begingroup\$

JavaScript (ES6), 75 bytes

s=>([x,o,y]=s,y||(y=o,o=-1),x<'0')?1/y?y^1:s:x+1|y+1|x==y?~x&y!=0^o+1?x:y:s

Try it online!

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

Retina 0.8.2, 62 bytes

0\+|\+0

/0|1\+.|.\+1
1
/1|.*0.*
0
1\.?(.)|(.)(\.?1|.?\2)
$1$2

Try it online! Link includes test cases. Explanation:

0\+|\+0

0+X and X+0 become X.

/0|1\+.|.\+1
1

/0, 1+X and X+1 become 1.

/1|.*0.*
0

/1, 0X, 0.X, X0 and X.0 become 0. (This is tested after /0 so that it doesn't erroneously match the X0 rule and after 0+X and X+0 so that a simpler regex can be used that would otherwise erroneously match them.)

1\.?(.)|(.)(\.?1|.?\2)
$1$2

1X, 1.X, X1, X.1, XX, X.X and X+X become X. (This is tested after /1 so that it doesn't erroneously match the X1 rule.)

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

Charcoal, 36 bytes

¿№θ/¿‹⌈θAI¬Σθθ¿⁼²LΦα№θιθFI№θ+¿№θιι⌈θ

Try it online! Link is to verbose version of code. Explanation:

¿№θ/

If the input contains a /, then:

¿‹⌈θA

If the input does not contain a letter, then...

I¬Σθ

... output the complement of the digit that it contains, otherwise...

θ

... output the input.

¿⁼²LΦα№θι

Otherwise, if the input contains two letters, then...

θ

... output the input, otherwise...

FI№θ+¿№θιι⌈θ

... solve the problem using the method from Simple Boolean Algebra Calculator.

\$\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.