Input 5 integers: Landmark X & Y, Starting Point X & Y, and View Distance Radius D

Output 1 double: % chance of coming within D of Landmark (X,Y) ("seeing") when walking straight in a random direction from Starting Point (X,Y).

Assume that "random direction" shall be a direction uniform on [0,2π], the land is flat, and of course the 'hiker' never turns after he begins moving.

Example: If landmark is at (10,10), starting point is (5,5), and view distance is 20 then the output would be 100% as no matter which direction you walk the landmark will be immediately visible.

  • 1
    \$\begingroup\$ This kinda lost its appeal since you already linked to the solution. \$\endgroup\$ – Martin Ender Jul 24 '14 at 18:23
  • \$\begingroup\$ @MartinBüttner I hadn't considered that since it is hardly implemented as code... but I suppose I can remove it if people also want to solve the math. \$\endgroup\$ – CloudMeta Jul 24 '14 at 19:08
  • \$\begingroup\$ @ioSamurai just wanted to let you know that people can still see the math if they choose to look at the revisions - although I don't see any problem with leaving the math there.. people would be able to copy the math from the first answer, and would be challenged with optimizing the space anyways... \$\endgroup\$ – user2813274 Aug 6 '14 at 15:18
  • \$\begingroup\$ @user2813274 Yeah I didn't think showing the math was an issue anyways because it was really about the coding. \$\endgroup\$ – CloudMeta Aug 6 '14 at 15:24
  • 2
    \$\begingroup\$ If you want to make this challenging/interesting, i suggest you ban trigonometric functions. Accuracy of probability should also be specified (I suggest to the nearest 1%, given that probability can be from 0 to 100%.) \$\endgroup\$ – Level River St Aug 6 '14 at 15:39

CJam -41 -40 -39 -38/35 31/26

This seems to work. It is my first attempt at CJam and/or codegolf. Run the code at http://cjam.aditsu.net/. In the section called input just place the variables as integers delimited with spaces in the input block in this order: distance D, landmark x, starting x, landmark y, starting y (for example 20 10 5 10 5). I had posted a previous one based on a misunderstanding of the equation that has been resolved. I also had been returning answers as probability ratios rather than percentages. Note: the second code has only 31 characters but combines alot from another user's CJam code.

ri{riri- 2#}:U;UU+.5#/mSP/:A.5<A1?e2


Accuratish one without arcsine being directly called (44 characters):

ririri- 2#riri- 2#+ .5#/:A1<AA3#6/+P/1?100*

Even more accurate (52 characters):

ririri- 2#riri- 2#+ .5#/:A1<AA3#6/+A5#40/3*+P/1?100


The absolute best I've written is 26 characters. I've still learned alot by watching professorfish's attempts but the crux is mine. I assumes (potentially incorrectly) that if you can see the landmark if it is closer (not closer than or equal to) your sight radius.


Haskell, 65 63

import Data.Complex

Use like (xstart:+ystart, xlandmark:+ylandmark) % distance. Gives result in percent. Why is it so expensive to load modules in Haskell?!?

Note that there is no if/then/else, pattern matching, etc. in this code, min does the magic.

  • 2
    \$\begingroup\$ I didn't expect Haskell to do so well here, nor the Spanish Inquisition. :D \$\endgroup\$ – cjfaure Aug 7 '14 at 20:12
  • \$\begingroup\$ what is the undocumented feature, if i may ask? \$\endgroup\$ – proud haskeller Aug 7 '14 at 20:56
  • \$\begingroup\$ is it the fixity of `min`? \$\endgroup\$ – proud haskeller Aug 7 '14 at 21:04
  • \$\begingroup\$ @proud haskeller: the way min deals with NaN. Since NaN compares to a non-NaN to False, the exact implementation of min is relevant (whether or not it uses the inverse of a result of a comparison). As far as I can tell, this behaviour is not specified anywhere, but I'd be glad to be proven wrong. \$\endgroup\$ – TheSpanishInquisition Aug 7 '14 at 21:05
  • \$\begingroup\$ when you write NaN, you mean undefined? or is there some other NaN I'm not aware about? \$\endgroup\$ – proud haskeller Aug 7 '14 at 21:13

CJam, 44 40 38 37

First CJam script! Uses the method on the Math.SE answer here.

Supports non-integer inputs as well, at no extra character cost.


Interpreter at cjam.aditsu.net.

Order of inputs

The inputs are given in this order, on STDIN, separated by spaces:

  1. View Distance Radius
  2. Landmark X
  3. Starting Point X
  4. Landmark Y
  5. Starting Point Y
  • \$\begingroup\$ Why not use P instead of 3.14? P starts off defined as pi This will give you the same length as mine. \$\endgroup\$ – kaine Aug 6 '14 at 19:24
  • 1
    \$\begingroup\$ @kaine Fortunately I've thought of another saving, I'm now beating you by one character \$\endgroup\$ – user16402 Aug 6 '14 at 19:26
  • 1
    \$\begingroup\$ How is that fortunate? \$\endgroup\$ – kaine Aug 6 '14 at 19:26
  • 1
    \$\begingroup\$ There is, what I believe, to be a mistake in this. If you start exactly as far as you can see away, you get 50% on yours 100% on mine. Easy change. An it is debatable which is correct. \$\endgroup\$ – kaine Aug 6 '14 at 19:33
  • \$\begingroup\$ ri{riri-_}:U;UU+mq/_1>{;1}{mSP/}?100 you are welcome dammit \$\endgroup\$ – kaine Aug 6 '14 at 20:05

Haskell — 72 70 69 68

I think this satisfies the question, but if not please leave a comment and I'll do my best to fix it:

w s y l z d|r<d=100|0<1=asin(d/r)/pi*100where r=sqrt$(l-s)^2+(z-y)^2
  • \$\begingroup\$ you can replace True with 0<1, and use $ in the definition of r. \$\endgroup\$ – proud haskeller Aug 6 '14 at 19:53
  • \$\begingroup\$ Thanks, I can't believe I missed that $ opportunity. \$\endgroup\$ – DrJPepper Aug 6 '14 at 20:17
  • \$\begingroup\$ You could also squish where up against 100. \$\endgroup\$ – comperendinous Aug 6 '14 at 20:48
  • \$\begingroup\$ It breaks vim's syntax highlighting (why I didn't delete the space in the first place), but it does indeed compile. \$\endgroup\$ – DrJPepper Aug 6 '14 at 20:54
  • 1
    \$\begingroup\$ Also, asinh should really be asin. The character reduction is a bonus! \$\endgroup\$ – comperendinous Aug 6 '14 at 21:33

Python - 73

from math import*
f=lambda l,s,d:(abs(l-s)<=d or asin(d/abs(l-s))/pi)*100

l is a complex number (e.g. 5 + 5j) describing the landmark position, s describes the start position and d is the view distance, for the example from the question call f as follows: f(5 + 5j, 10 + 10j, 20)

  • \$\begingroup\$ I guess you are missing a bracket (. \$\endgroup\$ – Falko Aug 9 '14 at 13:49
  • \$\begingroup\$ Now you are missing two brackets (...). ;) Otherwise you might return True rather than 100 in case the landmark is closer than the distance d. \$\endgroup\$ – Falko Aug 9 '14 at 20:29
  • \$\begingroup\$ Oh, that's what you mean... \$\endgroup\$ – hlt Aug 9 '14 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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