wow functional

This commit is contained in:
kirreen 2024-12-06 19:52:41 +01:00
parent 9f162d4a72
commit 0339c7af10

33
1/Skit.hs Normal file
View file

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