Which side has more?

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.

• 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. Jun 8, 2020 at 8:06
• 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. Jun 8, 2020 at 10:21
• @DominicvanEssen Alright, I will remove this option,
– nph
Jun 8, 2020 at 10:22

Python 2, 25 bytes

lambda s:cmp(s[::-1],s)%3


Try it online!

///, 6064 28 bytes

/*,*/,//,*/2//*,/1//,/0//*//


Try it online!

Found out most of my program was unnecessary, -32 bytes.

I made this program when I was learning the language, and decided to post it as a question.

• Very cool language for the problem Jun 15, 2020 at 10:35

05AB1E, 6 bytes

Â.S(3%


Try it online!

How?

Note that ',' is greater than '*'.

Â.S(3%
Â      - input, reversed(input) (say a, b)
.S    - compare: 1 if a > b; -1 if a < b; 0 if a = b
(   - negate
3  - push three
% - modulo

• Nice approach!! Jun 8, 2020 at 0:14
• @LuisMendo Did you see dingledooper's Python answer?
– user92069
Jun 8, 2020 at 0:47
• @Memberfor3months I hadn't seen it. Thanks! Jun 8, 2020 at 10:11

C (gcc), 65 $$\\cdots\$$ 54 47 bytes

Saved a 10 bytes thanks to dingledooper!!!
Saved a byte thanks to ceilingcat!!!
Saved 7 bytes thank to l4m2!!!

f(c){c=strlen(c)-strspn(c,"*")*2;c=c>1?2:c!=1;}


Try it online!

• f(c){c=strlen(c)-strspn(c,"*")*2;c=c>1?2:c!=1;} if high address not used
– l4m2
Jun 15, 2020 at 9:31
• @l4m2 Works on TIO, so I guess it's allowed - thanks! :D Jun 15, 2020 at 10:13

Gema, 20 characters

*,*=@cmps{*;*;2;0;1}


Sample run:

bash-5.0$echo -n '***,****' | gema '*,*=@cmps{*;*;2;0;1}' 2  Bash + Standard utilities, 28 bytes dc<<<1tr *, 1d1-dd*v/3+3%p  Try it online! sed -E, 30 bytes s/(.*),\1/0/;s/.+0/1/;s/0.+/2/  Try it online! K (ngn/k), 17 16 bytes Solution: {2/c<|c:#'","\x}  Try it online! Explanation: {2/c<|c:#'","\x} / the solution -> e.g. 1 e.g. 2 e.g. 3 { x} / lambda taking implicit 'x' -> "***,****" "***," "***,***" ","\ / split string on comma -> ("***";"****") ("***";"") ("***";"***") #' / count length of each -> 3 4 3 0 3 3 c: / store as c -> 3 4 (noop) 3 0 3 3 | / reverse it -> 4 3 0 3 3 3 c< / is c less than this? -> 1 0 0 1 0 0 2/ / convert from base 2 -> 2 1 0  Extra: • 13 bytes if it doesn't need to be a function 2/c<|c:#'","\ Jelly, 7 6 bytes ,ṚMḄ2c  A monadic Link accepting a list of characters which yields an integer in $$\[0,2]\$$. Try it online! How? Note that ',' is greater than '*'. ,ṚMḄ2c - Main Link: s e.g.: "*," ",*" "," Ṛ - reverse (s) ",*" "*," "," , - pair (s) with (that) ["*,",",*"] [",*","*,"] [",",","] M - indices of maximal values [2] [1] [1,2] Ḅ - convert from base 2, say x 2 1 4 2 - two 2 2 2 c - (2) choose (x) - i.e. binomial(2,x) 1 2 0 - implicit print (a list with a single element prints the element)  Perl 5 + -pl, 21 bytes /,/;$_=$cmp$';s;-1;2


Try it online!

APL (Dyalog Extended), 9 bytes

(⊥⍋|⍒)⊢⍮⌽


Try it online!

How it works

(⊥⍋|⍒)⊢⍮⌽  ⍝ left '*,' | right ',*' | equal ','
⊢⍮⌽  ⍝ Length-2 nested vector of self and reverse
⍋        ⍝ Grade up; order of indices to make it ascending-sorted
⍝ 1 2       | 2 1        | 1 2
⍒      ⍝ Grade down; order of indices to make it descending-sorted
⍝ 2 1       | 1 2        | 1 2
|       ⍝ Right modulo left
⍝ 0 1       | 1 0        | 0 0
⊥         ⍝ From base 2 to integer
⍝ 1         | 2          | 0

• How many bytes if you port my oK answer?
– mkst
Jun 15, 2020 at 16:02
• @streetster APL doesn't have a built-in for "split at ','". Jun 26, 2020 at 4:13

APL (Dyalog Unicode), 2019 13 bytesSBCS

Whooping -6 thanks to @Bubbler.

(⊃3|⍒-⍋)⊂,⊂∘⌽


Try it online! This should be golfable as there are some things I am repeating there but I am not sure how to do it yet.

APL (Dyalog Unicode), 19 bytesSBCS

{3|×1+(2×⍵⍳',')-≢⍵}


Try it online!

• 13 bytes using your second answer as the starting point. Jun 8, 2020 at 7:05
• @Bubbler thanks for the step by step!
– RGS
Jun 8, 2020 at 8:26
• I would be interested in understanding how my answer isn't helpful and the reason it got downvoted :)
– RGS
Jun 15, 2020 at 10:41
• (1 of 2) 05AB1E, Jelly, Japt and (2 of 2) APL answers downvoted - I imagine someone does not think we're counting bytes, (only ones missing would be Charcoal and Stax, maybe they didn't go to page 2). Jun 15, 2020 at 11:29
• @JonathanAllan that is weird, I even link the SBCS wiki page so people can rest assured I'm really counting bytes :/ these downvote floods always confuse me
– RGS
Jun 15, 2020 at 11:31

JavaScript (ES6),  26  25 bytes

Saved 1 byte thanks to @tsh

Taking advantage of the looser output rules: this versions returns 0 for ties, undefined for left or * for right.

s=>(s+0)[s.search,*2+1]


Try it online!

JavaScript (ES6), 31 bytes

Returns a Boolean value instead of 0 / 1.

s=>([a,b]=s.split,,a<b?2:a>b)


Try it online!

• @Shaggy Do you mean other than the one I'm linking to in my answer? Jun 8, 2020 at 10:23
• Christ! I really can't function without caffeine, can I?! :\ Jun 8, 2020 at 11:36

Ruby -nl, 18 bytes

~/,/;p ($<=>$')%3


Try it online!

Takes input from STDIN. Compares the regex match groups $ and $', which are set equal to everything on the left and right of the comma, respectively.

Io, 51 bytes

Mod3 doesn't work in Io. Too bad.

method(x,I := -x compare(x reverse);if(I== -1,2,I))


Try it online!

brainfuck, 57 bytes

-[>>,<--[>-<++++++]>-]<+[<<,]>[<-->+[<+>+]]-[<+>-----]<-.


Try it online!

It spreads * on the tape and checks if the second branch went past the first.

(.*),\1$\*+  Try it online! If the right-hand-side has an equal or less number of *'s, then those *'s are removed from both sides, along with the ,. Then the number of runs of *'s are counted. Japt, 9 8 bytes Returns true/false instead of 1/0 >Ô?2:U<Ô >Ô?2:U<Ô :Implicit input of string U > :Greater than Ô : U reversed ? :If true 2 : Literal 2 : :Else U<Ô : U less than U reversed?  Try it Original, 9 bytes 5 bytes just to handle the I/O requirements :\ q, mÊrÎu3  Try it q, mÊrÎu3 :Implicit input of string q, :Split on "," m :Map Ê : Length r :Reduce by Î : Sign of difference u3 :Positive modulo 3  If we could take a space delimited string as input then -2 bytes: ¸mÊrÎu3  If we could take an array as input then -1 more byte: mÊrÎu3  And, if we could use any 3 distinct values for the output then -2 more bytes: mÊrÎ  Java 10, 7451 50 bytes s->(Long.signum(s.indexOf(44)*2-s.length()+1)+3)%3  -24 bytes thanks to @OlivierGrégoire. Try it online. Explanation: s->{ // Method with String parameter and long return-type Math.signum( // Take the signum of: s.indexOf(44) // The index of ',' (codepoint 44) *2 // multiplied by 2 -s.length() // Subtract the entire length of the input +1 // And add 1 +3) // Then increase that result by 3 %3 // And take modulo-3 on it  • 51 bytes if double as result is accepted Jun 8, 2020 at 15:20 • @OlivierGrégoire Oh, nice approach. Maybe you should post it as a separated answer, since it's completely different than what I currently have. :) Jun 8, 2020 at 15:27 • @OlivierGrégoire Ah ok, from that 72 byter to your 51 byter makes indeed sense. If you don't want to post it, I will edit. Thanks! :) Jun 8, 2020 at 15:43 • F.Y.I. I just noticed that Long.signum exists. It might be better than Math.signum so that there's not this trailing .0. Jun 8, 2020 at 15:45 • A further byte can be saved by replacing "," with 44 (which is the codepoint for the comma). Jun 8, 2020 at 17:00 Ruby -n, 21 20 bytes p (~/,/*2<=>~/.$/)%3


Try it online!

• That's cool! I think it's its own answer. Jun 9, 2020 at 1:05
• Posted :) Jun 9, 2020 at 2:46

J, 19 18 bytes

3|i.&','*@--:@<:@#


Try it online!

How it works

3|i.&','*@--:@<:@#
-:@<:@# halved (length-1) of list
(where , would be in a balanced list)
i.&','           position of the ,
*@-        signum'd difference between both
3|                 mod 3


Alternative version, 19 bytes

This inserts - and + between the bit mask of the string: **,* -> 1 1 0 1 -> 1 + (1 + (0 - 1))) to get the difference between both sides. Then both signum and mod 3 like other solutions.

3|[:*'*'-+@.[/@:=]


Try it online!

How it works

3|[:*'*'-+@.[/@:=]
'*'         =] '***,*' -> 1 1 1 0 1
-+@.[      a function that chooses - or + based on the left argument
/@:   puts this function inbetween: 1+(1+(1+(0-1))) = 2
[:*               signum: 2 -> 1
3|                  mod 3


Befunge-93, 31 bytes

2>1~:1+v
p^_"$"2 0 .@^-","_$2\-


Try it online!

After initially pushing 2 to the stack, we push 1 for every * we encounter. After finding the ,, we start popping them instead. When we run out of input we subtract the top of the stack from 2, resulting in the correct output values.

Excel,  44  40 Bytes

=MOD(3-SIGN(1+LEN(A1)-2*FIND(",",A1)),3)


Finds the difference between Total Length + 1 and twice the position of the comma (Negative: Left; Zero: Tie; Positive: Right), use SIGN to convert these into -1, 0 and 1. Then subtract this from 3 to give 4/0/2, and take the Modulo Base 3 (1, 0, 2)

Old version: 44 Bytes

=MID(102,2+SIGN(1+LEN(A1)-2*FIND(",",A1)),1)


Finds the difference between Total Length + 1 and twice the position of the comma (Negative: Left; Zero: Tie; Positive: Right), use SIGN to convert these into -1, 0 and 1, add 2 (1, 2, 3), and then use MID to take the first, second or third digit from 102, respectively.

(Using MID saved 3 bytes over using CHOOSE)

I also experimented with a more maths-based approach (Multiply the Sign by 1.5, round it in the Positive direction with CEILING, then take the Absolute value), but that was 51 bytes instead.

Python 3, 56 60 bytes

-4 bytes thanks to math junkie

A third approach to this in python.

def f(x):a,b=x.split(',');return(1,0,2)[((a<b)-(a>b))+1]


Try it online!

• You can remove some unnecessary whitespace: Try it online! Jun 10, 2020 at 4:51

C (gcc) - 67 bytes

This is a complete program and can be compiled with gcc. Input is the first command line argument and output is the exit status.

main(u,v)int**v;{u=strlen(*++v)-strspn(*v,"*")*2;exit(u>1?2:u!=1);}


C (gcc), 83 bytes

x;f(char*z){char*Z,*W;for(Z=z;44-*Z++;);for(W=Z;*W++;)Z--;x=!!x*((x=z-Z+1)<0?1:2);}


Try it online!

C (gcc), 58 bytes

f(z,Z)long z,Z;{Z-=strlen(Z=index(z,44))+z-1;z=Z<0?2:!!Z;}


Try it online!

• @ceilingcat Thank you. Jun 11, 2020 at 22:43

Excel, 6463 62 bytes

=IF(LEN(A1)+1=2*FIND(",",A1),0,IF(LEN(A1)<2*FIND(",",A1),1,2))


-1 byte thanks to Dominic van Essen -1 byre thanks to Chronocidal

• 63 bytes by using >=: =IF(LEN(A1)+1=2*FIND(",",A1),0,IF(LEN(A1)>=2*FIND(",",A1),2,1)) Jun 8, 2020 at 15:48
• Can't you save another byte by changing >= to <, and swapping the 2 and the 1? (i.e. inverting the Logical Condition) Jun 10, 2020 at 13:16
• A few bytes can be saved by subtracting from 2 rather than using conditionals:. =2-(find(",",A1)>Len(a1)/2)-(find(",",A1)*2=Len(A1)+1) Jun 15, 2020 at 11:57

PHP, 5551 47 bytes

fn($s)=>(3+(($a=explode(',',$s))[0]<=>$a[1]))%3


Try it online!

This is the best I have so far.. Gosh, longer than Java and C :O

EDIT: saved 4 bytes using value of $v, now shorter than C! EDIT2: thanks a lot to 640KB for finding the finely elegant way to have the right numbers! -4 bytes Much shorter with only distinct values instead of fixed numbers requirement: PHP, 39 bytes fn($s)=>($a=explode(',',$s))[0]<=>$a[1]  Try it online! • For fun, here's what I came up with before I saw yours - the same size actually! tio.run/… Jun 19, 2020 at 12:48 Python 3.8 (pre-release), 62 bytes lambda x:y.index(max(y))+1if len({*(y:=x.split(","))})>1else 0  Try it online! I know that there is a shorter python solution already posted, but cmp doesn't exist in python 3. And don't bother trying to use bitwise operators to increment the value... it ends up being the same byte count. Ruby-nlaF,, 22 bytes p ($F[0]<=>\$F[1]||1)%3


Try it online!