aboutsummaryrefslogtreecommitdiffstats
path: root/day07/main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'day07/main.hs')
-rw-r--r--day07/main.hs29
1 files changed, 29 insertions, 0 deletions
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