Strategy

No attempts yetTime limit1sMemory limit128 MB

Problem

A well-known psychology experiment has two players repeatedly play a game in which, on each encounter, each player independently chooses to either TRADE with the other player or CHEAT them. The score for a single encounter is:

  • If both players TRADE, each gains 1 point.
  • If one TRADEs and the other CHEATs, the one who TRADEd loses 2 points and the one who CHEATed gains 2 points.
  • If both CHEAT, each loses 1 point.

Many people cannot find a winning strategy, or fail to stick to one, so it is fairer to compare strategies by simulating them on a computer. Each strategy is played by an automaton with three parts: a program that encodes the strategy, a memory of previous encounters, and a running score. The score starts at 0 and is updated after every encounter using the rules above. The memory can look up what happened in up to the last two encounters against the current opponent.

Read up to 10 strategies. Play every strategy against every other strategy (never against itself) for exactly 10 encounters, keeping a separate memory for each pair of opponents. On every encounter both automata choose their move simultaneously from the memory of earlier encounters, then both memories are updated. After all matches, report each strategy's final score.

Each strategy is a small program in the following grammar:

<program>   ::= <statement>.
<statement> ::= <command> | <ifstat>
<ifstat>    ::= IF <condition> THEN <statement> ELSE <statement>
<condition> ::= <cond> | <cond> <op> <condition>
<op>        ::= AND | OR
<cond>      ::= <memory> {= | #} {<command> | NULL}
<memory>    ::= {MY | YOUR} LAST {1 | 2}
<command>   ::= TRADE | CHEAT
  • LAST1 refers to the previous encounter between these two automata and LAST2 to the encounter before that.
  • MY refers to this automaton's own past moves; YOUR refers to the opponent's past moves.
  • '=' means "is equal to" and '#' means "is not equal to".
  • NULL means the encounter has not happened yet (for example, LAST1 is NULL during the first encounter and LAST2 is NULL during the first two encounters).
  • A condition is a list of simple comparisons joined by AND / OR with no operator precedence; they are grouped from right to left (a op b op c means a op (b op c)).
  • Spaces and line breaks may appear anywhere in a program and are only for readability.

For example, these are all valid programs:

CHEAT.
IF MY LAST1 = CHEAT THEN TRADE ELSE CHEAT.
IFYOURLAST2=NULLTHENTRADEELSEIFYOURLAST1=TRADETHENTRADE
ELSECHEAT.

Input

The input is a series of programs. Each program is at most 255 characters long and may be split across several lines for convenience. There are at most 10 programs. The input ends with a line that contains only a single '#'.

Output

Print one line per program, in input order. Each line contains that program's final score, right-justified in a field of width 3.