wow functional
This commit is contained in:
parent
9f162d4a72
commit
0339c7af10
1 changed files with 33 additions and 0 deletions
33
1/Skit.hs
Normal file
33
1/Skit.hs
Normal 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"
|
Loading…
Reference in a new issue