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)
|
||||
|
||||
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 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 s =
|
||||
let parts = splitOn ":" s in
|
||||
let key = parts !! 0 in
|
||||
let value = parts !! 1 in
|
||||
case key of
|
||||
"byr" -> (readMaybe value >>= intval 1920 2002) $> Byr
|
||||
"iyr" -> (readMaybe value >>= intval 2010 2020) $> Iyr
|
||||
"eyr" -> (readMaybe value >>= intval 2020 2030) $> Eyr
|
||||
"hgt" ->
|
||||
let inch = stripSuffix "cm" value >>= readMaybe >>= intval 150 193 in
|
||||
let cm = stripSuffix "in" value >>= readMaybe >>= intval 59 76 in
|
||||
(inch <|> cm) $> Hgt
|
||||
"hcl" -> hexColor value $> Hcl
|
||||
"ecl" -> if validEcl value then Just Ecl else Nothing
|
||||
"pid" -> nDigits 9 value $> Pid
|
||||
"cid" -> Just Cid
|
||||
case splitOn ":" s of
|
||||
[key, value] -> case key of
|
||||
"byr" -> (readMaybe value >>= intval 1920 2002) $> Byr
|
||||
"iyr" -> (readMaybe value >>= intval 2010 2020) $> Iyr
|
||||
"eyr" -> (readMaybe value >>= intval 2020 2030) $> Eyr
|
||||
"hgt" ->
|
||||
let inch = stripSuffix "cm" value >>= readMaybe >>= intval 150 193 in
|
||||
let cm = stripSuffix "in" value >>= readMaybe >>= intval 59 76 in
|
||||
(inch <|> cm) $> Hgt
|
||||
"hcl" -> hexColor value $> Hcl
|
||||
"ecl" -> if validEcl value then Just Ecl else Nothing
|
||||
"pid" -> nDigits 9 value $> Pid
|
||||
"cid" -> Just Cid
|
||||
_ -> Nothing
|
||||
_ -> Nothing
|
||||
|
||||
processLine :: String -> Maybe [Attr]
|
||||
|
|
Loading…
Reference in a new issue