aoc-2021/07_day/code.hs

47 lines
1.3 KiB
Haskell
Raw Normal View History

2021-12-07 07:36:03 +00:00
import Data.List
splitStrOnce :: String -> String -> (String, String)
splitStrOnce separator "" = ("", "")
splitStrOnce "" subject = (subject, "")
splitStrOnce separator subject = do
let sepLen = length separator
if (take sepLen subject) == separator then
("", (drop sepLen subject))
else do
let res = splitStrOnce separator (tail subject)
(((head subject):(fst res)), (snd res))
splitStr :: String -> String -> [String]
splitStr separator "" = []
splitStr separator subject = do
let res = splitStrOnce separator subject
(fst res):(splitStr separator (snd res))
intOfString :: String -> Int
intOfString x = read x :: Int
minimumsSnd :: Ord b => [(a, b)] -> [(a, b)]
minimumsSnd [] = []
minimumsSnd xs = filter ((==) minsnd . snd) xs
where minsnd = minimum (map snd xs)
2021-12-07 07:40:17 +00:00
sumK n = ((n+1)*n) `div` 2
2021-12-07 07:36:03 +00:00
main :: IO ()
main = do
input <- getContents
let crabs = do {
map (\x -> read x :: Int) $
map (filter (\x -> x /= '\n')) $
splitStr "," input
}
let cases = do {
2021-12-07 07:40:17 +00:00
map
(\x -> (x, sum (map (\y -> sumK (abs (y-x))) crabs)))
[(minimum crabs)..(maximum crabs)]
2021-12-07 07:36:03 +00:00
}
let min = minimumsSnd
putStrLn $ show $ snd $ head $ min cases -- ans to part 2
2021-12-07 07:40:17 +00:00
-- Gross, but it works