import IO
import Char
main = do fromHandle <- openFile "c:/a.txt" ReadMode
toHandle <- openFile "c:/b.txt" WriteMode
contents <- hGetContents fromHandle
hPutStr toHandle (transform contents)
hClose fromHandle
hClose toHandle
--
-- transform
--
-- reads an string with alle the data and parses it
--
-- returns a string with complete info on every team
--
-- (split string, split every line, remove everything but the names) and use it to map all parseLine's to it
--
transform :: String -> String
transform a = unlines (map (parseLine (lines a)) (unique x [])) where x = map(head.tail) (map (words) (lines a))
--
-- parseLine
--
-- takes Array with all the data and the team it has to generate the info for
--
-- returns a string with complete information of ONE team
--
parseLine :: [String] -> String -> String
parseLine [] q = q
parseLine (d:ata) query
| notElem ' ' query = parseLine (d:ata) ("0 "++query++" 0")
| not( any isDigit (last (words query))) = parseLine ata (show(read(head(words query)) :: Int) ++" "++ head(tail(words query)) ++ " 0")
| elem (head(tail(words query))) (words d) = parseLine ata (show((read (head(words query)) :: Int) + (read (head(words d)) :: Int)) ++" "++head(tail(words query)) ++" "++show((read (last(words query)) :: Int) +1) )
| otherwise = parseLine ata query
--
-- unique
--
-- takes a array with names and a []
--
-- returns an array with names so that every name is a unique one (filters all doubles out)
--
unique :: [String] -> [String] -> [String]
unique [] b = b
unique (x:xs) b
| elem x b = unique xs b
| notElem x b = unique xs (b++[x])