{-# LANGUAGE NondecreasingIndentation, ParallelListComp, TypeApplications #-}
module Day1 (day1a, day1b) where
import Common (readEntire)
import Data.Text (Text)
import qualified Data.Text as T (lines)
import qualified Data.Text.Read as T (decimal)
day1a :: Text -> Either String Int
day1a :: Text -> Either String Int
day1a 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 @Int Reader Int
forall a. Integral a => Reader a
T.decimal) (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
$ [Bool] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Bool] -> Int) -> [Bool] -> Int
forall a b. (a -> b) -> a -> b
$ (Bool -> Bool) -> [Bool] -> [Bool]
forall a. (a -> Bool) -> [a] -> [a]
filter Bool -> Bool
forall a. a -> a
id ([Bool] -> [Bool]) -> [Bool] -> [Bool]
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool) -> [Int] -> [Int] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<) [Int]
nums ([Int] -> [Bool]) -> [Int] -> [Bool]
forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
1 [Int]
nums
day1b :: Text -> Either String Int
day1b :: Text -> Either String Int
day1b 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 @Int Reader Int
forall a. Integral a => Reader a
T.decimal) (Text -> [Text]
T.lines Text
input)
let sums :: [Int]
sums = [Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
z | Int
x <- [Int]
nums | Int
y <- Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
1 [Int]
nums | Int
z <- Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
2 [Int]
nums]
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
$ [Bool] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Bool] -> Int) -> [Bool] -> Int
forall a b. (a -> b) -> a -> b
$ (Bool -> Bool) -> [Bool] -> [Bool]
forall a. (a -> Bool) -> [a] -> [a]
filter Bool -> Bool
forall a. a -> a
id ([Bool] -> [Bool]) -> [Bool] -> [Bool]
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool) -> [Int] -> [Int] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<) [Int]
sums ([Int] -> [Bool]) -> [Int] -> [Bool]
forall a b. (a -> b) -> a -> b
$ Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
1 [Int]
sums