{-|
Module:         Day6
Description:    <https://adventofcode.com/2021/day/6 Day 6: Lanternfish>
-}
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
module Day6 (day6a, day6b) where

import Common (readEntire)
import Data.Array.Unboxed (UArray, (!))
import Data.Text (Text)
import qualified Data.Text as T (lines, splitOn)
import qualified Data.Text.Read as T (decimal)
import Day6Meta (mkLUT)

day6 :: UArray Int Int -> Text -> Either String Int
day6 :: UArray Int Int -> Text -> Either String Int
day6 UArray Int Int
lut Text
input = do
    [Int]
nums <- (Text -> Either String Int) -> [Text] -> Either String [Int]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Reader Int -> Text -> Either String Int
forall a. Reader a -> Text -> Either String a
readEntire Reader Int
forall a. Integral a => Reader a
T.decimal) ([Text] -> Either String [Int]) -> [Text] -> Either String [Int]
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [Text]
T.splitOn Text
"," (Text -> [Text]) -> [Text] -> [Text]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Text -> [Text]
T.lines Text
input
    Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Either String Int) -> Int -> Either String Int
forall a b. (a -> b) -> a -> b
$ [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [UArray Int Int
lut UArray Int Int -> Int -> Int
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Int
num | Int
num <- [Int]
nums]

day6a :: Text -> Either String Int
day6a :: Text -> Either String Int
day6a = UArray Int Int -> Text -> Either String Int
day6 [mkLUT| 80 |]

day6b :: Text -> Either String Int
day6b :: Text -> Either String Int
day6b = UArray Int Int -> Text -> Either String Int
day6 [mkLUT| 256 |]