8
0
Alice, Bob, Carol, Dave, and Eve are going out for a nice game of golf and need your help to decide in what order they will play.
Your program will input some statements, which are defined as a condition, and then one or more optional logical boolean operators followed by another condition. (That's [Condition]([Logical][Condition])*
in regexp notation.) A logical boolean operator is "and" or "or." You may chose either and
or or
to have higher precedence, as long as it is consistent.
A condition is a name, followed by the word "is," followed optionally by the word "not," followed by one of these:
- in front of
- behind
- n spaces in front of
- n spaces behind
- next to
- nth
- nth to last
An example of a valid condition is "Alice is not next to Bob".
Associativity is from left to right, except for "not," which only applies to a single statement. This means that "x or y and z or not a" means "((x or y) and z) or a," where x could be "Alice is 1st" or "Alice is behind Bob" or "Alice is not 2nd to last."
Your output must be the order in which the players line up, separated by commas, from front to back of the line.
Here's the full specification in regexp notation:
[Statement] = [FullCondition]([Logical][FullCondition])*
[FullCondition] = [Name] is [Condition]
[Condition] = (not)? (in front of|behind|[Number] space(s)? (in front of|behind)|next to|[Ordinal]|[Ordinal] to last)
[Logical] = and|or
[Number] = 1|2|3|4...
[Ordinal] = 1st|2nd|3rd|4th...
Standard methods of input and output. Input should be as a string, and output can be as a string or list. And finally, here are some sample inputs and outputs.
Input:
The players are Alice, Bob, and Carol.
Alice is 2nd.
Bob is in front of Alice.
Output:
Bob, Alice, Carol
Input:
The players are Alice, Bob, and Carol.
Alice is 1 space in front of Bob. // remember to handle pluralization correctly!
Carol is 1st to last.
Output:
Alice, Bob, Carol
Input:
The players are Alice, Bob, and Carol.
Alice is in front of Bob.
Output:
Alice, Bob, Carol
Carol, Alice, Bob // multiple outputs may be required
Input:
The players are Alice, Bob, and Carol.
Alice is in front of Bob.
Bob is in front of Alice.
Output:
// nothing
Input:
The players are Alice, Bob, and Carol.
Alice is not in front of Bob and Carol is not 2nd or Bob is 1st.
Output:
Carol, Bob, Alice
Bob, Alice, Carol
Bob, Carol, Alice
This is code-golf, so the shortest code in bytes will win!
A great many thanks to @Doorknob for posting this in the Secret Santa's Sandbox. Again, thank you.
This was one I was going to take over but you really should repost it in sand box as it may need work – Christopher – 2017-04-04T15:16:34.313
I worded it wrong. I looked at taking it over and never posted anything on it :P I instead posted a different one. But it may be finished. – Christopher – 2017-04-04T15:32:27.780
Are lists a valid output format? So for example
{{Alice, Bob, Carol}, {Carol, Alice, Bob}}
for the third test case, and{{Alice, Bob, Carol}}
for the second? – Greg Martin – 2017-04-04T16:25:57.770@GregMartin Yes, I'll edit. – Comrade SparklePony – 2017-04-04T16:26:42.543
1Commonly in mathematics,
and
is taken to have higher precedence thanor
. (and
is multiplication in the boolean algebra, whileor
is sort of addition.) Can the solver choose which convention (this, or left-associativity) they'll consistently use? – Greg Martin – 2017-04-04T16:27:41.5801@GregMartin Yes. Consistently. – Comrade SparklePony – 2017-04-04T17:54:01.800
1This seems another challenge for Prolog! – orlp – 2017-04-04T19:47:09.967
Your regexp seems to allow statements like "Alice is next to Bob or behind Carol and Bob is 2nd to last or not 2 spaces in front of Carol" (note the omission of the subject's name in two of the four condition clauses). Is this intentional? And if so, does this example conflict with left-associativity, with which "Bob is 2nd to last" is supposed to be bound more tightly to "Alice is next to Bob or behind Carol" than to "not 2 spaces in front of Carol"? (And if so, more test cases please!) – Greg Martin – 2017-04-04T19:48:19.583
@GregMartin
((((Alice is next to Bob) or [Alice is] behind Carol) and Bob is 2nd to last) or [Alice is] not 2 spaces in front of Carol)
. Left associativity is the rule here. This does not contradict the last test case because(Alice is not in front of Bob) and [Alice is] Carol
does not make sense, which would make the entire parenthesis-enclosed version((Alice is not in front of Bob) and (Carol is not 2nd)) or (Bob is 1st)
, and everything works out. Left-associativity is not conflicted, and this is intentional. Does that answer your question? – Comrade SparklePony – 2017-04-04T20:54:55.343I truly don't see how your parsing of my example makes sense. How can the subject "Alice" be more tightly bound to "not 2 spaces in front of Carol" than "Bob", with all those nested parentheses in the way? I don't think either left-associativity or a common-sense reading supports that parsing. Perhaps, since you don't have any relevant test cases anyway, you can just change the spec to
[Statement] = [FullCondition]([Logical][FullCondition])*
. – Greg Martin – 2017-04-04T21:28:01.893@GregMartin Yeah, I see my mistake, sorry. But thank you for the new regexp, I think it makes more sense for this challenge. I will edit it in. – Comrade SparklePony – 2017-04-04T21:46:14.143
oh good, I thought maybe I was going crazy.... – Greg Martin – 2017-04-04T23:16:46.570
Can we assume something about the names? Are they always a subset of
{Alice, Bob, Carol, Dave, Eve}
or do we have to handle others? – PurkkaKoodari – 2017-04-05T07:31:07.497@Pietu1998 Yes, it is okay. – Comrade SparklePony – 2017-04-05T13:24:59.383
@Arnauld This is not okay, but I do not expect it to be to hard to fix. Sorry. – Comrade SparklePony – 2017-04-05T13:25:52.287
Does
next to
equal1 space behind
or also1 space in front of
? – Titus – 2017-04-05T14:39:53.523@Titus Yes, it does.
next to = ((1 space behind) or (1 space in front of))
. – Comrade SparklePony – 2017-04-05T15:03:43.997