diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2021-12-09 10:01:10 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2021-12-09 10:01:10 +0200 |
| commit | 6d6b2f27a0bef3a592be29dbb87d2f18c2f0c145 (patch) | |
| tree | 43e52b6592559b7b0d8bc41506b657f8631935e5 /day07/main.hs | |
| parent | 4c620530e2a25f8d372f0f4357b917f78517879d (diff) | |
Day 07
Diffstat (limited to 'day07/main.hs')
| -rw-r--r-- | day07/main.hs | 29 |
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 |
