CodeGolf - Barry the messy developer #2

Perl, 35

#!perl -pl
/,/;$_=$`-$'&&$`>$'^/D/?STOP:WAIT

Test me.


CJam, 31 29 27 characters

"㫅㍸ꕆ敟鸢Ꝓ約䢫솓儓隆뻨"2G#b128b:c~

This is just an encoded version of the following code (in order to make use of the scoring by characters):

r{',/:~3<)(f*~<"STOP""WAIT"?Nr}h

Run all test cases here.

There might be a way to shorten this by encoding STOP and WAIT, but I'm quite happy with the rest.

Explanation

The code is surrounded by a loop which reads on line at a time, processes it, then pushes a newline, and reads the next line... The loop terminates once r returns an empty string (i.e. after all lines have been processed). That's this bit:

r{ ... Nr}h

As for processing each line, I'm making use of the fact that upper case letters are variables in CJam, so I can eval some of the input.

',/:~3<)(f*~<"STOP""WAIT"?
',/                        e# Split the input on commas.
   :~                      e# Eval each of the three resulting strings. The first two
                           e# will yield the prices, the third will dump a bunch of
                           e# values corresponding to the variables DNOPUW in the array.
     3<                    e# Truncate to three elements, so we only get the prices and
                           e# the values corresponding to U (0) and D (13).
       )(                  e# Slices off that variable value and decrement it, to get
                           e# something negative for UP and positive for DOWN.
         f*                e# Multiply both numbers by that value. So if we had UP then
                           e# both numbers will be negative now, otherwise they'll just
                           e# be scaled without affecting their relative size.
           ~<              e# Unwrap the array and check which element is larger.
             "STOP""WAIT"? e# Select the desired output string based on this boolean.

So the catch is that for UP we invert the relative sizes of the prices, so that we can cover all cases with a single inequality at the end.


Perl, 77 73 bytes

while(<>){@p=split",";print($p[0]<$p[1]and$p[2]=~/D/?"STOP":"WAIT")."\n"}

Here's how it works:

  • while(<>) parses every line.
  • @p=split"," splits it by every comma. It's using the default Perl operator, $_ (which is where the line is stored.)
  • print (ternary) determines what to print.
  • $p[0]<$p[1]and$p[2]=~/D/ asks if the current price is less than the price we want, and it's going down (by checking for a D.)
  • (condition)?(if):(else) is the ternary operator.
  • If our condition earlier matched, it'll output STOP. Otherwise, it'll output WAIT.

I'm assuming there is no trailing newline on the input - a trailing newline produces an extra WAIT.

Thanks to Alex A. for helping me save 4 bytes!