AoC Benchmarks

aoc2021-day07b Haskell/GHC #2 program

source code


{-# LANGUAGE TypeApplications #-}

-- SPDX-License-Identifier: ISC
-- Copyright (c) 2021 Paolo Martini <mrtnpaolo@protonmail.com>

module Main (main) where

import Data.List (foldl')
import Data.Map.Strict qualified as M' (toAscList,fromListWith)

main =
  do crabs <- getInput parse 7
     print (part2 crabs)
  where
    parse xs = read @[Int] $ '[':xs++"]"

optimal cost xs = minimum (map cost [minimum xs..maximum xs])

part2 xs = optimal (\x -> sum [ triangle (abs (n-x)) | n <- xs]) xs
  where
    triangle n = n*(n+1)`div`2

-- Utilities

getInput :: (String -> a) -> Int -> IO a
getInput parse day = parse <$>  getContents
    

notes, command-line, and program output

NOTES:
Linux


Sun, 23 Jan 2022 14:49:07 GMT

MAKE:
ghc -O2 aoc2021_day07b.hs-2.hs -o app_hs
[1 of 1] Compiling Main             ( aoc2021_day07b.hs-2.hs, aoc2021_day07b.hs-2.o )
Linking app_hs ...

1.13s to complete and log all make actions

COMMAND LINE:
./app_hs 0 < aoc2021_day07b-input1000000.txt

TIMED OUT after 1400s


PROGRAM OUTPUT: