Code Golf Golf Score

22

1

It's Friday... so let's go golfing! Write code that determines the player's scoring on a hole in a game of golf. The code can be either a function or entire program. As the genre suggests, shortest code wins.

Input (parameters or stdin, your choice):

  • An integer representing the hole's par, guaranteed to be between 3 and 6
  • An integer representing the golfer's score, guaranteed to be between 1 and 64

Output (print to stdout or return, trailing newline allowed but not required, your choice):

  • if score is 1, output "Hole in one"
  • if score == par - 4 and par > 5, output "Condor"
  • if score == par - 3 and par > 4, output "Albatross"
  • if score == par - 2 and par > 3, output "Eagle"
  • if score == par - 1, output "Birdie"
  • if score == par, output "Par"
  • if score == par + 1, output "Bogey"
  • if score == par + 2, output "Double Bogey"
  • if score == par + 3, output "Triple Bogey"
  • if score > par + 3, output "Haha you loser"

EDIT Congrats to Dennis on having the shortest answer!

Josh

Posted 2016-03-04T16:49:34.453

Reputation: 2 783

34I always wondered what was after triple bogey. – ThisSuitIsBlackNot – 2016-03-04T19:27:12.220

3Incidentally the largest par is 7 not 6. – Joshua – 2016-03-04T20:57:56.143

4@Joshua I was temporarily confused about why you commented instead of edited your own post. Then it hit me. :P – Rɪᴋᴇʀ – 2016-03-04T21:25:17.307

@RikerW the two Josh's names are as close as their reputation :D – cat – 2016-03-04T21:55:52.480

What if ​ score == 1 ​ and ​ par == 2 ​ ? ​ ​ ​ ​ – None – 2016-03-04T22:15:15.410

@RickyDemer: Par is not allowed to be less than 3; however if it were you would you would output "Hole in one". – Joshua – 2016-03-04T22:16:17.303

What is the point of the "and par > X" clauses? Surely the "Hole in one" criteria being checked first would make that step unnecessary? – Darrel Hoffman – 2016-03-04T22:38:27.670

2Can the input be in any order? – Doorknob – 2016-03-05T01:26:54.143

@Doorknob input can be in any order. – Josh – 2016-03-07T13:20:26.130

Answers

6

Jelly, 61 bytes

_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»
瓵ḣ⁻×⁵ñBƑ»’?

Try it online!

Background

This uses Jelly's static dictionary compression. You can find a compressor here. This way,

“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»

and

“Bogey“Double Bogey“Triple Bogey“Haha you loser“Condor“Albatross“Eagle“Birdie“Par”

as well as

“µḣ⁻×⁵ñBƑ»

and

“Hole in one”

are equivalent.

How it works

_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Helper link. Arguments: score, par

_                                                Subtract the par from the score.
 «4                                              Cap the difference at 4.
   ị                                             Index into the list at the right.
    “Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Yield a list of strings.


瓵ḣ⁻×⁵ñBƑ»’?  Main link. Arguments: score, pair

            ?  If...
           ’   the decremented score if non-zero:
ç                Call the helper link on both input arguments.
 “µḣ⁻×⁵ñBƑ»      Else, return “Hole in one”.

Dennis

Posted 2016-03-04T16:49:34.453

Reputation: 196 637

Congrats on the shortest answer! – Josh – 2016-03-15T02:52:41.330

13

PHP 5.3+, 173 167 166 159 156 151 127 121 bytes

echo[Condor,Albatross,Eagle,Birdie,Par,$b=Bogey,"Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]?:"Hole in one";
Notice-free version, 139 137 bytes
echo$s-1?["Condor","Albatross","Eagle","Birdie","Par",$b="Bogey","Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]:"Hole in one";

Set $score and $par variables before the echo and you're off.

exploded view
echo [Condor,
      Albatross,
      Eagle,
      Birdie,
      Par,
      $b = Bogey,
      "Double $b",
      "Triple $b",
      "Haha you loser"][ min( 4+$s-$p,8 ) ]
  ?: "Hole in one";

Edits
-6: Not storing the array, just calling it if need be.
-1: Flipping the ternary around.
-7: The lowest $s-$p with $s>1 is -4, so the max() isn't necessary, since 4+$s-$p >= 0.
-3: Array -> explode(), thanks CoolestVeto!
-5: Cheaty string literal undefined constant plus $r[-1] -> false, thanks Ismael Miguel!
-24: Moving from an explode() function to a printf/%s setup, with some tweaks, more thanks to Ismael Miguel for the change of direction.
-6: Swerve, we're back to echo again!

ricdesi

Posted 2016-03-04T16:49:34.453

Reputation: 499

1Can you turn it into one string and split by an arbitrary character? – Addison Crump – 2016-03-04T17:17:38.860

@CoolestVeto As a matter of fact, I can. Weirdly, it saves fewer bytes than expected, but any bytes are better than no bytes! – ricdesi – 2016-03-04T17:20:46.163

1You can replace $b="Bogey"; with $b=Bogey; to save 2 bytes. Also, replace your $s-1? ... : ...; with an echo ... ?: ...;. Here's the 151 bytes long version: function g($s,$p){$b=Bogey;echo explode("-","Condor-Albatross-Eagle-Birdie-Par-$b-Double $b-Triple $b-Haha you loser")[min(4+$s-$p,8)]?:"Hole in one";} – Ismael Miguel – 2016-03-04T19:09:29.620

Had no idea you could cheat string literals, neat. – ricdesi – 2016-03-04T19:15:41.337

Actually, it isn't a string literal anymore. It is an undefined constant. PHP hates those, and pretends that it is a string. Also, notice that the code I've provided won't work on older PHP versions. The ?: operator was introduced on PHP5.3. It provides a default value if the first value is falsey. – Ismael Miguel – 2016-03-04T19:20:43.337

Yeah, it's a favorite of mine (?:, that is), I wind up having to double check my environment all the time to make sure it's copacetic. – ricdesi – 2016-03-04T19:21:54.463

If you don't like the warning you can use @Bogey, as it will "ignore" the warning. Also, can you try function g($s,$p){printf(explode(0,"Condor0Albatross0Eagle0Birdie0Par0%s0Double %s0Triple %s0Haha you loser")[min(4+$s-$p,8)]?:"Hole in one",Bogey);}? That one is 149 bytes long. I've used printf instead of echo and replaced the separator character with a number. Instead of string interpolation, I've decided to use the %s format, which is, literally, to indicate to replace it with a string. – Ismael Miguel – 2016-03-04T19:29:03.787

1Here's a (possibly) working and shorter one: printf([Condor,Albatross,Eagle,Birdie,Par,'%s','Double %s','Triple %s','Haha you loser'][min(4+$argv[1]-$argv[2],8)]?:'Hole in one',Bogey);. Basically: Removed the function declaration, removed the explore, removed the quotes, used an array and used $argv. The final code is 139 bytes long. – Ismael Miguel – 2016-03-04T19:39:32.770

PHP has implicit string literals for undefined identifiers? ಠ_ಠ what is this, perl? – cat – 2016-03-05T13:44:48.063

10

05AB1E, 91 90 bytes

Code:

-5+U“¥Ê€†€µ“ª"0Bogey"ДCondor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡“Haha€î loser“X0¹1Qm*@0ð:ðÛ

Explanation:

Part 1:

-5+                          # Computes Score - Par + 5
   U                         # Store in X
    “¥Ê€†€µ“ª                # Short for "Hole in one"
             "0Bogey"        # Push this string
                     Ð       # Triplicate

Part 2:

”Condor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡

This is the same as "Condor Albatross Eagle Birdie Par 0Bogey Double0Bogey Triple0Bogey" using string compression and string interpolation. We then split on spaces, using ð¡.

Part 3:

“Haha€î loser“                # Push "Haha you loser"
              X               # Push X
               0¹1Qm          # Compute 0 ^ (score == 1), this translates 1 to 0 and 
                                everything else to 1.
                    *         # Multiply the top two items
                     @        # Get the string from that position
                      0ð:     # Replace zeros with spaces
                         ðÛ   # Trim off leading spaces

Discovered a lot of bugs, uses CP-1252 encoding.

Try it online!

Adnan

Posted 2016-03-04T16:49:34.453

Reputation: 41 965

...Whoa. Nicely done. – cat – 2016-03-04T20:49:13.607

2@tac Thanks! :) – Adnan – 2016-03-04T20:59:11.980

This code kinda looks like the random Unicode messages that you get when you install a non-English .exe thing (if you have English as your language in Windows things). Looks spectacular, though! – clismique – 2016-03-09T06:49:40.290

@DerpfacePython Haha, the random unicode messages are partially part of the code and the other part is part of a compressed message. – Adnan – 2016-03-09T19:13:27.803

6

Vitsy, 131 bytes

D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
"rodnoC"
"ssortablA"
"elgaE"
"eidriB"
"raP"
"yegoB"
4m" elbuoD"
4m" elpirT"
"resol uoy ahaH"

Explanation:

D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
D1-)[                ]      If the second input is 1, do the bracketed stuff.
     "eno ni eloH"rZ;       Output "Hole in one" and quit.
r                           Reverse the stack.
 -                          Subtract the top two items.
  5+                        Add 5 to fix for negative values of score.
    D9/([  ]                If the result of that is greater than 8, do the stuff in brackets.
         X                  Remove the top item.
          9                 Push 9. This forces any items greater than 8 to be 9.
            m               Execute this number line.
             Z              Output everything in the stack.

This works by figuring out what the score is relative to the par, then executing different lines (and gaining different strings) thereof.

Try It Online!

Verbose Mode (for poops and giggles):

duplicate top item;
push 1;
subtract top two;
if (int) top is not 0;
begin recursive area;
toggle double quote;
push 14;
eval(stack);
capture stack as object with next;
 ;
eval(stack);
push input item;
 ;
push 14;
push length of stack;
capture stack as object with next;
push all ints between second to top and top;
toggle double quote;
reverse stack;
output stack as chars;
generic exit;
end recursive area;
reverse stack;
subtract top two;
push 5;
add top two;
duplicate top item;
push 9;
divide top two;
if (int) top is 0;
begin recursive area;
remove top;
push 9;
end recursive area;
goto top method;
output stack as chars;
:toggle double quote;
reverse stack;
capture stack as object with next;
push 13;
eval(stack);
capture stack as object with next;
push cosine of top;
toggle double quote;
:toggle double quote;
push inverse sine of top;
push inverse sine of top;
capture stack as object with next;
reverse stack;
push inverse tangent of top;
push 10;
push 11;
push length of stack;
push inverse cosine of top;
toggle double quote;
:toggle double quote;
push 14;
push length of stack;
g;
push 10;
push e;
toggle double quote;
:toggle double quote;
push 14;
push input item;
push 13;
reverse stack;
push input item;
B;
toggle double quote;
:toggle double quote;
reverse stack;
push 10;
push pi;
toggle double quote;
:toggle double quote;
push number of stacks;
push 14;
g;
capture stack as object with next;
B;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push 11;
flatten top two stacks;
capture stack as object with next;
duplicate top item;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push whether (int) top item is prime;
push input item;
reverse stack;
push tangent of top;
toggle double quote;
;
;
:toggle double quote;
reverse stack;
push 14;
push inverse sine of top;
capture stack as object with next;
push length of stack;
 ;
flatten top two stacks;
capture stack as object with next;
push number of stacks;
 ;
push 10;
factorize top item;
push 10;
push all ints between second to top and top;
toggle double quote;

Addison Crump

Posted 2016-03-04T16:49:34.453

Reputation: 10 763

"Push 9. This forces any items greater than 8 to be 9." Wat? – cat – 2016-03-04T20:33:34.957

@tac This is to force the input that would result in "Haha you loser" to go to the line containing "Haha you loser". – Addison Crump – 2016-03-04T20:42:16.170

5

JavaScript (ES6), 125 124 bytes

p=>s=>"Hole in one,Condor,Albatross,Eagle,Birdie,Par,Bogey,Double Bogey,Triple Bogey".split`,`[s-1&&s-p+5]||"Haha you loser"

Assign to a variable e.g. f=p=>s=>, then call it like so: f(6)(2) Par first, then score.

May be able to be shortened by combining the "Bogey"s.

ETHproductions

Posted 2016-03-04T16:49:34.453

Reputation: 47 880

An example of combining the bogeys is: ",Double ,Triple ".split\,`[k-1]+"Bogey"wherek=s-p`. – user48538 – 2016-03-04T19:03:33.747

Can I use your approach in my solution? – user48538 – 2016-03-04T19:07:19.217

@zyabin101 that isn't discouraged, as long as it isn't outright plagiarism – cat – 2016-03-04T21:32:57.243

@zyabin101 Thanks, I'll see if that makes it any shorter. And yes, feel free to use this approach in your answer. – ETHproductions – 2016-03-05T15:55:21.993

I'm already using this. – user48538 – 2016-03-05T16:16:47.857

4

LittleLua - 160 Bytes (non-competitive)

r()P=I B="Bogey"r()Z={"Hole in one","Condor","Albatross","Eagle","Birdie","Par",B,"Double "..B,"Triple "..B,"Haha, you loser"}p(I=='1'a Z[1]or Z[I-P+6]or Z[10])

I'm relatively certain I did this right.

Accepts two integers, par and player's score.

The version of Little Lua that I used to make this was uploaded after this challenge was posted, but was not edited afterwards. It's relatively obvious from the code that nothing has been added to simplify this challenge

LittleLua Info:

Once I am satisfied with the built ins and the functionality of Little Lua, source will be available along with an infopage.

LittleLua V0.02

Skyl3r

Posted 2016-03-04T16:49:34.453

Reputation: 399

2This is non-competitive because the file was uploaded to Dropbox 2 hours after the challenge was posted. – Mego – 2016-03-04T21:58:21.933

2https://github.com is much better at code hosting... – cat – 2016-03-04T22:28:40.653

Absolutely, I just havent had a chance to set it up. – Skyl3r – 2016-03-04T22:29:45.800

3

Mouse-2002, 223 207 bytes

Removing comments would likely help...

??s:p:s.1=["Hole in one"]s.p.4-=p.5>["Condor"]s.p.3-=p.4>["Albatross"]s.p.2-=p.3>["Eagle"]s.p.1-=["Birdie"]s.p.=["Par"]s.p.1+=["Bogey"]s.p.2+=["Double Bogey"]s.p.2+=["Double Bogey"]s.p.3+>["Haha you loser"]$

Ungolfed:

? ? s: p:

s. 1 = [
  "Hole in one"
]

~ 1
s. p. 4 - = p. 5 > [
  "Condor"
]

~ 2
s. p. 3 - = p. 4 > [
  "Albatross"
]

~ 3
s. p. 2 - = p. 3 > [
  "Eagle"
]

~ 4
s. p. 1 - = [
  "Birdie"
]

~ 5
s. p. = [
  "Par"
]

~ 6
s. p. 1 + = [
  "Bogey"
]

~ 7
s. p. 2 + = [
  "Double Bogey"
]

~ 8
s. p. 2 + = [
  "Double Bogey"
]

s. p. 3 + > [
  "Haha you loser"
]


$

cat

Posted 2016-03-04T16:49:34.453

Reputation: 4 989

2

bash, 150 136 bytes

b=Bogey
(($2<2))&&echo Hole in one||tail -$[$2-$1+5]<<<"Haha you loser
Triple $b
Double $b
$b
Par
Birdie
Eagle
Albatross
Condor"|head -1

Test run:

llama@llama:...code/shell/ppcg74767golfgolf$ for x in {1..11}; do bash golfgolf.sh 6 $x; done                                                          
Hole in one
Condor
Albatross
Eagle
Birdie
Par
Bogey
Double Bogey
Triple Bogey
Haha you loser
Haha you loser

Thanks to Dennis for 14 bytes!

Doorknob

Posted 2016-03-04T16:49:34.453

Reputation: 68 138

1

Python 2, 186 179 158 bytes

def c(s,p):a="Bogey";print["Condor","Albatross","Eagle","Birdie","Par",a,"Double "+a,"Triple "+a,"Haha you loser","Hole in one"][([[s-p+4,8][s-p>3],9][s==1])]

EDIT 1: added the missing "hole in one" case...

EDIT 2: golfed out some bytes (thanks to tac)

Max

Posted 2016-03-04T16:49:34.453

Reputation: 501

1

A lambda would be shorter, also see tips for golfing in Python

– cat – 2016-03-04T22:21:52.213

you can drop the space between 4 and else – cat – 2016-03-04T22:22:30.433

1If you modify the algorithm, you can just index a list rather than a dict – cat – 2016-03-04T22:23:24.730

you can drop the space between print and {, and if you use a semicolon to put the a= and print on the same line, you can shave a byte of whitespace – cat – 2016-03-04T22:24:43.230

Also, Albatross needs two s's – cat – 2016-03-04T22:25:11.247

1@tac actually "c=lambda x,y:" is longer than "def c(x,y):", thanks for the others suggestions – Max – 2016-03-04T22:59:20.303

1

Python 2.7, 226 bytes

p,s=input()
b="Bogey"
l={s==1:"Hole in one",5<p==s+4:"Condor",4<p==s+3:"Albatross",3<p==s+2:"Eagle",s==p-1:"Birdie",s==p:"Par",s==p+1:b,s==p+2:"Double "+b,s==p+3:"Triple "+b,s>p+3:"Haha you loser"}
for r in l:
 if r:print l[r]

Hard to come up with a short python code when you're late to the party, best I could think of.

janrn

Posted 2016-03-04T16:49:34.453

Reputation: 51

The last two lines should be changed to one: [print r for r in l if r] – cat – 2016-03-06T17:04:04.227

1

Haskell - 131 bytes (counting newline)

1%p="Hole in one"
s%p=lines"Condore\nAlbatros\nEagle\nBirdie\nPar\nBogey\nDouble Bogey\nTriple Bogey\nHaha you loser"!!min(4-p+s)8

lines is the only way I can think of to golf in a list of strings that have to contain spaces with access only to Prelude so stuck with two character delimiters.

Still, Haskell isn't usually this competitive (against general languages at least).

Steven Armstrong

Posted 2016-03-04T16:49:34.453

Reputation: 201

You can import anything you like, not just the builtins – cat – 2016-03-06T17:19:42.337

1

C, 198 Bytes

main(){char s=8,p=4,m[]="Hole in one.Condor.Albatross.Eagle.Birdie.Par.Bogey.Double Bogey.Triple Bogey.Haha you loser",*t,*x,i=0;for(x=m;t=strtok(x,".");x=0,i++)if((s-1?s-p>3?9:s-p+5:0)==i)puts(t);}

Johan du Toit

Posted 2016-03-04T16:49:34.453

Reputation: 1 524

0

Japt, 97 bytes

`Ho¤  e
CÆBr
AlßNoss
Eag¤
Bir¹e
P
zD½e zTp¤ zHa y lo r`rz"Bogey
" ·g9m´V©V-U+6

Contains a bunch of unprintables. Test it online!

How it works

`Ho¤  e\nCÆBr\nAlßNoss\nEag¤\nBir¹e\nP\nzD½e zTp¤ zHa y lo r`                        rz"Bogey\n" ·  g9m´ V© V-U+6
"Hole in one\nCondor\nAlbatross\nEagle\nBirdie\nPar\nzDouble zTriple zHaha you loser"rz"Bogey\n" qR g9m--V&&V-U+6

              // Implicit: U = par, V = score
"..."         // Take this long, compressed string.
rz"Bogey\n"   // Replace each instance of "z" with "Bogey\n".
qR            // Split at newlines.

--V&&V-U+6    // If V is 1, take 0; otherwise, take V-U+5.
9m            // Take the smaller of this and 9.
g             // Get the item at this index in the previous list of words.
              // Implicit output

ETHproductions

Posted 2016-03-04T16:49:34.453

Reputation: 47 880

0

Python 2.7.2, 275 bytes

s=int(input())
p=int(input())
a="Bogey"
if s==1:b="Hole in one"
elif p-4==s:b="Condor"
elif p-3==s:b="Albatross"
elif p-2==s:b="Eagle"
elif p-1==s:b="Birdie"
elif p==s:b="Par"
elif p+1==s:b=a
elif p+2==s:b="Double "+a
elif p+3==s:b="Triple "+a
else:b="Haha you loser"
print b

Ungolfed/explained:

score = int(input())
par = int(input)
foo = "Bogey" # a shortcut for 3 of the outputs
if score == 1:
    output = "Hole in one"
elif par - 4 == score:
    output = "Condor"
...
elif par == score:
    output = "Par"
elif par + 1 == score:
    output = foo # See what I mean?
elif par + 2 == score:
    output = "Double " + foo # Huh? Huh?
elif par + 3 == score:
    output = "Triple " + foo # That makes 3.
else:
    output = "Haha you loser"
print output # Make sense?

My second answer, ironically both are in Python. Golfing tips appreciated!

OldBunny2800

Posted 2016-03-04T16:49:34.453

Reputation: 1 379

Hint: you don't even need b. – Leaky Nun – 2016-07-22T17:26:43.310

I'm going to edit, just nrn. – OldBunny2800 – 2016-07-22T17:27:50.787

Have a look at this.

– Leaky Nun – 2016-07-22T17:28:54.483

Also, I thought Python 2 casts your input to int automatically. – Leaky Nun – 2016-07-22T17:29:08.407

-2

Python 2, 302 284 bytes

i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double ","Triple "][k-1]+"Bogey"
 else:
  print"Haha you loser"

If leading white space was allowed, that'd be 282 bytes:

i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double","Triple"][k-1],"Bogey"
 else:
  print"Haha you loser"

user48538

Posted 2016-03-04T16:49:34.453

Reputation: 1 478

12The use of a string array would seriously help you here. – Addison Crump – 2016-03-04T17:39:00.403

Agreed, this seemed really unoptimized. Any combination of terms/results would shorten the answer. – ricdesi – 2016-03-05T06:46:17.813

Why do you need raw_input()? Can't you just use input()? – OldBunny2800 – 2016-03-05T23:38:33.920

@ricdesi I combined the bogeys. – user48538 – 2016-03-06T14:58:32.143