-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.hs
More file actions
31 lines (24 loc) · 1013 Bytes
/
Task1.hs
File metadata and controls
31 lines (24 loc) · 1013 Bytes
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
module Task1 where
import System.IO
import Data.List
import Data.List.Split
main :: IO ()
main = do
--handle <- openFile "test1.txt" ReadMode
handle <- openFile "task1.txt" ReadMode
contents <- hGetContents handle
--print $ lines contents
let listOfPairs = transformLists $ map splitNumbers $ lines contents
sorted = (\(a,b) -> (sort a, sort b)) listOfPairs
differences = uncurry (zipWith (\x y -> abs(x-y))) sorted
occurences = map (numberOfOccurences (snd sorted)) (fst sorted)
print $ "Part 1: " ++ (show $ sum differences)
print $ "Part 2: " ++ (show $ sum $ zipWith (*) occurences (fst sorted))
hClose handle
splitNumbers :: String -> (Int, Int)
splitNumbers str = (read $ head splitted, read $ last splitted) where
splitted = splitOn " " str
transformLists :: [(Int, Int)] -> ([Int], [Int])
transformLists xs = (map fst xs, map snd xs)
numberOfOccurences :: [Int] -> Int -> Int
numberOfOccurences xs x = length $ filter (==x) xs