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

<https://adventofcode.com/2022/day/4>

>>> :{
:main +
   "2-4,6-8\n\
   \2-3,4-5\n\
   \5-7,7-9\n\
   \2-8,3-7\n\
   \6-6,4-6\n\
   \2-6,4-8\n"
:}
2
4

-}
module Main where

import Data.Ix (inRange)

import Advent (format, countBy)

-- |
-- >>> :main
-- 584
-- 933
main :: IO ()
IO ()
main =
 do [(Int, Int, Int, Int)]
input <- [format|2022 4 (%u-%u,%u-%u%n)*|]
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ ((Int, Int, Int, Int) -> Bool) -> [(Int, Int, Int, Int)] -> Int
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Int
countBy (\(Int
a,Int
b,Int
c,Int
d) -> Int
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
c Bool -> Bool -> Bool
&& Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
b Bool -> Bool -> Bool
|| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
a Bool -> Bool -> Bool
&& Int
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
d) [(Int, Int, Int, Int)]
input
    Int -> IO ()
forall a. Show a => a -> IO ()
print (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ ((Int, Int, Int, Int) -> Bool) -> [(Int, Int, Int, Int)] -> Int
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Int
countBy (\(Int
a,Int
b,Int
c,Int
d) -> (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int
a,Int
b) Int
c Bool -> Bool -> Bool
|| (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int
a,Int
b) Int
d Bool -> Bool -> Bool
|| (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int
c,Int
d) Int
a Bool -> Bool -> Bool
|| (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int
c,Int
d) Int
b) [(Int, Int, Int, Int)]
input