diff options
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day02.hs | 7 | ||||
-rw-r--r-- | execs/Day04.hs | 17 | ||||
-rw-r--r-- | execs/Day05.hs | 9 |
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 | ||
3 | import Text.ParserCombinators.Parsec | 3 | import Text.ParserCombinators.Parsec |
4 | import Text.Parsec.Char | 4 | import Text.Parsec.Char |
5 | 5 | import Utils | |
6 | countElem c ls = sum $ map (fromEnum . (== c)) ls | ||
7 | |||
8 | xor a b = (not a && b) || (a && not b) | ||
9 | |||
10 | right (Right a) = a | ||
11 | 6 | ||
12 | data PassProp = PassProp | 7 | data 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 @@ | |||
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 |
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 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | import Data.Char (digitToInt) | 3 | import Data.Char (digitToInt) |
4 | import Utils (binaryToInt) | ||
4 | 5 | ||
5 | doValidate = toInt . map readBin | 6 | doValidate = binaryToInt . map readBin |
6 | toInt = foldl (\a x -> a * 2 + digitToInt x) 0 | 7 | readBin s | s `elem` "FL" = '0' |
7 | readBin 'F' = '0' | 8 | | otherwise = '1' |
8 | readBin 'L' = '0' | ||
9 | readBin _ = '1' | ||
10 | 9 | ||
11 | main :: IO () | 10 | main :: IO () |
12 | main = do | 11 | main = do |