aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-05 07:49:25 +0000
committerAkshay <[email protected]>2020-12-05 07:49:25 +0000
commit555da2ae6f658672cfc0d37e437ec356c0c0fa63 (patch)
treef1b1b2e411ac130373421aece0cb8e5709a0e925 /execs
parente0a5d9cbf99c2d6bbaef12f2ce5506eda0ba5bec (diff)
factor common functions into Utils
Diffstat (limited to 'execs')
-rw-r--r--execs/Day02.hs7
-rw-r--r--execs/Day04.hs17
-rw-r--r--execs/Day05.hs9
3 files changed, 12 insertions, 21 deletions
diff --git a/execs/Day02.hs b/execs/Day02.hs
index a2815b7..c462ec9 100644
--- a/execs/Day02.hs
+++ b/execs/Day02.hs
@@ -2,12 +2,7 @@ module Main where
2 2
3import Text.ParserCombinators.Parsec 3import Text.ParserCombinators.Parsec
4import Text.Parsec.Char 4import Text.Parsec.Char
5 5import Utils
6countElem c ls = sum $ map (fromEnum . (== c)) ls
7
8xor a b = (not a && b) || (a && not b)
9
10right (Right a) = a
11 6
12data PassProp = PassProp 7data PassProp = PassProp
13 { range :: (Int, Int) 8 { range :: (Int, Int)
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 @@
1module Main where 1module Main where
2 2
3import Text.ParserCombinators.Parsec 3import Text.ParserCombinators.Parsec
4import Data.Map.Strict (Map, (!)) 4import Data.Map.Strict (Map, (!))
5import qualified Data.Map.Strict as Map 5import qualified Data.Map.Strict as Map
6import Data.Char (isDigit, isHexDigit) 6import Data.Char (isDigit, isHexDigit)
7import Utils
7 8
8right (Right a) = a
9requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ] 9requiredFields = [ "byr" , "iyr" , "eyr" , "hgt" , "hcl" , "ecl" , "pid" ]
10eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"] 10eyeColors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
11 11
@@ -22,9 +22,6 @@ parseInput s = Map.fromList <$> parse block "input" s
22doCheck :: Map String String -> Bool 22doCheck :: Map String String -> Bool
23doCheck ls = all ((== True) . flip Map.member ls) requiredFields 23doCheck ls = all ((== True) . flip Map.member ls) requiredFields
24 24
25bet :: Int -> (Int, Int) -> Bool
26bet k (l, u) = k >= l && k <= u
27
28validByr s = bet (read s :: Int) (1920, 2002) 25validByr s = bet (read s :: Int) (1920, 2002)
29validIyr s = bet (read s :: Int) (2010, 2020) 26validIyr s = bet (read s :: Int) (2010, 2020)
30validEyr s = bet (read s :: Int) (2020, 2030) 27validEyr 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
44doValidity :: Map String String -> Bool 41doValidate :: Map String String -> Bool
45doValidity map = all ((== True) . (\(s, v) -> v $ map ! s)) ls 42doValidate 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
diff --git a/execs/Day05.hs b/execs/Day05.hs
index aae8f49..27397f7 100644
--- a/execs/Day05.hs
+++ b/execs/Day05.hs
@@ -1,12 +1,11 @@
1module Main where 1module Main where
2 2
3import Data.Char (digitToInt) 3import Data.Char (digitToInt)
4import Utils (binaryToInt)
4 5
5doValidate = toInt . map readBin 6doValidate = binaryToInt . map readBin
6toInt = foldl (\a x -> a * 2 + digitToInt x) 0 7readBin s | s `elem` "FL" = '0'
7readBin 'F' = '0' 8 | otherwise = '1'
8readBin 'L' = '0'
9readBin _ = '1'
10 9
11main :: IO () 10main :: IO ()
12main = do 11main = do