29
\$\begingroup\$

The challenge is to golf a program when given a string composed of any number of asterisks, then a comma, than any number of asterisks, say which side has more.

The asterisk-strings can be empty.

Output

0 for ties.

1 for the left.

2 for the right.

Examples

Input: ***,**** Output: 2

Input: ***, Output: 1

Input: ***,*** Output: 0

Input: , Output: 0

Scoring

This is . Shortest answer wins.

\$\endgroup\$
3
  • 17
    \$\begingroup\$ It seems this would be improved by loosening the output format—for example, allowing -1, 0, and 1 for the three possibilities, or really any three consistent outputs. \$\endgroup\$ Jun 8 '20 at 8:06
  • 12
    \$\begingroup\$ I'm not sure that this is Ok at this stage (already 25 answers). Some of the existing answers (including my own) already made some effort to adhere to the originally-specified output format. \$\endgroup\$ Jun 8 '20 at 10:21
  • 2
    \$\begingroup\$ @DominicvanEssen Alright, I will remove this option, \$\endgroup\$
    – nph
    Jun 8 '20 at 10:22

45 Answers 45

1
2
1
\$\begingroup\$

AWK -F,, 24 22 20+3 = 23 bytes

$0=$2>$1?2:($1>$2)""

Try it online!

Edit: amazingly, golfing-away 2 bytes has actually made it (a bit) more readable

Commented version:

$0=             # set the line to
    $2>$1 ?     # if $2>$1 (alphabetic comparison)
            2   # 2
    :           # otherwise
      ($1>$2)   # logical: 1 for $1>$2, 0 if not
             "" # append "" to force text output
                # otherwise awk won't print zero

# use default behaviour to print line
\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 48 bytes

>~:1+!#v_","-!#v_1+
^ p 0 +$#99 "-"<
+1`_: #<.@.

Try it online!

Adds one for each character it encounters, until it sees a ,; in that case, the program self modifies to start subtracting one for each character it encounters. After seeing the end of the string, it checks whether the tally is 0, less than 0, or something else, and prints 0, 2 or 1 accordingly.

\$\endgroup\$
1
\$\begingroup\$

R, 46 45 bytes

(or 42 bytes using TRUE/FALSE instead of 1/0)

d=scan(,'',,,',');`if`(d[2]>d,2,1-!d[2]<d[1])

Try it online!

Edit: -1 byte thanks to Giuseppe

\$\endgroup\$
3
  • \$\begingroup\$ You can use ,, in place of se= (forcing R to use the argument order rather than the names) to save a byte. Might also be able to use a sign(diff(* lengths))%%3 like I've seen in other answers to golf off some bytes. \$\endgroup\$
    – Giuseppe
    Jun 8 '20 at 14:36
  • \$\begingroup\$ Thanks for the ,,! Updated with acknowledgment... (again!) \$\endgroup\$ Jun 8 '20 at 15:32
  • \$\begingroup\$ ...and you should post the other approach (which is much better than mine and beats it by at least 7 bytes)... \$\endgroup\$ Jun 8 '20 at 15:34
1
\$\begingroup\$

SimpleTemplate, 66 bytes

This was quite fun!

Uses the most basic and naive approach, but the shortest one i can come up with as well.

{@ifargv.0 is equalargv.1}0{@elseifargv.0 is lowerargv.1}2{@else}1

That's quite a mess, lets explore it:

{@if argv.0 is equal to argv.1}
    {@echo 0}
{@else if argv.0 is lower than argv.1}
    {@echo 2}
{@else}
    {@echo 1}
{@/}

Should be self-explanatory.
A string is considered lower or higher depending on it's length and the characters on it are lower numerically (e.g.: b > a).

To turn into a function, all it needs is that the {@echo} is converted to {@return}.
Due to a bug (version 0.83), you can't return 0, but you can return "0".


You can test this on http://sandbox.onlinephpfunctions.com/code/683ec5b3e71cfb4f2bb78267125330b077248ef7

\$\endgroup\$
1
\$\begingroup\$

R, 38 bytes

sign(-diff(nchar(scan(,'',,,','))))%%3

Try it online!

Credit to Dominic van Essen for the sep argument to scan(), which I probably use less often than I should.

Ports the mod 3 approach found in a few other answers.

\$\endgroup\$
1
\$\begingroup\$

Golfscript, 22 bytes

','/).@).@;@>@@>2 0if+

Try it online!

code   | explanation                      | example
','/   | split input at ',' into list     | "*,**" -> ["*", "**"]
).     | uncons from the right and double | ["*", "**"] -> ["*"] "**" "**"
@).    | put list on tos and do it again  | ["*"] "**" "**" -> "**" "**" [] "*" "*"
@;     | discard empty list               | "**" "**" [] "*" "*" -> "**" "**" "*" "*"
@>     | pull up a "right" and compare    | "**" "**" "*" "*" -> "**" "*" 0
@@     | pull up remaining elements       | "**" "*" 0 -> 0 "**" "*"
>2 0if | compare, if 1 replace with two   | 0 "**" "*" -> 0 2
+      | add the results                  | 0 2 -> 2
\$\endgroup\$
1
\$\begingroup\$

Pyth, 16 bytes

%_._h.+mldcQ","3

Try it online!

\$\endgroup\$
1
1
\$\begingroup\$

Python 3.8, 32 bytes

lambda s:((r:=s[::-1])!=s)+(r<s)

Try it online!

It is just one byte shorter than other Python 3.x

lambda s:(s[::-1]!=s)+(s[::-1]<s)
\$\endgroup\$
0
0
\$\begingroup\$

Charcoal, 18 bytes

⊞υθ⊞υ⮌θI⁻²↨⮌⌕Aυ⌈υ²

Try it online! Link is to verbose version of code. Based on @JonathanAllen's answer. Explanation:

⊞υθ⊞υ⮌θ

Push the input and its reverse to the predefined empty list.

         υ  List of input and reverse
        ⌈   Take the maximum
     ⌕Aυ    Get the indices of that in the list
    ⮌       Reverse the list of indices
   ↨      ² Convert from base 2
 ⁻²         Subtract from 2
I           Cast to string
            Implicitly print

When the input equals its reverse, both indices 0, 1 are returned, and 10 equals 2 in base 2, while if the left side has more then the maximum is at index 1, while if the right side has more then the maximum is at index 0. Subtracting this from 2 results in the desired value.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 14 bytes

[ÂQ#',¡ć@>,q}0

Try it online!

A weird program method, I know, but hey, it works! The ÂQ part is from this answer regarding 05AB1E golfing tips

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 43 bytes

DECLARE @ varchar(max)='*,**'

PRINT(3+sign(charindex(',',@)*2+~len(@)))%3
\$\endgroup\$
0
\$\begingroup\$

Octave / MATLAB, 41 38 bytes

@(x)mod(sign(2*find(x-42)-nnz(x)-1),3)

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

PowerShell, 59 bytes

$a,$b=$i.split(',');if($a-gt$b){1}elseif($a-eq$b){0}else{2}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stax, 9 7 bytes

üJN☺ïS♂

Run and debug it

Unpacked:

',/E|$3%

-2 bytes thanks to recursive

\$\endgroup\$
1
  • \$\begingroup\$ You can omit {%m, as comparisons work on strings too. \$\endgroup\$
    – recursive
    Jun 18 '20 at 23:41
0
\$\begingroup\$

FEU, 30 26 bytes

Saved 4 bytes by using map instead of multiple substitutes

m/(\**),\1/0/\*+0/1/0\*+/2

Try it online!

Explanation

m                          # Map mode, execute multiple substitutions
 /(\**),\1/0               # Replaces the balance of the sides with 0
            /\*+0/1        # If there are asterisks on the left of 0, replace with 1
                   /0\*+/2 # If there are asterisks on the right of 0, replace with 2
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.