diff options
author | Akshay <[email protected]> | 2020-12-05 07:49:25 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-05 07:49:25 +0000 |
commit | 555da2ae6f658672cfc0d37e437ec356c0c0fa63 (patch) | |
tree | f1b1b2e411ac130373421aece0cb8e5709a0e925 /execs/Day04.hs | |
parent | e0a5d9cbf99c2d6bbaef12f2ce5506eda0ba5bec (diff) |
factor common functions into Utils
Diffstat (limited to 'execs/Day04.hs')
-rw-r--r-- | execs/Day04.hs | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/execs/Day04.hs b/execs/Day04.hs index 565415f..a95dfd0 100644 --- a/execs/Day04.hs +++ b/execs/Day04.hs | |||
@@ -1,11 +1,11 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | import Text.ParserCombinators.Parsec | 3 | import Text.ParserCombinators.Parsec |
4 | import Data.Map.Strict (Map, (!)) | 4 | import Data.Map.Strict (Map, (!)) |
5 | import qualified Data.Map.Strict as Map | 5 | import qualified Data.Map.Strict as Map |
6 | import Data.Char (isDigit, isHexDigit) | 6 | import Data.Char (isDigit, isHexDigit) |
7 | import Utils | ||
7 | 8 | ||
8 | right (Right a) = a | ||
9 | requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ] | 9 | requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ] |
10 | eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"] | 10 | eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"] |
11 | 11 | ||
@@ -22,9 +22,6 @@ parseInput s = Map.fromList <$> parse block "input" s | |||
22 | doCheck :: Map String String -> Bool | 22 | doCheck :: Map String String -> Bool |
23 | doCheck ls = all ((== True) . flip Map.member ls) requiredFields | 23 | doCheck ls = all ((== True) . flip Map.member ls) requiredFields |
24 | 24 | ||
25 | bet :: Int -> (Int, Int) -> Bool | ||
26 | bet k (l, u) = k >= l && k <= u | ||
27 | |||
28 | validByr s = bet (read s :: Int) (1920, 2002) | 25 | validByr s = bet (read s :: Int) (1920, 2002) |
29 | validIyr s = bet (read s :: Int) (2010, 2020) | 26 | validIyr s = bet (read s :: Int) (2010, 2020) |
30 | validEyr s = bet (read s :: Int) (2020, 2030) | 27 | validEyr s = bet (read s :: Int) (2020, 2030) |
@@ -41,8 +38,8 @@ validHgt s = | |||
41 | (v, "in") -> bet v (59, 76) | 38 | (v, "in") -> bet v (59, 76) |
42 | _ -> False | 39 | _ -> False |
43 | 40 | ||
44 | doValidity :: Map String String -> Bool | 41 | doValidate :: Map String String -> Bool |
45 | doValidity map = all ((== True) . (\(s, v) -> v $ map ! s)) ls | 42 | doValidate map = all ((== True) . (\(s, v) -> v $ map ! s)) ls |
46 | where ls = [ ("byr", validByr) | 43 | where ls = [ ("byr", validByr) |
47 | , ("iyr", validIyr) | 44 | , ("iyr", validIyr) |
48 | , ("eyr", validEyr) | 45 | , ("eyr", validEyr) |
@@ -64,4 +61,4 @@ main = do | |||
64 | {- part 1 -} | 61 | {- part 1 -} |
65 | print $ length $ filter doCheck blocks | 62 | print $ length $ filter doCheck blocks |
66 | {- part 2 -} | 63 | {- part 2 -} |
67 | print $ length $ filter (\p -> doCheck p && doValidity p) blocks | 64 | print $ length $ filter (doCheck &+ doValidate) blocks |