]> localhost Git - adventofcode2018.git/blame_incremental - day04/guards.hs
Begin Day 4, implement parser
[adventofcode2018.git] / day04 / guards.hs
... / ...
CommitLineData
1import qualified Data.Map.Strict as M
2import Text.ParserCombinators.ReadP
3import Control.Applicative ((<|>))
4
5main :: IO ()
6main = do
7 shifts <- map parse . lines <$> getContents
8 print "a"
9
10type GID = Int
11
12data Status = Begin | Awake | Asleep deriving (Show)
13data Date = Date Int Int Int deriving (Show)
14data Time = Time Int Int deriving (Show)
15
16data Shift = Shift Status GID Date Time deriving (Show)
17
18parse :: String -> Shift
19parse = fst . head . readP_to_S go
20 where digit :: ReadP Char
21 digit = satisfy (\char -> char >= '0' && char <= '9')
22 stGet :: String -> Status
23 stGet "wakes up" = Awake
24 stGet "falls asleep" = Asleep
25 stGet "Guard #" = Begin
26 go :: ReadP Shift
27 go = do
28 char '['
29 year <- read <$> count 4 digit
30 char '-'
31 month <- read <$> count 2 digit
32 char '-'
33 day <- read <$> count 2 digit
34 char ' '
35 hour <- read <$> count 2 digit
36 char ':'
37 minute <- read <$> count 2 digit
38 string "] "
39 rt <- string "Guard #" <|> string "wakes up" <|> string "falls asleep"
40 gid <- option (-1) (read <$> many1 digit)
41 optional $ string " begins shift"
42 eof
43 let d = Date year month day
44 let t = Time hour minute
45 return (Shift (stGet rt) gid d t)