Skip to content

Instantly share code, notes, and snippets.

@lepinay
Last active August 29, 2015 14:12
Show Gist options
  • Save lepinay/5c68501f49d70b06fd19 to your computer and use it in GitHub Desktop.
Save lepinay/5c68501f49d70b06fd19 to your computer and use it in GitHub Desktop.
test haskell
import System.IO
import Control.Monad
import Data.List
main :: IO ()
main = do
hSetBuffering stdout NoBuffering -- DO NOT REMOVE
-- The code below will read all the game information for you.
-- On each game turn, information will be available on the standard input, you will be sent:
-- -> the total number of visible enemies
-- -> for each enemy, its name and distance from you
-- The system will wait for you to write an enemy name on the standard output.
-- Once you have designated a target:
-- -> the cannon will shoot
-- -> the enemies will move
-- -> new info will be available for you to read on the standard input.
loop
loop :: IO ()
loop = do
input_line <- getLine
let count = read input_line :: Int -- The number of current enemy ships within range
let ennemies = replicateM count $ do
input_line <- getLine
let input = words input_line
let enemy = input!!0 -- The name of this enemy
let dist = read (input!!1) :: Int -- The distance to your cannon of this enemy
return (enemy,dist)
do
en <- ennemies
let (name,_) = head $ sortBy (\ (_,a) (_,b) -> compare a b ) en
putStrLn name
loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment