diff options
author | Akshay <[email protected]> | 2020-12-02 05:40:11 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-02 05:40:11 +0000 |
commit | 7e617b670988e50c43a140a19033162c8695b716 (patch) | |
tree | 90558432be3aaa967aa4b6ed92c3be74778e421c /execs | |
parent | cb13d9733b7af2ee7662ffb428cf6e2a11df138a (diff) |
add initial day 2 solution
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day02.hs | 39 |
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 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Data.Char | ||
4 | import Control.Monad | ||
5 | |||
6 | countElem :: (Eq a) => a -> [a] -> Int | ||
7 | countElem c ls = sum $ map (fromEnum . (== c)) ls | ||
8 | |||
9 | xor :: Bool -> Bool -> Bool | ||
10 | xor True True = False | ||
11 | xor True False = True | ||
12 | xor False True = True | ||
13 | xor False False = False | ||
14 | |||
15 | ranges :: [String] -> (Int, Int) | ||
16 | ranges r = | ||
17 | let p = head r | ||
18 | start = read $ takeWhile (/= '-') p | ||
19 | end = abs . read $ dropWhile (/= '-') p | ||
20 | in (start, end) | ||
21 | |||
22 | charac :: [String] -> Char | ||
23 | charac s = head (s !! 1) | ||
24 | |||
25 | pass :: [String] -> String | ||
26 | pass = last | ||
27 | |||
28 | doCheck1 :: ((Int, Int), Char , String) -> Bool | ||
29 | doCheck1 ((lower, upper), c, pass) = count >= lower && count <= upper | ||
30 | where count = countElem c pass | ||
31 | |||
32 | doCheck2 :: ((Int, Int), Char , String) -> Bool | ||
33 | doCheck2 ((lower, upper), c, pass) = (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c) | ||
34 | |||
35 | main :: IO () | ||
36 | main = do | ||
37 | n <- map (fromEnum . doCheck2 . (\w -> (ranges w, charac w, pass w)) . words) . lines <$> readFile "input/02" | ||
38 | print $ sum n | ||
39 | |||