Skip to content

Commit

Permalink
005A
Browse files Browse the repository at this point in the history
  • Loading branch information
mkut committed Jul 1, 2012
1 parent 2a9eee8 commit 6244f34
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 005/A/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main
in.txt
out.txt
ans.txt
34 changes: 34 additions & 0 deletions 005/A/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
################################
## Makefile for Haskell (AtCoder version)
## Author: mkut
## Date : 2012-5-17
## Rev : 0.1.1
################################

## project settings
TARGET = main
SOURCES = haskell.hs
MILIBS = IO Contest

## command settings
HC = ghc
HCFLAGS = --make -O

## directory settings
MILIB_DIR = /home/mkut/milib/haskell

## file name macros
OBJS = $(SOURCES:%.hs=%.hi) $(SOURCES:%.hs=%.o)
MILIB_SRC = $(MILIBS:%=$(MILIB_DIR)/%.hs)

## make rules
.PHONY: default clean

default: $(TARGET)

$(TARGET): $(SOURCES) $(MILIB_SRC)
$(HC) $(HCFLAGS) -o $@ $^
rm $(OBJS)

clean:
rm -f *.o $(TARGET) $(OBJS) $(GEN_OBJS)
66 changes: 66 additions & 0 deletions 005/A/haskell.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types #-}
import qualified Data.ByteString.Lazy.Char8 as C
import System.IO
import Text.Parsec.Prim
import Text.Parsec.Combinator
import Text.Parsec.Char
import Text.Printf
import Data.Functor.Identity
import Control.Monad

main = contestMain printer solver parser

solver = length . filter f
where
f "TAKAHASHIKUN" = True
f "Takahashikun" = True
f "takahashikun" = True
f _ = False

printer = hPrint

parser = do
n <- number
ret <- count n word
char '.'
return ret

-- Milib.Contest
type Printer a = Handle -> a -> IO ()
type Solver a b = a -> b
type Parser b = Stream C.ByteString Identity Char => Parsec C.ByteString () b
type CMain a b = Printer b -> Solver a b -> Parser a -> IO ()
type HCMain a b = Handle -> Handle -> CMain a b

instance Stream C.ByteString Identity Char where
uncons = return . C.uncons

hContestMain :: HCMain a b
hContestMain hin hout printer solver parser = do
input <- C.hGetContents hin
case parse parser "" input of
Left err -> do { hPutStr stderr "parse err: "; hPrint stderr err }
Right x -> printer hout $ solver x

contestMain :: CMain a b
contestMain = hContestMain stdin stdout

-- Milib.IO
number' :: (Stream s m Char, Integral a, Read a) => ParsecT s u m a
number' =
do ds <- many1 digit
return (read ds)
<?> "number"

number :: (Stream s m Char, Integral a, Read a) => ParsecT s u m a
number = do spaces; number'

word' :: Stream s m Char => ParsecT s u m String
word' = many1 letter

word :: Stream s m Char => ParsecT s u m String
word = do spaces; word'

-- vim: set expandtab:

0 comments on commit 6244f34

Please sign in to comment.