28
\$\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\$
  • 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\$ – Greg Martin Jun 8 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\$ – Dominic van Essen Jun 8 at 10:21
  • 2
    \$\begingroup\$ @DominicvanEssen Alright, I will remove this option, \$\endgroup\$ – nph Jun 8 at 10:22

45 Answers 45

18
\$\begingroup\$

Python 2, 25 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
8
\$\begingroup\$

///, 60 64 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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Very cool language for the problem \$\endgroup\$ – JBernardo Jun 15 at 10:35
7
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Nice approach!! \$\endgroup\$ – Luis Mendo Jun 8 at 0:14
  • \$\begingroup\$ @LuisMendo Did you see dingledooper's Python answer? \$\endgroup\$ – user92069 Jun 8 at 0:47
  • \$\begingroup\$ @Memberfor3months I hadn't seen it. Thanks! \$\endgroup\$ – Luis Mendo Jun 8 at 10:11
6
\$\begingroup\$

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!

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

Gema, 20 characters

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

Sample run:

bash-5.0$ echo -n '***,****' | gema '*,*=@cmps{*;*;2;0;1}'
2

Try it online! / Try all test cases online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Bash + Standard utilities, 28 bytes

dc<<<1`tr *, 1d`1-dd*v/3+3%p

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

sed -E, 30 bytes

s/(.*),\1/0/;s/.+0/1/;s/0.+/2/

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

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)
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Perl 5 + -pl, 21 bytes

/,/;$_=$`cmp$';s;-1;2

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How many bytes if you port my oK answer? \$\endgroup\$ – streetster Jun 15 at 16:02
  • \$\begingroup\$ @streetster APL doesn't have a built-in for "split at ','". \$\endgroup\$ – Bubbler Jun 26 at 4:13
3
\$\begingroup\$

APL (Dyalog Unicode), 20 19 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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 13 bytes using your second answer as the starting point. \$\endgroup\$ – Bubbler Jun 8 at 7:05
  • \$\begingroup\$ @Bubbler thanks for the step by step! \$\endgroup\$ – RGS Jun 8 at 8:26
  • \$\begingroup\$ I would be interested in understanding how my answer isn't helpful and the reason it got downvoted :) \$\endgroup\$ – RGS Jun 15 at 10:41
  • \$\begingroup\$ (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). \$\endgroup\$ – Jonathan Allan Jun 15 at 11:29
  • \$\begingroup\$ @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 \$\endgroup\$ – RGS Jun 15 at 11:31
3
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @Shaggy Do you mean other than the one I'm linking to in my answer? \$\endgroup\$ – Arnauld Jun 8 at 10:23
  • \$\begingroup\$ Christ! I really can't function without caffeine, can I?! :\ \$\endgroup\$ – Shaggy Jun 8 at 11:36
3
\$\begingroup\$

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:#'","\
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

brainfuck, 57 bytes

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

Try it online!

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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Retina, 15 13 bytes

(.*),\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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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Î
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Java 10, 74 51 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
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 51 bytes if double as result is accepted \$\endgroup\$ – Olivier Grégoire Jun 8 at 15:20
  • 1
    \$\begingroup\$ @OlivierGrégoire Oh, nice approach. Maybe you should post it as a separated answer, since it's completely different than what I currently have. :) \$\endgroup\$ – Kevin Cruijssen Jun 8 at 15:27
  • 1
    \$\begingroup\$ @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! :) \$\endgroup\$ – Kevin Cruijssen Jun 8 at 15:43
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Olivier Grégoire Jun 8 at 15:45
  • 1
    \$\begingroup\$ A further byte can be saved by replacing "," with 44 (which is the codepoint for the comma). \$\endgroup\$ – Olivier Grégoire Jun 8 at 17:00
2
\$\begingroup\$

Ruby -n, 21 20 bytes

p (~/,/*2<=>~/.$/)%3

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That's cool! I think it's its own answer. \$\endgroup\$ – histocrat Jun 9 at 1:05
  • \$\begingroup\$ Posted :) \$\endgroup\$ – Dingus Jun 9 at 2:46
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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);}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @ceilingcat Thank you. \$\endgroup\$ – Jonathan Frech Jun 11 at 22:43
2
\$\begingroup\$

Excel, 64 63 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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 63 bytes by using >=: =IF(LEN(A1)+1=2*FIND(",",A1),0,IF(LEN(A1)>=2*FIND(",",A1),2,1)) \$\endgroup\$ – Dominic van Essen Jun 8 at 15:48
  • \$\begingroup\$ Can't you save another byte by changing >= to <, and swapping the 2 and the 1? (i.e. inverting the Logical Condition) \$\endgroup\$ – Chronocidal Jun 10 at 13:16
  • \$\begingroup\$ 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) \$\endgroup\$ – Shazback Jun 15 at 11:57
2
\$\begingroup\$

PHP, 55 51 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!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ For fun, here's what I came up with before I saw yours - the same size actually! tio.run/… \$\endgroup\$ – 640KB Jun 19 at 12:48
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby -nlaF,, 22 bytes

p ($F[0]<=>$F[1]||1)%3

Try it online!

| improve this answer | |
\$\endgroup\$

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.