{-# Language UnboxedTuples #-}
{-|
Module      : Advent.Chinese
Description : Chinese-remainder theorem
Copyright   : 2021 Eric Mertens, 2011 Daniel Fischer, 2016-2017 Andrew Lelechenko, Carter Schonwald, Google Inc.
License     : ISC
Maintainer  : emertens@gmail.com

<https://en.wikipedia.org/wiki/Chinese_remainder_theorem>

-}
module Advent.Chinese (Mod(..), toMod, chinese) where

import Control.Monad (foldM)
import GHC.Num.Integer (integerGcde)

-- | A package of a residue and modulus. To 'toMod' when constructing
-- to ensure the reduced invariant is maintained.
data Mod = Mod { Mod -> Integer
residue, Mod -> Integer
modulus :: !Integer }
  deriving (Mod -> Mod -> Bool
(Mod -> Mod -> Bool) -> (Mod -> Mod -> Bool) -> Eq Mod
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Mod -> Mod -> Bool
$c/= :: Mod -> Mod -> Bool
== :: Mod -> Mod -> Bool
$c== :: Mod -> Mod -> Bool
Eq, ReadPrec [Mod]
ReadPrec Mod
Int -> ReadS Mod
ReadS [Mod]
(Int -> ReadS Mod)
-> ReadS [Mod] -> ReadPrec Mod -> ReadPrec [Mod] -> Read Mod
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Mod]
$creadListPrec :: ReadPrec [Mod]
readPrec :: ReadPrec Mod
$creadPrec :: ReadPrec Mod
readList :: ReadS [Mod]
$creadList :: ReadS [Mod]
readsPrec :: Int -> ReadS Mod
$creadsPrec :: Int -> ReadS Mod
Read, Int -> Mod -> ShowS
[Mod] -> ShowS
Mod -> String
(Int -> Mod -> ShowS)
-> (Mod -> String) -> ([Mod] -> ShowS) -> Show Mod
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Mod] -> ShowS
$cshowList :: [Mod] -> ShowS
show :: Mod -> String
$cshow :: Mod -> String
showsPrec :: Int -> Mod -> ShowS
$cshowsPrec :: Int -> Mod -> ShowS
Show)

-- | Construct an element of 'Mod' with a given value and modulus.
-- Modulus must be greater than zero.
toMod ::
  Integer {- ^ integer -} ->
  Integer {- ^ modulus -} ->
  Mod     {- ^ residue mod modulus -}
toMod :: Integer -> Integer -> Mod
toMod Integer
r Integer
m
  | Integer
m Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0     = Integer -> Integer -> Mod
Mod (Integer
r Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m) Integer
m -- needs to be `mod` to handle negative values
  | Bool
otherwise = String -> Mod
forall a. HasCallStack => String -> a
error (String
"toMod: invalid modulus " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
m)

chinese' :: Mod -> Mod -> Maybe Mod
chinese' :: Mod -> Mod -> Maybe Mod
chinese' (Mod Integer
n1 Integer
m1) (Mod Integer
n2 Integer
m2)
  | Integer
d Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1
  = Mod -> Maybe Mod
forall a. a -> Maybe a
Just (Mod -> Maybe Mod) -> Mod -> Maybe Mod
forall a b. (a -> b) -> a -> b
$! Integer -> Integer -> Mod
toMod (Integer
m2          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
v Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
m1          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
u) (Integer
m1          Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
m2)
  | (Integer
n1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
n2) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`rem` Integer
d Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
  = Mod -> Maybe Mod
forall a. a -> Maybe a
Just (Mod -> Maybe Mod) -> Mod -> Maybe Mod
forall a b. (a -> b) -> a -> b
$! Integer -> Integer -> Mod
toMod (Integer
m2 Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
v Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
m1 Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
n2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
u) (Integer
m1 Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
m2)
  | Bool
otherwise = Maybe Mod
forall a. Maybe a
Nothing
  where
    (Integer
d, Integer
u, Integer
v) = Integer -> Integer -> (Integer, Integer, Integer)
integerGcde Integer
m1 Integer
m2

-- | Find an integer that is equal to all the given numbers individually
-- considering the modulus of those numbers.
--
-- Example: If @x = 2 (mod 3) = 3 (mod 5) = 2 (mod 7)@ then @x = 23@
-- 
-- >>> chinese [toMod 2 3, toMod 3 5, toMod 2 7]
-- Just 23
chinese :: [Mod] -> Maybe Integer
chinese :: [Mod] -> Maybe Integer
chinese []     = Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
0
chinese (Mod
x:[Mod]
xs) = Mod -> Integer
residue (Mod -> Integer) -> Maybe Mod -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Mod -> Mod -> Maybe Mod) -> Mod -> [Mod] -> Maybe Mod
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Mod -> Mod -> Maybe Mod
chinese' Mod
x [Mod]
xs

{-
Implementation of 'chinese' adapted from arithmoi-0.11.0.1 under terms of the following license.

Copyright (c) 2011 Daniel Fischer, 2016-2017 Andrew Lelechenko, Carter Schonwald, Google Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-}