Lets play golf while we golf.


  • Distance to the hole initially is 700m
  • Each hit to the ball will make it advance to the hole 250-350m, this should be random.
  • Each hit has a 5% probabilities of going into water, this will make the quantity of hits increase by 1 as a penalty.
  • When ball is under 250m it will advance 70% to 90% (random again) of missing distance with a probability of 80%, Advance 90% to 99% with 14% Probability, 1% of doing 10%(and finishing) and 5% of going to water and increasing the number of hits by 1 as a penalty.
  • When ball is under 10m it has 95% of going into hole (finishing) and 5% of advancing 75% of the missing path. No probability of going water.


-Imagine ball is 270m from hole, if we hit for 300m, the distance to the hole is now 30, this means, distance will be absolute value.

-Numbers will always be integer due to deal the probability of hitting for example 300m when ball is 300m away from the hole.

-Round down number of m of the hit, imagine you are at 1m , if you fall on 5% of not going into the hole, it will advance 0.




Distance to the hole in each hit /n

Total number of hits

Example output (don't print comments)

433m //Hit for 267m (700-267=433)

130m //Hit for 303m (433-303=130)

130m //Ball on water +1 penalty hit

35m //Hit for 95m (130-95=35)

7m //Hit for 28m (35-28=7

0m //Ball on hole

Total hits 7 //6 hits +1 penalty

This is codegolf!

  • \$\begingroup\$ Can you add a full work through example? If the ball goes in the water is the distance where it was when it was hit or where it should be if it wasn't in the water? \$\endgroup\$ Jun 2, 2017 at 11:33
  • \$\begingroup\$ @TheLethalCoder if it goes to water the distance doesn't change, repeat the last hit \$\endgroup\$ Jun 2, 2017 at 11:44
  • \$\begingroup\$ @NeilSlater the problem is that you don't know how many hits there will be so how many inputs? \$\endgroup\$ Jun 2, 2017 at 11:46
  • \$\begingroup\$ Hey! Welcome to PPCG! In my opinion, this is a good first challenge! Good luck for future! :) \$\endgroup\$
    – Arjun
    Jun 2, 2017 at 11:46
  • \$\begingroup\$ When the ball is under 10m, is there still a 5% chance the ball goes in the water? \$\endgroup\$ Jun 2, 2017 at 12:02

3 Answers 3


Python 3.6, 250 bytes

Saved 4 bytes thanks to isaacg, and 1 thanks to KoishoreRoy!

d=700                    # Current distance
from random import*
r=randrange              # Function that gets a random value in the range [0, input)
i=0                      # Number of strokes
while d:
 i+=1;x=r(20)<1          # x is False 95% of the time
                         # My justification for reusing this random value
                         # is that it's used once and only once, separate by if/elif
 if d<10:d-=[d,d*.75][x] # We're within putting range; goes in if x is true; otherwise makes 75% progress
 elif x:i+=1             # Goes in the water, add a stroke
 elif d<250:
                         # Only 95 because we already checked to see if it would go in the water
                         # 99% of the time (s>0), it doesn't go in
                         # 14% of the time (s<15), it makes 90-99% progress
                         # Otherwise, it makes 70-90% progress
 else:d-=250+r(101)      # Lose 250-350 yards
print(f'Total hits {i}')

Try it online! (Uses Python 3.5 printing syntax at a cost of 6 bytes since TIO does not yet support Python 3.6.)

  • \$\begingroup\$ You are missing the number of hits (remember the ball can go water and add a hit) \$\endgroup\$ Jun 2, 2017 at 12:24
  • \$\begingroup\$ Updated and golfed slightly further! \$\endgroup\$ Jun 2, 2017 at 12:41
  • 1
    \$\begingroup\$ @KoishoreRoy You're right, it doesn't. I can't seem to figure out why. It definitely worked before I started using semicolons, but taking them out doesn't seem to help. Any thoughts? \$\endgroup\$ Jun 3, 2017 at 0:10
  • 1
    \$\begingroup\$ it's flagging an error in the last end. I tried replacing all the spaces with tabs and that didn't help me figure out anything either. Also, unrelated to the running of the code, you can save a few bytes by using from random import randrange as r \$\endgroup\$ Jun 3, 2017 at 0:12
  • 1
    \$\begingroup\$ My bad. What I said earlier actually increases 1 byte! Use from random import*;r=randrange instead. That reduces 1 byte. \$\endgroup\$ Jun 3, 2017 at 0:20

Perl 6, 212 bytes

my&p=(^*).pick;say 'Total hits ',(700,->\d{my \n=d>249??abs d-(p(20)??250+p
100!!0)!!d>9??d-(|((d*(70+p 21)div 100) xx 80),|((d*(90+p 10)div 100) xx
14),d,|(0 xx 5))[p 100]!!p(20)??d div 4!!0;"{n}m".say;n}...0)-1

&p is a helper function that picks a random number from 0 to one less than its argument. The expression after 'Total hits ' is a lazily-constructed list that generates each element based on the previous element. The elements are printed as they are generated, which isn't very functional, but it is shorter than storing them in an intermediate array.


JavaScript, 204 198 bytes

P('Total hits '+h)

Less golfed:

            ? r(20)<1
                ? h++*0
                : 250+r(100)
        : d * (d<10
            ? x<5
                ? .75
                : 1
        : x<5
            ? h++*0
        : x<6
            ? 1
        : .9 + r(
                ? .09
                : -.2

console.log('Total hits '+h)
  • \$\begingroup\$ Running your code, I got this output once. I don't think the distance can increase once you get within 250 meters, can you explain how your code meets the criteria of the problem? \$\endgroup\$ Jun 3, 2017 at 0:12
  • 1
    \$\begingroup\$ @musicman523 It appears I added the percentage value to the distance rather than the percentage of the distance. I have now fixed it. \$\endgroup\$
    – darrylyeo
    Jun 3, 2017 at 0:34

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.