# Code Golf Golf Score

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!

• I always wondered what was after triple bogey. Mar 4, 2016 at 19:27
• Incidentally the largest par is 7 not 6. Mar 4, 2016 at 20:57
• @Joshua I was temporarily confused about why you commented instead of edited your own post. Then it hit me. :P Mar 4, 2016 at 21:25
• @RikerW the two Josh's names are as close as their reputation :D
– cat
Mar 4, 2016 at 21:55
• Can the input be in any order? Mar 5, 2016 at 1:26

## PHP 5.3+, 173167166159156151127 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!

• Can you turn it into one string and split by an arbitrary character? Mar 4, 2016 at 17:17
• @CoolestVeto As a matter of fact, I can. Weirdly, it saves fewer bytes than expected, but any bytes are better than no bytes! Mar 4, 2016 at 17:20
• You 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";} Mar 4, 2016 at 19:09 • Had no idea you could cheat string literals, neat. Mar 4, 2016 at 19:15 • Here's a (possibly) working and shorter one: printf([Condor,Albatross,Eagle,Birdie,Par,'%s','Double %s','Triple %s','Haha you loser'][min(4+$argv-$argv,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. Mar 4, 2016 at 19:39

# 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!

• ...Whoa. Nicely done.
– cat
Mar 4, 2016 at 20:49
• 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! Mar 9, 2016 at 6:49
• @DerpfacePython Haha, the random unicode messages are partially part of the code and the other part is part of a compressed message. Mar 9, 2016 at 19:13

# 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”.

• Congrats on the shortest answer!
– Josh
Mar 15, 2016 at 2:52

# 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;
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;
• "Push 9. This forces any items greater than 8 to be 9." Wat?
– cat
Mar 4, 2016 at 20:33
• @tac This is to force the input that would result in "Haha you loser" to go to the line containing "Haha you loser". Mar 4, 2016 at 20:42

## 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 Zor Z[I-P+6]or Z)


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

• This is non-competitive because the file was uploaded to Dropbox 2 hours after the challenge was posted.
– user45941
Mar 4, 2016 at 21:58
• github.com is much better at code hosting...
– cat
Mar 4, 2016 at 22:28
• Absolutely, I just havent had a chance to set it up. Mar 4, 2016 at 22:29

# 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.

• An example of combining the bogeys is: ",Double ,Triple ".split,[k-1]+"Bogey" where k=s-p. Mar 4, 2016 at 19:03
• Can I use your approach in my solution? Mar 4, 2016 at 19:07
• @zyabin101 that isn't discouraged, as long as it isn't outright plagiarism
– cat
Mar 4, 2016 at 21:32
• @zyabin101 Thanks, I'll see if that makes it any shorter. And yes, feel free to use this approach in your answer. Mar 5, 2016 at 15:55
• I'm already using this. Mar 5, 2016 at 16:16

# Mouse-2002, 223 207 bytes

??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" ]$


## 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!

# Python 2, 186179 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)

• A lambda would be shorter, also see tips for golfing in Python
– cat
Mar 4, 2016 at 22:21
• you can drop the space between 4 and else
– cat
Mar 4, 2016 at 22:22
• If you modify the algorithm, you can just index a list rather than a dict
– cat
Mar 4, 2016 at 22:23
• 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
Mar 4, 2016 at 22:24
• @tac actually "c=lambda x,y:" is longer than "def c(x,y):", thanks for the others suggestions
– Max
Mar 4, 2016 at 22:59

# 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).

• You can import anything you like, not just the builtins
– cat
Mar 6, 2016 at 17:19

# 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.

• The last two lines should be changed to one: [print r for r in l if r]
– cat
Mar 6, 2016 at 17:04

# 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);}


# Japt, 97 bytes

Ho¤  e
CÆBr
AlßNoss
Eag¤
Bir¹e
P
zD½e zTp¤ zHa y lo rrz"Bogey


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


## 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!

• Hint: you don't even need b. Jul 22, 2016 at 17:26
• I'm going to edit, just nrn. Jul 22, 2016 at 17:27
• Have a look at this. Jul 22, 2016 at 17:28
• Also, I thought Python 2 casts your input to int automatically. Jul 22, 2016 at 17:29

# 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"

• The use of a string array would seriously help you here. Mar 4, 2016 at 17:39
• Agreed, this seemed really unoptimized. Any combination of terms/results would shorten the answer. Mar 5, 2016 at 6:46
• Why do you need raw_input()? Can't you just use input()? Mar 5, 2016 at 23:38
• @ricdesi I combined the bogeys. Mar 6, 2016 at 14:58