From 2fc2e7351e3584d005c0aea1deb2f733aa056f2d Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 11 Dec 2022 13:04:30 +0100 Subject: [PATCH] finish day three --- three.hs | 11 +++++++++-- three2.hs | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 three2.hs 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