aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-02 05:40:11 +0000
committerAkshay <[email protected]>2020-12-02 05:40:11 +0000
commit7e617b670988e50c43a140a19033162c8695b716 (patch)
tree90558432be3aaa967aa4b6ed92c3be74778e421c /execs
parentcb13d9733b7af2ee7662ffb428cf6e2a11df138a (diff)
add initial day 2 solution
Diffstat (limited to 'execs')
-rw-r--r--execs/Day02.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/execs/Day02.hs b/execs/Day02.hs
new file mode 100644
index 0000000..bcb0896
--- /dev/null
+++ b/execs/Day02.hs
@@ -0,0 +1,39 @@
1module Main where
2
3import Data.Char
4import Control.Monad
5
6countElem :: (Eq a) => a -> [a] -> Int
7countElem c ls = sum $ map (fromEnum . (== c)) ls
8
9xor :: Bool -> Bool -> Bool
10xor True True = False
11xor True False = True
12xor False True = True
13xor False False = False
14
15ranges :: [String] -> (Int, Int)
16ranges r =
17 let p = head r
18 start = read $ takeWhile (/= '-') p
19 end = abs . read $ dropWhile (/= '-') p
20 in (start, end)
21
22charac :: [String] -> Char
23charac s = head (s !! 1)
24
25pass :: [String] -> String
26pass = last
27
28doCheck1 :: ((Int, Int), Char , String) -> Bool
29doCheck1 ((lower, upper), c, pass) = count >= lower && count <= upper
30 where count = countElem c pass
31
32doCheck2 :: ((Int, Int), Char , String) -> Bool
33doCheck2 ((lower, upper), c, pass) = (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c)
34
35main :: IO ()
36main = do
37 n <- map (fromEnum . doCheck2 . (\w -> (ranges w, charac w, pass w)) . words) . lines <$> readFile "input/02"
38 print $ sum n
39