aboutsummaryrefslogtreecommitdiff
path: root/execs/Day04.hs
blob: 1834250812d23c0f5ff014877884df1fe46af1c4 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Main where

import           Data.Char                     (isDigit, isHexDigit)
import           Data.Map.Strict               (Map, (!))
import qualified Data.Map.Strict               as Map
import           Text.ParserCombinators.Parsec
import           Utils

requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ]
eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]

block = cell `sepBy` oneOf " \n"
cell = do
    tag <- many lower
    char ':'
    rest <- many (alphaNum <|> char '#')
    return (tag, rest)

parseInput :: String -> Either ParseError (Map String String)
parseInput s = Map.fromList <$> parse block "input" s

doCheck :: Map String String -> Bool
doCheck ls = all ((== True) . flip Map.member ls) requiredFields

validByr s = bet (read s :: Int) (1920, 2002)
validIyr s = bet (read s :: Int) (2010, 2020)
validEyr s = bet (read s :: Int) (2020, 2030)
validEcl = flip elem eyeColors
validPid s = length s == 9 && all isDigit s
validHcl ('#':rest) = length rest == 6 && all isHexDigit rest
validHcl _          = False
validHgt s =
    let value = takeWhile isDigit s
        unit = dropWhile isDigit s
        height = (read value :: Int, unit)
     in case height of
          (v, "cm") -> bet v (150, 193)
          (v, "in") -> bet v (59, 76)
          _         -> False

doValidate :: Map String String -> Bool
doValidate map = all ((== True) . (\(s, v) -> v $ map ! s))  ls
    where ls = [ ("byr", validByr)
               , ("iyr", validIyr)
               , ("eyr", validEyr)
               , ("hgt", validHgt)
               , ("hcl", validHcl)
               , ("ecl", validEcl)
               , ("pid", validPid)
               ]

parseLines :: [String] -> [String]
parseLines allLines = unwords first : next
  where (first, rest) = break null allLines
        next = if null rest then [] else parseLines (tail rest)

main :: IO ()
main = do
    n <- parseLines . lines <$> readFile "input/04"
    let blocks = map (right . parseInput) n
    {- part 1 -}
    print $ length $ filter doCheck blocks
    {- part 2 -}
    print $ length $ filter (doCheck &+ doValidate) blocks