50
5
This challenge is to lift the spirits of our mod Alex A., who is usually wrong.
Suppose you have a friend named Alex who needs help with basic logic and math, specifically mathematical equivalence.
He gives you a list of equations of the form [variable] = [variable]
where a [variable]
is always a single uppercase letter A to Z (not a lowercase letter, not a number, nor anything else). There's one equation per line in the list except for a single line that only says therefore
.
All the equations above the therefore
are premises, facts that are assumed to be true. All the equations below the therefore
are unverified propositions, facts that Alex is attempting to infer from the premises, and they may or may not be true.
For example, in this equation list the single conclusionary proposition A = C
happens to be true:
A = B
B = C
therefore
A = C
It's your job to tell Alex if all of his propositions logically follow from the given premises. That is, you need to tell Alex if he is wrong or right in his conclusions.
Write a program/function that takes in a string of a list of equations as described and prints/returns
Alex is right
if all the conclusions follow logically from the premises, and otherwise outputs
Alex is wrong
if any conclusion does not logically follow from the premises.
The shortest code in bytes wins.
Be sure to watch out for these cases:
Variable always equals themselves. e.g.
B = A therefore A = A X = X
results in
Alex is right
.Variables with unknown relationships cannot be assumed to be equal. e.g.
P = Q therefore E = R
results in
Alex is wrong
.When there are no equations after the
therefore
then the conclusions are vacuously true. e.g.D = C therefore
and
therefore
both result in
Alex is right
.When there are no equations before the
therefore
then only self-equality can be inferred. e.g.therefore R = R
results in
Alex is right
, buttherefore R = W
results in
Alex is wrong
.
More Examples
Alex is wrong cases: (separated by empty lines)
A = B
C = D
therefore
A = C
A = L
E = X
A = I
S = W
R = O
N = G
therefore
G = N
L = I
R = O
S = A
X = X
X = E
D = K
D = Q
L = P
O = L
M = O
therefore
K = L
A = B
therefore
B = C
Z = A
S = S
therefore
A = Z
A = A
S = A
A = S
Z = A
Z = A
K = L
K = X
therefore
X = P
L = X
L = P
therefore
A = B
B = C
A = C
therefore
A = A
B = B
C = C
D = D
E = E
F = F
G = G
H = H
I = I
J = J
K = K
T = I
L = L
M = M
N = N
O = O
P = P
Q = Q
R = R
S = S
T = T
U = U
V = V
W = W
X = X
Y = Y
Z = Z
A = B
B = C
C = D
D = E
E = F
F = G
G = H
H = I
I = J
J = K
K = L
L = M
M = N
N = O
O = P
P = O
Q = R
R = S
S = T
T = U
U = V
V = W
W = X
X = Y
Y = Z
therefore
A = Z
therefore
C = D
T = Y
A = Z
P = Q
therefore
E = R
therefore
R = W
Alex is right cases:
H = J
therefore
J = H
K = L
K = X
therefore
L = X
C = B
B = A
therefore
A = B
K = L
K = X
K = P
therefore
L = X
L = P
X = P
A = Y
Y = Q
Q = O
therefore
O = Y
O = A
C = C
therefore
C = C
A = B
B = A
therefore
A = B
B = A
A = B
B = C
C = D
therefore
A = A
A = B
A = C
A = D
B = A
B = B
B = C
B = D
C = A
C = B
C = C
C = D
D = A
D = B
D = C
D = D
therefore
A = A
B = B
C = C
D = D
E = E
F = F
G = G
H = H
I = I
J = J
K = K
L = L
M = M
N = N
O = O
P = P
Q = Q
R = R
S = S
T = T
U = U
V = V
W = W
X = X
Y = Y
Z = Z
D = I
F = H
J = M
therefore
M = J
D = I
H = F
A = B
B = C
C = D
D = E
E = F
F = G
G = H
H = I
I = J
J = K
K = L
L = M
M = N
N = O
O = P
P = Q
Q = R
R = S
S = T
T = U
U = V
V = W
W = X
X = Y
Y = Z
therefore
Z = A
F = R
G = I
W = L
A = B
B = C
therefore
A = C
B = A
therefore
A = A
X = X
P = P
C = G
M = C
therefore
D = C
therefore
therefore
therefore
R = R
42PHP, 13 bytes
Alex is wrong
Verifies all test cases. – Dennis – 2015-10-22T18:48:19.36019Hey, sometimes is better than never. ¯\(ツ)/¯ – Alex A. – 2015-10-22T20:00:32.480
5[tag:alex-is-wrong] – Optimizer – 2015-10-22T20:06:46.470
3Also, -1 for lies – Optimizer – 2015-10-22T20:12:23.483
7
therefore\nTABS < SPACES
-->Alex is right
– Doorknob – 2015-10-22T20:51:42.8707Love to see a solution in prolog. – azz – 2015-10-24T10:53:00.443
@DerFlatulator i think prolog is considered a loophole :p – Abr001am – 2015-10-26T20:47:12.073
1@DerFlatulator I thought about it, but I don't know a good way to transform the input format into something that Prolog could make sense of... maybe someone who knows the language better could, though. – DLosc – 2015-10-27T22:36:56.133