AoC2024/1/Skit.hs
2024-12-06 19:52:41 +01:00

33 lines
871 B
Haskell

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"