asdf
This commit is contained in:
parent
70a8c18a27
commit
ffb5f12540
1 changed files with 15 additions and 16 deletions
31
4.hs
31
4.hs
|
@ -13,7 +13,7 @@ data Attr = Byr | Iyr | Eyr | Hgt | Hcl | Ecl | Pid | Cid
|
||||||
deriving (Show, Bounded, Enum, Eq, Ord)
|
deriving (Show, Bounded, Enum, Eq, Ord)
|
||||||
|
|
||||||
intval :: Int -> Int -> Int -> Maybe Int
|
intval :: Int -> Int -> Int -> Maybe Int
|
||||||
intval lo hi n = guard (n >= lo && n <= hi) $> n
|
intval lo hi n = guard (lo <= n && n <= hi) $> n
|
||||||
|
|
||||||
nDigits :: Int -> String -> Maybe String
|
nDigits :: Int -> String -> Maybe String
|
||||||
nDigits n s = guard (length s == n && all isDigit s) $> s
|
nDigits n s = guard (length s == n && all isDigit s) $> s
|
||||||
|
@ -26,21 +26,20 @@ validEcl = flip elem ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
|
||||||
|
|
||||||
parseAttr :: String -> Maybe Attr
|
parseAttr :: String -> Maybe Attr
|
||||||
parseAttr s =
|
parseAttr s =
|
||||||
let parts = splitOn ":" s in
|
case splitOn ":" s of
|
||||||
let key = parts !! 0 in
|
[key, value] -> case key of
|
||||||
let value = parts !! 1 in
|
"byr" -> (readMaybe value >>= intval 1920 2002) $> Byr
|
||||||
case key of
|
"iyr" -> (readMaybe value >>= intval 2010 2020) $> Iyr
|
||||||
"byr" -> (readMaybe value >>= intval 1920 2002) $> Byr
|
"eyr" -> (readMaybe value >>= intval 2020 2030) $> Eyr
|
||||||
"iyr" -> (readMaybe value >>= intval 2010 2020) $> Iyr
|
"hgt" ->
|
||||||
"eyr" -> (readMaybe value >>= intval 2020 2030) $> Eyr
|
let inch = stripSuffix "cm" value >>= readMaybe >>= intval 150 193 in
|
||||||
"hgt" ->
|
let cm = stripSuffix "in" value >>= readMaybe >>= intval 59 76 in
|
||||||
let inch = stripSuffix "cm" value >>= readMaybe >>= intval 150 193 in
|
(inch <|> cm) $> Hgt
|
||||||
let cm = stripSuffix "in" value >>= readMaybe >>= intval 59 76 in
|
"hcl" -> hexColor value $> Hcl
|
||||||
(inch <|> cm) $> Hgt
|
"ecl" -> if validEcl value then Just Ecl else Nothing
|
||||||
"hcl" -> hexColor value $> Hcl
|
"pid" -> nDigits 9 value $> Pid
|
||||||
"ecl" -> if validEcl value then Just Ecl else Nothing
|
"cid" -> Just Cid
|
||||||
"pid" -> nDigits 9 value $> Pid
|
_ -> Nothing
|
||||||
"cid" -> Just Cid
|
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
processLine :: String -> Maybe [Attr]
|
processLine :: String -> Maybe [Attr]
|
||||||
|
|
Loading…
Add table
Reference in a new issue