aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc.cabal16
-rw-r--r--execs/Day02.hs7
-rw-r--r--execs/Day04.hs17
-rw-r--r--execs/Day05.hs9
-rw-r--r--lib/Utils.hs32
5 files changed, 55 insertions, 26 deletions
diff --git a/aoc.cabal b/aoc.cabal
index a5be71d..6690e58 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -16,32 +16,38 @@ maintainer: [email protected]
16build-type: Simple 16build-type: Simple
17extra-source-files: CHANGELOG.md 17extra-source-files: CHANGELOG.md
18 18
19library
20 hs-source-dirs: lib
21 default-language: Haskell2010
22 build-depends: base
23 exposed-modules: Utils
24
19executable Day01 25executable 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
25executable Day02 31executable 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
31executable Day03 37executable 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
37executable Day04 43executable 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
43executable Day05 49executable 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
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
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 @@
1module Utils ( binaryToInt
2 , countElem
3 , xor
4 , right
5 , bet
6 , (&+)
7 ) where
8
9import Data.Char (digitToInt)
10import Control.Monad
11import Data.Either
12
13
14binaryToInt :: String -> Int
15binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0
16
17countElem :: (Eq a) => a -> [a] -> Int
18countElem c ls = sum $ map (fromEnum . (== c)) ls
19
20xor :: Bool -> Bool -> Bool
21xor a b = (not a && b) || (a && not b)
22
23right :: Either a b -> b
24right (Right b) = b
25right _ = undefined
26
27bet :: Int -> (Int, Int) -> Bool
28bet k (l, u) = k >= l && k <= u
29
30-- combine filter predicates
31(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
32(&+) = liftM2 (&&)