From 555da2ae6f658672cfc0d37e437ec356c0c0fa63 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 5 Dec 2020 13:19:25 +0530 Subject: factor common functions into Utils --- execs/Day04.hs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'execs/Day04.hs') 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 @@ module Main where -import Text.ParserCombinators.Parsec -import Data.Map.Strict (Map, (!)) +import Text.ParserCombinators.Parsec +import Data.Map.Strict (Map, (!)) import qualified Data.Map.Strict as Map -import Data.Char (isDigit, isHexDigit) +import Data.Char (isDigit, isHexDigit) +import Utils -right (Right a) = a requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ] eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"] @@ -22,9 +22,6 @@ 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) @@ -41,8 +38,8 @@ validHgt s = (v, "in") -> bet v (59, 76) _ -> False -doValidity :: Map String String -> Bool -doValidity map = all ((== True) . (\(s, v) -> v $ map ! s)) ls +doValidate :: Map String String -> Bool +doValidate map = all ((== True) . (\(s, v) -> v $ map ! s)) ls where ls = [ ("byr", validByr) , ("iyr", validIyr) , ("eyr", validEyr) @@ -64,4 +61,4 @@ main = do {- part 1 -} print $ length $ filter doCheck blocks {- part 2 -} - print $ length $ filter (\p -> doCheck p && doValidity p) blocks + print $ length $ filter (doCheck &+ doValidate) blocks -- cgit v1.2.3