diff --git a/three.hs b/three.hs index dcc0e05..c26d86b 100644 --- a/three.hs +++ b/three.hs @@ -1,3 +1,4 @@ +import Data.Char splitlist :: [a] -> ([a],[a]) splitlist list = (take n list,drop n list) @@ -8,8 +9,14 @@ inBoth (first,second) = do inFirst <- first if (inFirst `elem` second) then return inFirst else [] - +toPriority :: Char -> Int +toPriority char = ordNum - (substract ordNum) + where + ordNum = (ord char) + substract n + | n < 97 = 38 + | otherwise = 96 main = do input <- readFile "three-input.txt" - print $ map ( inBoth . splitlist ) $ lines input + print $sum $ map (toPriority . head . inBoth . splitlist ) $ lines input diff --git a/three2.hs b/three2.hs new file mode 100644 index 0000000..a3d7b0a --- /dev/null +++ b/three2.hs @@ -0,0 +1,22 @@ +import Data.Char +import Data.List.Split + + +inAll :: [String] -> [Char] +inAll [first,second,third] = do + eachChar <- first + if (eachChar `elem` second && eachChar `elem` third) then return eachChar else [] + + +toPriority :: Char -> Int +toPriority char = ordNum - (substract ordNum) + where + ordNum = (ord char) + substract n + | n < 97 = 38 + | otherwise = 96 + +main = do + input <- readFile "three-input.txt" + print $sum $ map (toPriority . head . inAll ) $ chunksOf 3 $ lines input + --print $map ( inAll ) $ chunksOf 3 $ lines input