AoC Benchmarks

aoc2021-day04b Haskell/GHC #2 program

source code


{-# LANGUAGE ImportQualifiedPost, TypeApplications, ViewPatterns #-}

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

module Main (main) where

import Data.List (unfoldr,partition,transpose)

main =
  do (balls,boards) <- getInput (parse . lines) 4
     let winners = play balls boards
     print (part2 winners)
  where
    parse (xs:_:ys) = (balls,boards)
      where
        balls = read @[Int] ('[' : xs ++ "]")

        boards = unfoldr collect ys
          where
            collect [] = Nothing
            collect xs = Just (board,drop 6 xs)
              where
                board = map (map (read @Int) . words) (take 5 xs)

play []     _      = []
play (n:ns) boards = map (score n) winners ++ play ns rest
  where
    (winners,rest) = partition winning (map (mark n) boards)

mark n = map (map replace)
  where
    replace x | x == n = -1 | otherwise = x

winning board = bingo board || bingo (transpose board)
  where
    bingo = any (all (-1 ==))

score ball b = ball * sum [ n | n <- concat b, n /= -1 ]

part2 = last

-- Utilities

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

getInputLines :: (String -> a) -> Int -> IO [a]
getInputLines parse day = getInput (map parse . lines) day

    

notes, command-line, and program output

NOTES:
Linux


Sun, 23 Jan 2022 13:44:20 GMT

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

0.71s to complete and log all make actions

COMMAND LINE:
./app_hs 0 < aoc2021_day04b-input900.txt

UNEXPECTED OUTPUT 

1c1
< 2785812
---
> 5371020

PROGRAM OUTPUT:
2785812