Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 76 lines (60 sloc) 1.906 kb
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
1 {-# LANGUAGE CPP #-}
2 {-# OPTIONS_GHC -O #-}
3
d4f9290 Krzysztof Skrzętnicki initial commit
authored
4 module Main where
5
6 import qualified Data.ByteString.Lazy as BSL
7 import Data.List ( sortBy )
8 import Data.Ord ( comparing )
9 import Data.Char
10 import Data.Ratio
11
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
12 #ifdef DEBUG
13 import Debug.Trace
14 import System.IO
15 #endif
16
d4f9290 Krzysztof Skrzętnicki initial commit
authored
17 transUnit :: String -> Integer
18 transUnit "G" = 1024 * transUnit "M"
19 transUnit "M" = 1024 * transUnit "K"
20 transUnit "K" = 1024
21
22 transUnit "g" = 1000 * transUnit "m"
23 transUnit "m" = 1000 * transUnit "k"
24 transUnit "k" = 1000
25
26 transUnit "" = 1
27
28 firstColumnSize :: BSL.ByteString -> Integer
29 firstColumnSize line = let
30 (pre,_) = BSL.break (isSpace . e2e) line
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
31 (num,unit) = BSL.break (isAlpha . e2e) pre
32 (preComm,postComm) = BSL.break ((`elem` ".,") . e2e) num
d4f9290 Krzysztof Skrzętnicki initial commit
authored
33
34 toString = map e2e . BSL.unpack
35 readSafe x = (fst . last) $ (0,"") : reads x
36
37 a,b,c :: Ratio Integer
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
38 a = readSafe (toString preComm) % 1
39 b = let l = fromIntegral $ 10 * BSL.length postComm in
d4f9290 Krzysztof Skrzętnicki initial commit
authored
40 case l of
41 0 -> 0
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
42 _ -> readSafe (toString postComm) % l
d4f9290 Krzysztof Skrzętnicki initial commit
authored
43 c = (transUnit (toString unit)) % 1
44 result = round ((a+b) * c)
45 in
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
46 #ifdef DEBUG
47 traceShow (map round [a,b,c], map toString [line,pre,num,preComm,postComm,unit]) result
48 #else
d4f9290 Krzysztof Skrzętnicki initial commit
authored
49 result
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
50 #endif
d4f9290 Krzysztof Skrzętnicki initial commit
authored
51
52
53
54 e2e :: (Enum a, Enum b) => a -> b
55 e2e = toEnum . fromEnum
56
57 main = do
8cd0b6c Added 'debug' flag to .cabal file and debug mode to executable. Fix bug ...
Krzysztof Skrzętnicki authored
58 #ifdef DEBUG
59 hPutStrLn stderr "DEBUG MODE!"
60 #endif
d4f9290 Krzysztof Skrzętnicki initial commit
authored
61 input <- BSL.getContents
62 mapM_ BSL.putStrLn (sortBy (comparing firstColumnSize) $ BSL.split (e2e '\n') input)
63
64 {- TEST:
65
66 1
67 2
68 3
69 1,1k
70 2,1m
71 3,1g
72 1,1K
73 2,1M
74 3,1G
75
76 -}
Something went wrong with that request. Please try again.