From 0339c7af10284517df7343d3ec72198b6ad2d80e Mon Sep 17 00:00:00 2001 From: kirreen Date: Fri, 6 Dec 2024 19:52:41 +0100 Subject: [PATCH] wow functional --- 1/Skit.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1/Skit.hs diff --git a/1/Skit.hs b/1/Skit.hs new file mode 100644 index 0000000..9d7216f --- /dev/null +++ b/1/Skit.hs @@ -0,0 +1,33 @@ +import Data.List (sort, transpose) + +main :: IO () +main = do + input <- readFile "./input" + putStrLn (solve input) + +solve :: String -> String +solve input = show (riktigaSolve (parseInput input)) + +--splitString :: String -> [String] +--splitString input = lines input + +parseInput :: String -> [[Int]] +parseInput input = transpose (stringToIntList (lines input)) + where + getPairs :: String -> [Int] + getPairs line = map stringToInt (words line) + + stringToInt :: String -> Int + stringToInt input = read input + + stringToIntList :: [String] -> [[Int]] + stringToIntList input = map getPairs input + +riktigaSolve :: [[Int]] -> Int +riktigaSolve [left,right] = svaret + where + sortedLeft = sort left + sortedRight = sort right + absedList = map (\(x, y) -> abs(x - y)) (zip sortedLeft sortedRight) + svaret = sum absedList +riktigaSolve fuckedShit = error "hlvetes textfil"