finish day three

This commit is contained in:
qvalentin 2022-12-11 13:04:30 +01:00
parent 018d194796
commit 2fc2e7351e
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
2 changed files with 31 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import Data.Char
splitlist :: [a] -> ([a],[a]) splitlist :: [a] -> ([a],[a])
splitlist list = (take n list,drop n list) splitlist list = (take n list,drop n list)
@ -8,8 +9,14 @@ inBoth (first,second) = do
inFirst <- first inFirst <- first
if (inFirst `elem` second) then return inFirst else [] 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 main = do
input <- readFile "three-input.txt" input <- readFile "three-input.txt"
print $ map ( inBoth . splitlist ) $ lines input print $sum $ map (toPriority . head . inBoth . splitlist ) $ lines input

22
three2.hs Normal file
View File

@ -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