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