diff options
-rw-r--r-- | aoc.cabal | 16 | ||||
-rw-r--r-- | execs/Day02.hs | 7 | ||||
-rw-r--r-- | execs/Day04.hs | 17 | ||||
-rw-r--r-- | execs/Day05.hs | 9 | ||||
-rw-r--r-- | lib/Utils.hs | 32 |
5 files changed, 55 insertions, 26 deletions
@@ -16,32 +16,38 @@ maintainer: [email protected] | |||
16 | build-type: Simple | 16 | build-type: Simple |
17 | extra-source-files: CHANGELOG.md | 17 | extra-source-files: CHANGELOG.md |
18 | 18 | ||
19 | library | ||
20 | hs-source-dirs: lib | ||
21 | default-language: Haskell2010 | ||
22 | build-depends: base | ||
23 | exposed-modules: Utils | ||
24 | |||
19 | executable Day01 | 25 | executable Day01 |
20 | main-is: Day01.hs | 26 | main-is: Day01.hs |
21 | build-depends: base | 27 | build-depends: base, aoc |
22 | default-language: Haskell2010 | 28 | default-language: Haskell2010 |
23 | hs-source-dirs: execs | 29 | hs-source-dirs: execs |
24 | 30 | ||
25 | executable Day02 | 31 | executable Day02 |
26 | main-is: Day02.hs | 32 | main-is: Day02.hs |
27 | build-depends: base, parsec | 33 | build-depends: base, parsec, aoc |
28 | default-language: Haskell2010 | 34 | default-language: Haskell2010 |
29 | hs-source-dirs: execs | 35 | hs-source-dirs: execs |
30 | 36 | ||
31 | executable Day03 | 37 | executable Day03 |
32 | main-is: Day03.hs | 38 | main-is: Day03.hs |
33 | build-depends: base | 39 | build-depends: base, aoc |
34 | default-language: Haskell2010 | 40 | default-language: Haskell2010 |
35 | hs-source-dirs: execs | 41 | hs-source-dirs: execs |
36 | 42 | ||
37 | executable Day04 | 43 | executable Day04 |
38 | main-is: Day04.hs | 44 | main-is: Day04.hs |
39 | build-depends: base, parsec, containers | 45 | build-depends: base, parsec, containers, aoc |
40 | default-language: Haskell2010 | 46 | default-language: Haskell2010 |
41 | hs-source-dirs: execs | 47 | hs-source-dirs: execs |
42 | 48 | ||
43 | executable Day05 | 49 | executable Day05 |
44 | main-is: Day05.hs | 50 | main-is: Day05.hs |
45 | build-depends: base | 51 | build-depends: base, aoc |
46 | default-language: Haskell2010 | 52 | default-language: Haskell2010 |
47 | hs-source-dirs: execs | 53 | hs-source-dirs: execs |
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 |
diff --git a/lib/Utils.hs b/lib/Utils.hs new file mode 100644 index 0000000..1eb42fc --- /dev/null +++ b/lib/Utils.hs | |||
@@ -0,0 +1,32 @@ | |||
1 | module Utils ( binaryToInt | ||
2 | , countElem | ||
3 | , xor | ||
4 | , right | ||
5 | , bet | ||
6 | , (&+) | ||
7 | ) where | ||
8 | |||
9 | import Data.Char (digitToInt) | ||
10 | import Control.Monad | ||
11 | import Data.Either | ||
12 | |||
13 | |||
14 | binaryToInt :: String -> Int | ||
15 | binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 | ||
16 | |||
17 | countElem :: (Eq a) => a -> [a] -> Int | ||
18 | countElem c ls = sum $ map (fromEnum . (== c)) ls | ||
19 | |||
20 | xor :: Bool -> Bool -> Bool | ||
21 | xor a b = (not a && b) || (a && not b) | ||
22 | |||
23 | right :: Either a b -> b | ||
24 | right (Right b) = b | ||
25 | right _ = undefined | ||
26 | |||
27 | bet :: Int -> (Int, Int) -> Bool | ||
28 | bet k (l, u) = k >= l && k <= u | ||
29 | |||
30 | -- combine filter predicates | ||
31 | (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) | ||
32 | (&+) = liftM2 (&&) | ||