Last active
August 29, 2015 14:10
-
-
Save jun784/4ce57228042638479d93 to your computer and use it in GitHub Desktop.
CG : haskellの練習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.IO | |
import Control.Monad | |
import Data.List | |
import Data.Data | |
import Data.Function | |
import Data.Ord | |
data Enemy = Enemy{name:: String, dist:: Int} | |
-- [String]とStringの違い... | |
createEnemy :: String -> Enemy | |
-- 引数の渡し方はこれでいい? | |
createEnemy x = Enemy enemy dist | |
where | |
-- hPutStrLn stderr (show x) | |
input = words x | |
enemy = input!!0 -- The name of this enemy | |
dist = read (input!!1) :: Int -- The distance to your cannon of this enemy | |
-- 型の返し方はこれでいい? | |
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 | |
-- 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 () | |
-- hPutStrLn stderr "Debug messages..." | |
-- (w:ws) <- replicateM count getLine | |
-- hPutStrLn stderr (show w) | |
-- hPutStrLn stderr (show count) | |
lines <- replicateM count getLine | |
let enemies = map createEnemy lines | |
-- sortBy (\x y -> compare (dist x) (dist y)) enemies | |
-- hPutStrLn stderr (show enemies) | |
-- The name of the most threatening enemy (HotDroid is just one example) | |
putStrLn $ name $ head $ sortBy (comparing dist) enemies | |
loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment