summaryrefslogtreecommitdiff
path: root/2015/day08.hs
blob: d48212bcf2e2cffb01e73a5351af1e67153b600f (plain)
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
module Main where

import Data.Char
import Control.Monad

parseHexSingle :: Char -> Int
parseHexSingle x  -- = -- subl syntax
    | ord x >= ord '0' && ord x <= ord '9' = ord x - ord '0'
    | ord x >= ord 'a' && ord x <= ord 'f' = 10 + ord x - ord 'a'
    | ord x >= ord 'A' && ord x <= ord 'F' = 10 + ord x - ord 'A'
    | otherwise = undefined

parseHex :: String -> Int
parseHex "" = 0
parseHex (x:xs) = 16 * parseHexSingle x + parseHex xs

parse :: String -> String
parse s  -- = -- subl syntax
    | length s > 1 && head s == '"' && last s == '"' = parse' $ init $ tail s
    | otherwise = undefined

parse' :: String -> String
parse' "" = ""
parse' ('\\':'\\':xs) = '\\' : parse' xs
parse' ('\\':'"':xs) = '"' : parse' xs
parse' ('\\':'x':a:b:xs) = chr (parseHex $ [a,b]) : parse' xs
parse' (x:xs) = x : parse' xs

day8 :: IO ()
day8 = do
    input <- liftM lines $ readFile "day08.txt"
    print $ sum [length s - length (parse s) | s <- input]

day8_2 :: IO ()
day8_2 = do
    input <- liftM lines $ readFile "day08.txt"
    print $ sum [length (show s) - length s | s <- input]

main = day8