Skip to content

Instantly share code, notes, and snippets.

@dittos
Created May 12, 2013 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dittos/5563284 to your computer and use it in GitHub Desktop.
Save dittos/5563284 to your computer and use it in GitHub Desktop.
type Point = (Float, Float)
data Orbit = Orbit Point Float
data TestCase = TestCase Point Point [Orbit]
-- Input
readFloats :: IO [Float]
readFloats = do
line <- getLine
return (map read (words line))
readInt :: IO Int
readInt = getLine >>= return . read
readOrbit = do
orbit <- readFloats
let cx:cy:r:_ = orbit
return (Orbit (cx, cy) r)
readTestCase = do
points <- readFloats
let x1:y1:x2:y2:_ = points
n <- readInt
orbits <- sequence (replicate n readOrbit)
return (TestCase (x1, y1) (x2, y2) orbits)
-- Process
distance :: Point -> Point -> Float
distance (x1, y1) (x2, y2) = sqrt ((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
contains :: Orbit -> Point -> Bool
contains (Orbit c r) p = (distance c p) <= r
countIf pred xs = length (filter pred xs)
evaluate (TestCase from to orbits) =
countIf (\x -> (contains x from) /= (contains x to)) orbits
-- Output
main = do
n <- readInt
testCases <- sequence (replicate n readTestCase)
print (map evaluate testCases)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment