21
\$\begingroup\$

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!

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

15 Answers 15

12
\$\begingroup\$

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!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Can you turn it into one string and split by an arbitrary character? \$\endgroup\$ Mar 4, 2016 at 17:17
  • \$\begingroup\$ @CoolestVeto As a matter of fact, I can. Weirdly, it saves fewer bytes than expected, but any bytes are better than no bytes! \$\endgroup\$
    – ricdesi
    Mar 4, 2016 at 17:20
  • 1
    \$\begingroup\$ 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";} \$\endgroup\$ Mar 4, 2016 at 19:09
  • \$\begingroup\$ Had no idea you could cheat string literals, neat. \$\endgroup\$
    – ricdesi
    Mar 4, 2016 at 19:15
  • 1
    \$\begingroup\$ 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[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. \$\endgroup\$ Mar 4, 2016 at 19:39
10
\$\begingroup\$

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!

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

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”.
\$\endgroup\$
1
  • \$\begingroup\$ Congrats on the shortest answer! \$\endgroup\$
    – Josh
    Mar 15, 2016 at 2:52
6
\$\begingroup\$

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

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

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

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.

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

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


$
\$\endgroup\$
2
\$\begingroup\$

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!

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

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)

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

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

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

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.

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

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);}
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ Hint: you don't even need b. \$\endgroup\$
    – Leaky Nun
    Jul 22, 2016 at 17:26
  • \$\begingroup\$ I'm going to edit, just nrn. \$\endgroup\$
    – AAM111
    Jul 22, 2016 at 17:27
  • \$\begingroup\$ Have a look at this. \$\endgroup\$
    – Leaky Nun
    Jul 22, 2016 at 17:28
  • \$\begingroup\$ Also, I thought Python 2 casts your input to int automatically. \$\endgroup\$
    – Leaky Nun
    Jul 22, 2016 at 17:29
-1
\$\begingroup\$

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"
\$\endgroup\$
4
  • 12
    \$\begingroup\$ The use of a string array would seriously help you here. \$\endgroup\$ Mar 4, 2016 at 17:39
  • \$\begingroup\$ Agreed, this seemed really unoptimized. Any combination of terms/results would shorten the answer. \$\endgroup\$
    – ricdesi
    Mar 5, 2016 at 6:46
  • \$\begingroup\$ Why do you need raw_input()? Can't you just use input()? \$\endgroup\$
    – AAM111
    Mar 5, 2016 at 23:38
  • \$\begingroup\$ @ricdesi I combined the bogeys. \$\endgroup\$
    – user48538
    Mar 6, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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