'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback

import numpy as np
import sympy as sp

from sympy import (symbols, Symbol, pi, latex, sympify,
                   Rational, Derivative, simplify,
                   UnevaluatedExpr as ueval
                   )
from xv.km.util import latex_fraction, wrap_in_latex
from xv.util import number_to_nth, list_to_str


from  xv.util import UtilClass

# _combinatorics_helper
# CombinatoricsHelper


class CombinatoricsHelper(UtilClass):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
    def get_problem_most_difficult(self, **kwargs):
        '''
        '''
        sInfo = f"""Processing {self.__class__.__name__}.{inspect.stack()[0][3]}: """
        self._log(sInfo.strip())

        index = kwargs.get('index', None)
        
        try:
            
            if index is None:
                random_choices = list(range(1, 6))
        
                identifier = f"{self.__class__.__name__}{id(self)}.{inspect.stack()[0][3]}.random_choices"
                random_choice = self.get_no_repeat_random_choice(identifier, random_choices)
            else:
                random_choice = index
            
            if random_choice == 1:
                return self._problem_airlines_seat_of_couples(**kwargs)
            
    
            return self._problem_airlines_seat_of_couples(**kwargs)
        
        except Exception as e:
            sError = f"""Error {self.__class__.__name__}.{inspect.stack()[0][3]}: {e}
            """
            self._log(sError)
            self._log(traceback.format_exc())
            pass
        
        return None
        pass

    def _problem_airlines_seat_of_couples(self, **kwargs):
        
        prob_s = f"""
        A small airplane has $4$ rows of seats with $3$ seats in each row. 
        Eight passengers have boarded the plane and are distributed randomly 
        among the seats. A married couple is next to board. 
        What is the probability there will be 2 adjacent 
        seats in the same row for the couple? 
        <br/>
        Ref: <a 
        target = "_blank"
        href="https://artofproblemsolving.com/wiki/index.php/2024_AMC_8_Problems/Problem_25"
        >AOPS 2024</>
        """
        prob_s = prob_s.replace("\n", " ").strip()
        
        soln_stmts = []
        
        soln_stmt = f"""
        Let us look how the couple cannot have adjacent seats. In a row,
        1. when someone is already is the middle seat, or
        2. when two people occupy a row.

        Now we can us this to count all possible probabilities with 8 people in the plain:
        """
        soln_stmts.append(soln_stmt.strip())
        
        v1 = 70
        
        soln_stmt = f"""
        1. Middle seat in 4 rows are occupied .
        
        $\\displaystyle 
        \\binom{{ \\text{{ 4 rows }}  }}{{ \\text{{ 4 rows}}  }}
        \\cdot  
        \\binom{{ \\text{{ selected 4 rows }}  }}{{ \\text{{ occupy 4 middle sheets }}  }}
        \\cdot  
        \\binom{{ \\text{{ 0 non-middle seats of leftover 1 row }}   }}{{ \\text{{ occupy 0 seats }}  }}  
        \\cdot  
        \\binom{{ \\text{{ 8 non-middle seats from selected 4 rows }}   }}
               {{ \\text{{ occupy 4 seats }}  }}  
 
        \\\\ ~ \\\\
        \\displaystyle 
        = 
        \\binom{{ 4  }}{{ 4 }}  
        \\cdot 
        \\binom{{ 4  }}{{ 4 }}  
        \\cdot 
        \\binom{{ 0  }}{{ 0 }}  
        \\cdot 
        \\binom{{ 8  }}{{ 4 }}  
        =
        1 
        \\cdot 
        1
        \\cdot 
        1
        \\cdot 
        \\frac{{ 8 \\cdot 7 \\cdot 6 \\cdot 5  }}{{ 4 \\cdot 3 \\cdot 2 \\cdot 1 }}  
        
        = {v1}
        $
        """
        soln_stmts.append(soln_stmt.strip())


        v2 = 80
        soln_stmt = f"""
        2. Middle seat in 3 rows and 2 non-middle seats in the leftover row are occupied .
        
        $\\displaystyle 
        \\binom{{ \\text{{ 4 rows }}  }}{{ \\text{{ 3 rows}}  }}
        \\cdot  
        \\binom{{ \\text{{ selected 3 rows }}  }}{{ \\text{{ occupy 3 middle sheets }}  }}
        \\cdot  
        \\binom{{ \\text{{ 2 non-middle seats of leftover 1 row }}   }}{{ \\text{{ occupy 2 seats }}  }}  
        \\cdot  
        \\binom{{ \\text{{ 6 non-middle seats from selected 3 rows }}   }}
               {{ \\text{{ occupy 3 seats }}  }}  
 
        \\\\ ~ \\\\
        \\displaystyle 
        = 
        \\binom{{ 4  }}{{ 3 }}  
        \\cdot 
        \\binom{{ 3  }}{{ 3 }}  
        \\cdot 
        \\binom{{ 2  }}{{ 2 }}  
        \\cdot 
        \\binom{{ 6  }}{{ 3 }}  
        =
        \\frac{{ 4  }}{{ 1 }}  
        \\cdot 
        1
        \\cdot 
        \\frac{{ 6 \\cdot 5 \\cdot 4  }}{{ 3 \\cdot 2 \\cdot 1 }}  
        
        = {v2}
        $
        """
        soln_stmts.append(soln_stmt.strip())

        
        v3 = 36
        soln_stmt = f"""
        3. Middle seat in 2 rows and 4 non-middle seats in the leftover rows are occupied .
        
        $\\displaystyle 
        \\binom{{ \\text{{ 4 rows }}  }}{{ \\text{{ 2 rows}}  }}
        \\cdot  
        \\binom{{ \\text{{ selected 2 rows }}  }}{{ \\text{{ occupy 2 middle sheets }}  }}
        \\cdot  
        \\binom{{ \\text{{ 4 non-middle seats of leftover 2 rows }}   }}{{ \\text{{ occupy 4 seats }}  }}  
        \\cdot  
        \\binom{{ \\text{{ 4 non-middle seats from selected 2 rows }}   }}
               {{ \\text{{ occupy 2 seats }}  }}  
 
        \\\\ ~ \\\\
        \\displaystyle 
        = 
        \\binom{{ 4  }}{{ 2 }}  
        \\cdot 
        \\binom{{ 2  }}{{ 2 }}  
        \\cdot 
        \\binom{{ 4  }}{{ 4 }}  
        \\cdot 
        \\binom{{ 4  }}{{ 2 }}  
        =
        \\frac{{ 4 \\cdot 3 }}{{ 2 \\cdot 1 }}  
        \\cdot 
        1
        \\cdot 
        1
        \\cdot 
        \\frac{{ 4 \\cdot 3 }}{{ 2 \\cdot 1 }}  
        
        = {v3}
        $
        """
        soln_stmts.append(soln_stmt.strip())


        v4 = 8
        soln_stmt = f"""
        4. Middle seat in 1 row and 6 non-middle seats in the leftover rows are occupied .
        
        $\\displaystyle 
        \\binom{{ \\text{{ 4 rows }}  }}{{ \\text{{ 1 rows}}  }}
        \\cdot  
        \\binom{{ \\text{{ selected 1 rows }}  }}{{ \\text{{ occupy 1 middle sheets }}  }}
        \\cdot  
        \\binom{{ \\text{{ 6 non-middle seats of leftover 3 rows }}   }}{{ \\text{{ occupy 6 seats }}  }}  
        \\cdot  
        \\binom{{ \\text{{ 2 non-middle seats from selected 1 row }}   }}
               {{ \\text{{ occupy 1 seats }}  }}  
 
        \\\\ ~ \\\\
        \\displaystyle 
        = 
        \\binom{{ 4  }}{{ 1 }}  
        \\cdot 
        \\binom{{ 1  }}{{ 1 }}  
        \\cdot 
        \\binom{{ 6  }}{{ 6 }}  
        \\cdot 
        \\binom{{ 2  }}{{ 1 }}  
        =
        \\frac{{ 4 }}{{ 1 }}  
        \\cdot 
        1
        \\cdot 
        1
        \\cdot 
        \\frac{{ 2 }}{{ 1 }}  
        
        = {v4}
        $
        """
        soln_stmts.append(soln_stmt.strip())

        v5 = 1
        soln_stmt = f"""
        5. Middle seat in 0 rows and 8 non-middle seats in the leftover row are occupied .
        
        $\\displaystyle 
        \\binom{{ \\text{{ 4 rows }}  }}{{ \\text{{ 0 rows}}  }}
        \\cdot  
        \\binom{{ \\text{{ selected 0 rows }}  }}{{ \\text{{ occupy 0 middle sheets }}  }}
        \\cdot  
        \\binom{{ \\text{{ 8 non-middle seats of leftover 4 rows }}   }}{{ \\text{{ occupy 8 seats }}  }}  
        \\cdot  
        \\binom{{ \\text{{ 0 non-middle seats from selected 0 row }}   }}
               {{ \\text{{ occupy 0 seats }}  }}  
 
        \\\\ ~ \\\\
        \\displaystyle 
        = 
        \\binom{{ 4  }}{{ 0 }}  
        \\cdot 
        \\binom{{ 0  }}{{ 0 }}  
        \\cdot 
        \\binom{{ 8 }}{{ 8 }}  
        \\cdot 
        \\binom{{ 0  }}{{ 0 }}  
        =
        1
        \\cdot 
        1
        \\cdot 
        1
        \\cdot 
        1  
        
        = {v5}
        $
        """
        soln_stmts.append(soln_stmt.strip())

        v_desired = v1 + v2 + v3 + v4 + v5
        soln_stmt = f"""
        Total number of ways as mentioned above when the couple cannot sit in adjacent seats
        
        $\\displaystyle 
        = {v1} + {v2} + {v3} + {v4} + {v5} 
        
        = {v_desired} 
        $
        """
        soln_stmts.append(soln_stmt.strip())

        import math 
        v_total =  math.comb(12, 8)
        
        soln_stmt = f"""
        The number of ways 12 seats can be occupied by 8 people  $\\displaystyle 
        =
        \\binom{{ 12 }}{{ 8 }}  
        = {v_total}
        $
        """
        soln_stmts.append(soln_stmt.strip())


        
        v_probability_not = Rational(v_desired, v_total)
        soln_stmt = f"""
        Probability of the couple cannot sit in adjacent seats $\\displaystyle 
        =
        \\frac{{ {v_desired} }}{{ {v_total} }}  
        =
        = {latex(v_probability_not)}
        $
        """
        soln_stmts.append(soln_stmt.strip())

        v_probability = 1 - Rational(v_desired, v_total)
        soln_stmt = f"""
        Probability of the couple sitting in adjacent seats $\\displaystyle 
        = 1 - {latex(v_probability_not)}
        = {latex(v_probability)}
        $
        """
        soln_stmts.append(soln_stmt.strip())

        answer_s = f"""
        $\\displaystyle {latex(v_probability)} $
        """
        
        soln_s = "\n\n".join(soln_stmts)
        
                
        result = {
            'prob_s': prob_s.replace("\n", " ").strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        
        return result
        pass



    pass
#class CombinatoricsHelper:
