-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.hs
44 lines (37 loc) · 1.12 KB
/
day8.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Control.Parallel
import Text.Parsec
import Text.Parsec.String (Parser)
import Text.Parsec.Char
import Text.Parsec.Token (hexadecimal)
import Data.Char (chr)
import Data.Maybe (fromJust)
import Data.Hex (unhex)
escape :: String -> Char -> Parser (Char)
escape s c = const c <$> string s
hex :: Parser (Char)
hex = do
char '\\'
char 'x'
d1 <- oneOf $ ['0'..'9'] ++ ['a'..'z'] ++ ['A'..'Z']
d2 <- oneOf $ ['0'..'9'] ++ ['a'..'z'] ++ ['A'..'Z']
return (head . fromJust . unhex $ [d1,d2])
lineParser :: Parser (String)
lineParser = many $ do
c <- choice . map try $ [escape "\\\"" '"', escape "\\\\" '\\', hex, anyChar]
return c
parser :: Parser (String)
parser = do
char '"'
line <- lineParser
char '"'
return line
getParseValue :: String -> String -> String
getParseValue name val = either (error . show) (id) $ parse parser name val
getEscd :: String -> String
getEscd = getParseValue "day8"
main :: IO ()
main = do
contents <- getLine >>= readFile
let escd = sum . map (length . getEscd) . lines $ contents
let norm = sum . map length . lines $ contents
print $ (escd `par` norm) `pseq` (norm - escd)