{-# Language QuasiQuotes, ParallelListComp #-}
{-|
Module      : Main
Description : Day 1 solution
Copyright   : (c) Eric Mertens, 2021
License     : ISC
Maintainer  : emertens@gmail.com

<https://adventofcode.com/2021/day/1>

Count the number of increasing pairs of measurements.

-}
module Main where

import Advent (countBy, format)

-- | >>> :main
-- 1681
-- 1704
main :: IO ()
IO ()
main =
 do [Int]
input <- [format|2021 1 (%u%n)*|]
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Int] -> Int
solve Int
1 [Int]
input)
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> [Int] -> Int
solve Int
3 [Int]
input)

-- | >>> solve 1 [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
-- 7
--
-- >>> solve 3 [199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
-- 5
solve ::
  Int {- ^ window size -} ->
  [Int] {- ^ measurements -} ->
  Int {- ^ count of ascending pairs -}
solve :: Int -> [Int] -> Int
solve Int
n [Int]
input = (Bool -> Bool) -> [Bool] -> Int
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Int
countBy Bool -> Bool
forall a. a -> a
id [Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
y | Int
x <- [Int]
input | Int
y <- Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
n [Int]
input]