From 6d6b2f27a0bef3a592be29dbb87d2f18c2f0c145 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 9 Dec 2021 10:01:10 +0200 Subject: Day 07 --- day07/main.hs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day07/main.hs (limited to 'day07/main.hs') diff --git a/day07/main.hs b/day07/main.hs new file mode 100644 index 0000000..39f001e --- /dev/null +++ b/day07/main.hs @@ -0,0 +1,29 @@ +module Main where + +import Data.Text (pack, split, unpack) +import Utils + +move1 :: Int -> Int -> Int +move1 target crab = abs (crab - target) + +move2 :: Int -> Int -> Int +move2 target crab = + let n = abs (crab - target) + in n * (n + 1) `div` 2 + +makeMoves moveFn crabs target = foldr (\crab acc -> acc + moveFn target crab) 0 crabs + +e :: (Int -> Int -> Int) -> [Int] -> Int +e moveFn crabs = + let targets = [minimum crabs .. maximum crabs] + consumptions = map (makeMoves moveFn crabs) targets + in minimum consumptions + +main :: IO () +main = + do + contents <- getContents + let input = contents $> pack .> split (== ',') .> map (unpack .> read) :: [Int] + + e move1 input $> print + e move2 input $> print -- cgit v1.3