{-# Language ImportQualifiedPost #-}
module Main (main) where
import Advent (counts, countBy, getInputLines)
import Data.List (tails)
import Data.Map (Map)
import Data.Map qualified as Map
main :: IO ()
IO ()
main =
do [String]
inp <- Int -> Int -> IO [String]
getInputLines Int
2018 Int
2
Int -> IO ()
forall a. Show a => a -> IO ()
print ([String] -> Int
forall a. Ord a => [[a]] -> Int
part1 [String]
inp)
String -> IO ()
putStrLn ([String] -> String
forall a. Eq a => [[a]] -> [a]
part2 [String]
inp)
part1 :: Ord a => [[a]] -> Int
part1 :: forall a. Ord a => [[a]] -> Int
part1 [[a]]
inp = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product ((Int -> Int) -> [Int] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Int
exact [Int
2,Int
3])
where
cards :: [Map a Int]
cards = ([a] -> Map a Int) -> [[a]] -> [Map a Int]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> Map a Int
forall (f :: * -> *) a. (Foldable f, Ord a) => f a -> Map a Int
counts [[a]]
inp
exact :: Int -> Int
exact Int
n = (Map a Int -> Bool) -> [Map a Int] -> Int
forall (f :: * -> *) a. Foldable f => (a -> Bool) -> f a -> Int
countBy (Int -> Map a Int -> Bool
forall a. Eq a => a -> Map a a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Int
n) [Map a Int]
cards
part2 :: Eq a => [[a]] -> [a]
part2 :: forall a. Eq a => [[a]] -> [a]
part2 [[a]]
inp = [[a]] -> [a]
forall a. HasCallStack => [a] -> a
head [[a]
r | [a]
x:[[a]]
xs <- [[a]] -> [[[a]]]
forall a. [a] -> [[a]]
tails [[a]]
inp, [a]
y <- [[a]]
xs, Just [a]
r <- [[a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
offbyone [a]
x [a]
y]]
offbyone :: Eq a => [a] -> [a] -> Maybe [a]
offbyone :: forall a. Eq a => [a] -> [a] -> Maybe [a]
offbyone (a
x:[a]
xs) (a
y:[a]
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [a]) -> Maybe [a] -> Maybe [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [a] -> Maybe [a]
forall a. Eq a => [a] -> [a] -> Maybe [a]
offbyone [a]
xs [a]
ys
| [a]
xs [a] -> [a] -> Bool
forall a. Eq a => a -> a -> Bool
== [a]
ys = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
xs
offbyone [a]
_ [a]
_ = Maybe [a]
forall a. Maybe a
Nothing