summaryrefslogtreecommitdiff
path: root/Day3/app/Main.hs
blob: 2399b86c23f86610f8f747506b3eec9c000d902c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module Main (main) where

import System.Environment

main :: IO ()
main = getArgs >>= readFile . head >>= print . solveDay3

solveDay3 :: String -> (Integer, Integer)
solveDay3 x = (solveDay3Part1 x, solveDay3Part2 x)

solveDay3Part1 :: String -> Integer
solveDay3Part1 = sum . map getLinePriority . lines

getLinePriority :: String -> Integer
getLinePriority = getCharPriority . findCharacterInFirstAndSecondHalf

getCharPriority :: Maybe Char -> Integer
getCharPriority (Just 'a') = 01
getCharPriority (Just 'b') = 02
getCharPriority (Just 'c') = 03
getCharPriority (Just 'd') = 04
getCharPriority (Just 'e') = 05
getCharPriority (Just 'f') = 06
getCharPriority (Just 'g') = 07
getCharPriority (Just 'h') = 08
getCharPriority (Just 'i') = 09
getCharPriority (Just 'j') = 10
getCharPriority (Just 'k') = 11
getCharPriority (Just 'l') = 12
getCharPriority (Just 'm') = 13
getCharPriority (Just 'n') = 14
getCharPriority (Just 'o') = 15
getCharPriority (Just 'p') = 16
getCharPriority (Just 'q') = 17
getCharPriority (Just 'r') = 18
getCharPriority (Just 's') = 19
getCharPriority (Just 't') = 20
getCharPriority (Just 'u') = 21
getCharPriority (Just 'v') = 22
getCharPriority (Just 'w') = 23
getCharPriority (Just 'x') = 24
getCharPriority (Just 'y') = 25
getCharPriority (Just 'z') = 26
getCharPriority (Just 'A') = 27
getCharPriority (Just 'B') = 28
getCharPriority (Just 'C') = 29
getCharPriority (Just 'D') = 30
getCharPriority (Just 'E') = 31
getCharPriority (Just 'F') = 32
getCharPriority (Just 'G') = 33
getCharPriority (Just 'H') = 34
getCharPriority (Just 'I') = 35
getCharPriority (Just 'J') = 36
getCharPriority (Just 'K') = 37
getCharPriority (Just 'L') = 38
getCharPriority (Just 'M') = 39
getCharPriority (Just 'N') = 40
getCharPriority (Just 'O') = 41
getCharPriority (Just 'P') = 42
getCharPriority (Just 'Q') = 43
getCharPriority (Just 'R') = 44
getCharPriority (Just 'S') = 45
getCharPriority (Just 'T') = 46
getCharPriority (Just 'U') = 47
getCharPriority (Just 'V') = 48
getCharPriority (Just 'W') = 49
getCharPriority (Just 'X') = 50
getCharPriority (Just 'Y') = 51
getCharPriority (Just 'Z') = 52
getCharPriority _ = 0

findCharacterInFirstAndSecondHalf :: String -> Maybe Char
findCharacterInFirstAndSecondHalf x =  findElementInBothLists (splitAt (length x `div` 2) x)

findElementInBothLists :: (Eq a) => ([a],[a]) -> Maybe a
findElementInBothLists (a:as,b) = if a `elem` b then Just a else findElementInBothLists (as,b)
findElementInBothLists _ = Nothing

-- Todo
solveDay3Part2 :: String -> Integer
solveDay3Part2 = sum . map getGroupPriority . elfGroups

getGroupPriority :: (String, String, String) -> Integer
getGroupPriority = getCharPriority . findElementInThreeLists 

findElementInThreeLists :: (Eq a) => ([a],[a],[a]) -> Maybe a
findElementInThreeLists (a:as,b,c) = if a `elem` b && a `elem` c then Just a else findElementInThreeLists (as,b,c)

elfGroups :: String -> [(String, String, String)]
elfGroups = groupThree . lines

groupThree :: [a] -> [(a,a,a)]
groupThree (a:b:c:as) = (a,b,c) : groupThree as
groupThree _ = []