Golfception arrives

8

2

Lets play golf while we golf.

Problem:

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

Clarifications:

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

Input:

Nothing

Output:

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!

Java Gonzar

Posted 2017-06-02T11:30:36.147

Reputation: 173

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? – TheLethalCoder – 2017-06-02T11:33:37.070

@TheLethalCoder if it goes to water the distance doesn't change, repeat the last hit – Java Gonzar – 2017-06-02T11:44:38.000

@NeilSlater the problem is that you don't know how many hits there will be so how many inputs? – Java Gonzar – 2017-06-02T11:46:08.603

Hey! Welcome to PPCG! In my opinion, this is a good first challenge! Good luck for future! :) – Arjun – 2017-06-02T11:46:36.983

When the ball is under 10m, is there still a 5% chance the ball goes in the water? – musicman523 – 2017-06-02T12:02:34.603

Should the distance always be an integer? – musicman523 – 2017-06-02T12:10:02.643

@musicman523 Edited now to fill those questions :) – Java Gonzar – 2017-06-02T12:13:21.077

If distances are always integers, should resulting distance be rounded away from the hole (so there is only the 1% chance of actually hitting the hole? Or could a 90% shot at 2m distance also hit the hole? (I'm guessing the 10%(and finishing) should be 100%. – lrn – 2017-06-02T12:13:31.820

@Irn 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. – Java Gonzar – 2017-06-02T12:16:36.323

The "random" 250-350 hit, should this be uniformly random? – lrn – 2017-06-02T12:16:48.173

@Irn, yes, if the random number generated from 0-100 is 55, the hit will be 305 – Java Gonzar – 2017-06-02T12:18:30.177

As submissions have gone ahead as-is, I have removed my comments. There is no absolute need to alter your challenge IMO. I think there is a pre-challenge review process you can use to discuss design issues in future before inviting competitors. – Neil Slater – 2017-06-02T12:25:53.793

@NeilSlater Thank you Neil :) – Java Gonzar – 2017-06-02T12:27:49.043

@NeilSlater That's the Sandbox.

– wizzwizz4 – 2017-06-02T12:29:57.570

I tried, but there are far too many percentage-based conditionals and RNG for this to really work in Braingolf. – Skidsdev – 2017-06-02T16:02:14.923

Do we need to print the 'm' in the distances? Should we print 700m at the beginning? – musicman523 – 2017-06-02T18:42:19.887

Yes, m should be printed although it is not necessary to print the first distance (700m) – Java Gonzar – 2017-06-02T20:10:28.940

Answers

1

JavaScript, 204 198 bytes

P=console.log
r=x=>Math.random()*x
for(d=700,h=0;d;)x=r(100),D=d>=250?r(20)<1?h++*0:250+r(100):d*(d<10?x<5?.75:1:x<5?h++*0:x<6?1:.9+r(x<20?.09:-.2)),d=Math.abs(d-D|0),h++,P(d+'m')
P('Total hits '+h)

Less golfed:

r=x=>Math.random()*x
for(d=700,h=0;d;){
    x=r(100),
    D=
        d>=250
            ? r(20)<1
                ? h++*0
                : 250+r(100)
        : d * (d<10
            ? x<5
                ? .75
                : 1
        : x<5
            ? h++*0
        : x<6
            ? 1
        : .9 + r(
            x<20
                ? .09
                : -.2
            )
        ),
    d=Math.abs(d-D|0),
    h++,
    console.log(d+'m')
}

console.log('Total hits '+h)

darrylyeo

Posted 2017-06-02T11:30:36.147

Reputation: 6 214

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?

– musicman523 – 2017-06-03T00:12:23.087

1@musicman523 It appears I added the percentage value to the distance rather than the percentage of the distance. I have now fixed it. – darrylyeo – 2017-06-03T00:34:08.947

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:
  s=r(95);d-=[d,d*[.7+r(21)/100,.9*r(10)/100][s<15]][s>0]
                         # 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
 d=int(abs(d));print(f'{d}m')
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.)

musicman523

Posted 2017-06-02T11:30:36.147

Reputation: 4 472

You are missing the number of hits (remember the ball can go water and add a hit) – Java Gonzar – 2017-06-02T12:24:37.297

Updated and golfed slightly further! – musicman523 – 2017-06-02T12:41:08.537

Where do you add the penalty hit for going to water? – Java Gonzar – 2017-06-02T12:42:58.547

Fixed a couple issues and golfed slightly further. The penalty for going in the water happens with elif r(0,19)<1:0; this cases the current iteration to do nothing 5% of the time. – musicman523 – 2017-06-02T13:00:44.227

It has to do something, adding 1 more hit as penalty – Java Gonzar – 2017-06-02T16:32:08.497

It does - every iteration prints the current distance and increments the number of hits. That line makes it so d doesn't change in the current iteration. – musicman523 – 2017-06-02T17:33:57.723

I mean, apart from the missed hit, there is another hit adittion for loosing the ball – Java Gonzar – 2017-06-02T18:38:40.037

Oh, gotcha! Fixed it, costing just 3 bytes. I do think this can still be golfed further, though. – musicman523 – 2017-06-02T18:41:00.583

You can use semicolons to combine indented lines without statements into one line, which shortens it. For instance, i+=1;x=r(20)<1 works. – isaacg – 2017-06-02T21:30:49.197

is it just me or the two code doesn't work? – Koishore Roy – 2017-06-02T23:55:12.033

1@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? – musicman523 – 2017-06-03T00:10:17.390

1it'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 – Koishore Roy – 2017-06-03T00:12:54.527

I totally forgot you can do that in Python. Thanks! As for the syntax error...I'll try and figure it out. – musicman523 – 2017-06-03T00:14:24.177

1My bad. What I said earlier actually increases 1 byte!

Use from random import*;r=randrange instead. That reduces 1 byte. – Koishore Roy – 2017-06-03T00:20:56.027

Found the error - stray brace. Also thanks for the byte! I like being at an even 250 ☺ – musicman523 – 2017-06-03T00:24:50.203

2

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.

Sean

Posted 2017-06-02T11:30:36.147

Reputation: 4 136