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
65
66
67
|
module Main where
import Text.ParserCombinators.Parsec
import Data.Map.Strict (Map, (!))
import qualified Data.Map.Strict as Map
import Data.Char (isDigit, isHexDigit)
right (Right a) = a
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
bet :: Int -> (Int, Int) -> Bool
bet k (l, u) = k >= l && k <= u
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
doValidity :: Map String String -> Bool
doValidity 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 (\p -> doCheck p && doValidity p) blocks
|