/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/
//VERSION 3
//return statements again bizarrely do not stop execution.
//Also removed system.exit(0) and set flag to control code flow.
//This code takes logical concepts from Version1 and Version2


//In VERSION1 and VERSION2, accessing number1 and number2 with substring
//In this example  remainingPortionFirstNumber = Integer.toString(number1);
//so it is not sufficient to use int since it will limit length of the number.

//also this is more closer to the first principles used in addition
//the code is much more tider, better variable names
//So I will explain most logic at variable declaration.
//using same test casee.
//Most importantly, it is 100% recursion based.
//I had to force it back into consecutiveNines loop by configuring a flag here..
//I envisage I need to incorporate this into Version1 and Version2 to ensure
//it removes iteration... and also remove do while loop



public class Main
{
    //these variables manage the StopClock
    static long startTime;
    static long endTime;
    static boolean running;
    static long elapsed;
    
    
    static long lastDigitNumber; //this performs %10 on remainingPortion
    
    // this is the (initial number /10) to remove lastDigitNumber
    static long remainingPortion;
    
    static boolean finishedUniformMSD; //it has finished addition of the most MSD
    //common at same position on number1 and number2
    
    //once it has determined grandTotal (remainingPortion is empty)
    static boolean finishedExecution;

    //either 1 or 0 as in real world
    static long carryForward;
    
    //total of the digits
    static long total;
    
    //flag to identify if consecutive nines
    //it affects how the numbers are changed 9=>0 (carryForward=1)
    //or 9=>9 (no carryForward)
    //or 9=>10 (if MSD and carryForward=1)
    static boolean consecutiveNines=false;
   
    //remainingPortionFirstNumber or remainingPortionSecondNumber 
    //are changed to this variable
    //once either remainingPortionSecondNumber==0 
    //or remainingPortionFirstNumber==0
    //i.e it is processing the left non uniform MSD
    //For instance the   remainingPortionFirstNumber= 7 is assigned to number
    //76      number1
    //+8      number2
    static String number;
    
    static String grandTotal=""; 
    // as you would expect to see in equals
    //section when performing calculation
    //   556 
    //  +733
    //  ------
    //  1289
     // ------
     
    //it keeps backupTotal since it might have filled grandTotal
    //above such as   289 (taking lastDigitTotal=2)  
    //and expecting another.. But if there is not another number,
    //it has to store the backupTotaltotal = 12  to give 1289
    static String backupTotal;
   
    //likewise it stores backupGrandTotal since above grandTotal
    //would be 289 and if it realises 
    //remainingPortionFirstNumber and remainingPortionSecondNumber are 0
    //it would attempt grandTotal = total + grandTotal    = 12289
    //instead it has to re-instate backupGrandTotal = 89
    //perform grandTotal= String.valueOf(backupTotal) + backupGrandTotal  = 12 89
    static String backupGrandTotal;
   
    //it starts as Integer conversion from String number1/number2
    //progressively gets smaller
    static long remainingPortionFirstNumber;
    static long remainingPortionSecondNumber;
   
    //this will be %10 of their respective remainingPortionXXXX
    static long lastDigitFirstNumber;
    static long lastDigitSecondNumber;
    
    //total always two digits wide. This will get least MSD (total%10)
    static long lastDigitTotal;
   
   //logic generated via AI.
   //used for stop clock function
    public static void start() {
        Main.startTime = System.nanoTime();
        Main.running = true;
    }

    public static void stop() {
        Main.endTime = System.nanoTime();
        Main.running = false;
    }

    public static long getElapsedTime() {
      
        if (running) {
            elapsed = System.nanoTime() - startTime;
        } else {
            elapsed = endTime - startTime;
        }
        return elapsed;
    }

    public static double getElapsedTimeInSeconds() {
        return getElapsedTime() / 1_000_000_000.0;
    }     
         
   
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //extensive test cases.
        //All tested with flag finishedExecution in code
        //also used return statements (fail to function)
        //removed system.exit(0);
        
        //***PASS**** 100***
        String number2=   "39799";
        String number1=       "4";
        
        //***PASS**1764***
        //String number2="993";
        //String number1="771";
        
        //PASS*****160******
        //String number2 = "136";
        //String number1 =  "24";
        
        //PASS******846370032454542******
        //String number2 = "846369999999999";
        //String number1 =        "32454543";
        
        //PASS****82160****
        //String number2 = "81636";   
        //String number1 =   "524";
        
        //PASS******* - 400219***
        //String number2 = "399627";   
        //String number1 =    "592";
        
        //pass*** - 400019***
        //String number2 = "399947";   
        //String number1 =     "72";
        
        //PASS****4962*** 
        //String number2 =   "3970";   
        //String number1 =    "992";
        
        //PASS****562***
        //String number1 =   "370";   
        //String number2 =   "192";
        
        //PASS***1000***
        //String number2 =   "999";   
        //String number1 =     "1";u
        
        //PASS****1998***
        //String number1 =   "999";   
        //String number2 =   "999";
        
        //***PASS***108***
        //String number1 =   "99";   
        //String number2 =   "9";
    
         //***PASS***1400019***
         //String number2 = "1399947";   
         //String number1 =      "72";
         
        //***PASS****5210***
        //String number2 = "4666";
        //String number1 =  "544";
        
        //***PASS***711****
        //String number2 =  "652";
        //String number1 =   "59";
        
        //PASS****1390219 *****
        //String number2 = "1389627";   
        //String number1 =     "592";
        
        //PASS****
        //String number2 = "1384627";   
        //String number1 =     "592";
        
        //PASS***********22023****
        //String number1 = "21999";   
        //String number2 =     "24";
        
        //PASS****2*****
        //String number1 = "1";   
        //String number2 = "1";
        
        //PASS***10******
        //String number1 = "1";   
        //String number2 = "9";
        
        //PASS***95759******
        //String number1 = "95427";   
        //String number2 =   "332";
        
        //PASS*****9719****
        //String number1 = "9647";   
        //String number2 =   "72";
        
        //PASS****100119*****
        //String number1 = "99647";   
        //String number2 =   "472";
        
        //**PASS** 10009*****
        //String number1 ="9947"; 
        //String number2 = "62";
        
        //***PASS  22023***
        //String number1 = "21999";   
        //String number2 =     "24";
        
        //**PASS 108 ***
        //String number1 = "99";   
        //String number2 =  "9";
        
        //String number1 = "999999999999999999999999999999999999";
        //String number2 = "324235435345435324234324234324";
        
        //***PASS****
        //String number1 =   "32";   
        //String number2 =   "89";
    
        //***PASS**** 562
        //String number1 =   "370";   
        //String number2 =   "192";
        
        //***NOW PASS**1764***
        //String number2="993";
        //String number1="771";
        
        //PASS***** 1998***
        //String number1 =   "999";   
        //String number2 =   "999";
        
        //starts the clock
        start();
        System.out.println("****grand total ******:" + addition(number, number1, number2));
        
        //stops the clock
        stop();
        System.out.println("Elapsed time in seconds: " + getElapsedTimeInSeconds());
       
    }
   
    public static String addition (String number, String number1, String number2)
    {
        //gets the entire number into long variable
        remainingPortionFirstNumber=Long.valueOf(number1);
        remainingPortionSecondNumber=Long.valueOf(number2);
        
        //it assumes there are more
        //MSD available in remainingPortionSecondNumber
        //so remainingPortion gets value of remainingPortionSecondNumber
        //the generic variable number is also now value of number2 
        if (remainingPortionFirstNumber==0 && !consecutiveNines)
        {
            System.out.println("FORCE 1");
            //generic variables are set on remaining part of number2
               
            remainingPortion=remainingPortionSecondNumber;
            number=number2;   //it assigns the other number to give correct logic
            finishedUniformMSD = true;
        }
        
           //it assumes there are more
        //MSD available in remainingPortionFirstNumber
        //so remainingPortion gets value of remainingPortionFirstNumber
        //the generic variable number is also now value of number1
        if (remainingPortionSecondNumber==0 && !consecutiveNines)
            {
                System.out.println("FORCE ");
                remainingPortion=remainingPortionFirstNumber;
                number=number1;  //it assigns the other number to give correct logic
                finishedUniformMSD = true;
                System.out.println("WHAT ISP: " + remainingPortionFirstNumber);
                System.out.println(grandTotal);
            }
            //Note in practice both varies can be 0.
            //above if useful until situation arises (i.e from point finishedUniformMSD=true)
            //to remainingPortionFirstNumber && remainingPortionSecondNumber = 0 (last calculation)
            
            //in meantime it is focussing on getting to the finishedUniformMSD, hence code below.
            //it starts calculation   MSD<=LSD
            
            if (!finishedUniformMSD)
            {
                System.out.println("this is when the code flows");
                
                System.out.println(remainingPortionFirstNumber);
                System.out.println(remainingPortionSecondNumber);
           
                lastDigitFirstNumber = remainingPortionFirstNumber%10;
                System.out.println("This is last digit first number: " +lastDigitFirstNumber);
                
                lastDigitSecondNumber = remainingPortionSecondNumber%10;
                System.out.println("This is last digit first number: " +lastDigitSecondNumber);
                
                //adds digits naturally
                //this is my first addition. It is not violated anything in the challenge
                //Since this is only technique to perform calculation. Challenge is not asking
                //re-invent addition operator.
                total = lastDigitFirstNumber + lastDigitSecondNumber;
           
                System.out.println(lastDigitFirstNumber +  "+" + lastDigitSecondNumber + "="+total);
                System.out.println("GRAND SO FAR1: " + grandTotal);
                System.out.println(total);
                
                //moves across one digit like in real life addition.
                //equivalent to discarding last digit
                remainingPortionFirstNumber = remainingPortionFirstNumber/10;
                remainingPortionSecondNumber=remainingPortionSecondNumber/10;
                
                //ie if total+carryForward is 10-18 and not finishedExecution
                if (total+carryForward>=10 && !finishedExecution)
                {
                    System.out.println("OVER");
                    System.out.println("WHAT IS LAST DIGIT TOTAL: " + lastDigitTotal);
                    
                    //keeps backup as explained in variable declaration area
                    backupTotal=Long.toString(total+carryForward);
                    backupGrandTotal = grandTotal;
                    
                    //stores the total + carryForward
                    //note for first execution, carryForward it inherits is 0
                    total = total + carryForward;
                    
                    //as real life, it uses the 2nd digit to store in grandTotal
                    lastDigitTotal = total%10;
                    
                    //it is lastDigitTotal stored as MSD each time
                    grandTotal = lastDigitTotal + grandTotal;
                    
                    //carryForward set since total = 10+
                    carryForward=1;
               
                    System.out.println("***GRAND SO FAR: " + grandTotal);
                    System.out.println("backup GRAND SO FAR: " + backupGrandTotal);
                    System.out.println("backup total SO FAR: " + backupTotal);
                    
                    //recursive call again.
                    //Note variable number is not in any other logic until
                    //finishedUniformMSD=true;
                    
                    addition(number, Long.toString(remainingPortionFirstNumber), Long.toString(remainingPortionSecondNumber));
                }  //end of if total 10-18
                
                //total + carryForward <10
                if (total+carryForward<10 && !finishedExecution)
                {
                    System.out.println("UNDER");
                    
                    backupTotal=Long.toString(total+carryForward);
                    backupGrandTotal = grandTotal;
                    total= total + carryForward;
                    
                    lastDigitTotal = total%10;
                    System.out.println("WHAT IS TOTAL:");
                    grandTotal = lastDigitTotal + grandTotal;
                    carryForward=0;
               
                    System.out.println("GRAND SO FAR: " + grandTotal);
                    System.out.println("backup GRAND SO FAR: " + backupGrandTotal);
                    System.out.println("backup total SO FAR: " + backupTotal);
               
                    addition(number, Long.toString(remainingPortionFirstNumber), Long.toString(remainingPortionSecondNumber));
                }  //end if total +carryForward<10
                
            }  //end of if (!finishedUniformMSD)
            
            //System.out.println("NOT HERE1");
            
            //means both numbers were uniform length and reached MSD
            //it has to rely on the backup variables.
            //see explanation in declaration
            if (remainingPortionFirstNumber==0 && remainingPortionSecondNumber==0)
            {
                System.out.println("backuptotal: " + backupTotal);
                System.out.println("backupGrandTotal: " + backupGrandTotal);
                System.out.println("grandTotal: " + grandTotal);
           
                grandTotal= String.valueOf(backupTotal) + backupGrandTotal;
           
                System.out.println("uniform numbers finished execution: grandtotal: " + grandTotal);
                finishedExecution=true;
                //System.exit(0);
                return grandTotal;
                
            }
            
            //this can be only option when one of the numbers most MSD is not equal to 0
            //hence it completes extra logic
            
            //just to clarify if it reached grandTotal below    412
                                          //                   + 26
                                          //                  ------
                                          //                     38
                                          //                  ------
            //the remainingPortionSecondNumber is still 41/10  = 4
            //whereas remainingPortionFirstNumber is  2%10 = 0
            //hence loop below is applicable
            
            else
            {
                System.out.println("NOT HERE2");
                
                if (!finishedExecution)
                {
                    //this deals with the non-uniform aspect, until then, it continues normally.
                    //this loop might be an overhead to performance, it can be removed potentially
                    //since we know that the flag above finishedExecution would be true if
                    //below remainingPortionFirstNumber && remainingPortionSecondNumber were 0
                    if (remainingPortionFirstNumber%10 == 0  || remainingPortionSecondNumber==0)
                    {
                        //rest processing of interest is from number, which obtained value above.
                        remainingPortion = Long.valueOf(number);
                        lastDigitNumber = remainingPortion%10;
       
                        System.out.println("remain:"+ remainingPortion);
                        System.out.println("last: " + lastDigitNumber);
                        System.out.println(carryForward);
                        System.out.println("GT:" + grandTotal);
                        
                        //It needs to handle this scenario differently 
                        if (consecutiveNines)
                        {
                            System.out.println("TIME FOR EXTRA LOGIC");
            System.out.println("CURRERRN grand: " + grandTotal);
            System.out.println("left remaining portion: " + remainingPortion);
            System.out.println(grandTotal);
           
            if (remainingPortion/10==0)
            {
                System.out.println("single nine left");
                //this represents only a single nine left
                //in that case it would take totanotl of it with carry forward
                total = lastDigitNumber + carryForward;
                grandTotal = Long.toString(total)+ grandTotal;
                System.out.println("IJN: " + grandTotal);
                //System.exit(0);
                finishedExecution=true;
                return grandTotal;
            }
           
           
           
            lastDigitNumber = remainingPortion%10;
           
           
           
                if (lastDigitNumber==9)
                {
                    System.out.println("GETS JH");
                total = lastDigitNumber + carryForward;
                lastDigitNumber=total%10;
                grandTotal = lastDigitNumber + grandTotal;
                remainingPortion=remainingPortion/10;
                consecutiveNines=true;
                addition(Long.toString(remainingPortion), number1, number2);
                }
               
                //end of the 9s
                else
                {
                    total = lastDigitNumber + carryForward;
                    grandTotal = total + grandTotal;
                    //shrink value
                    remainingPortion=remainingPortion/10;
                    grandTotal = remainingPortion + grandTotal;
                    System.out.println("finished1");
                    System.out.println(grandTotal);
                    //System.exit(0);
                    finishedExecution=true;
                    return grandTotal;
                   
                }
               
            }
       
        //easiest - done
                                     //so  784308  
                                     //    +   51   = 784359
       
        if (lastDigitNumber+carryForward<10 && !finishedExecution)     //lastdigit = 3
        {
            System.out.println("ldn: " + lastDigitNumber);
            total = lastDigitNumber + carryForward;
            grandTotal = Long.toString(total) + grandTotal;
            System.out.println("grad: " + grandTotal);
           
           
            remainingPortion=remainingPortion/10;
            System.out.println("current remaining check: " + remainingPortion);
           
            if (remainingPortion!=0)
            {
                grandTotal=remainingPortion+grandTotal;
                System.out.println("finished2");
                finishedExecution=true;
                //there is still MSD left
            }
                System.out.println(grandTotal);
                finishedExecution=true;
                //System.exit(0);
                return grandTotal;
        }
       
                //easiest - done
                                     //so  784948  
                                     //    +   71   = 785 019
       
       
         if (lastDigitNumber+carryForward>9 && !finishedExecution)     //lastdigit = 9
        {
                //checks next highest MSD for another 9
                if (remainingPortion%10==9)
                {
                    System.out.println("consecutive nine");
                   
                    //it leaves remainingportion intact
                    //remainingPortion=remainingPortion/10;
                    //but sets flag to handle the situation better.
                    consecutiveNines=true;
                    System.out.println("current grand: " + grandTotal);
                   
                    addition(Long.toString(remainingPortion), number1, number2);
                   
                }
           
            if (lastDigitNumber!=9 && !finishedExecution)
            {
                System.out.println("DDD");
                total = lastDigitNumber + 1;   //carry 1 over
               
                grandTotal = total + grandTotal;
                System.out.println("CURRENT GRAND: " + grandTotal);
               
                //since it has dealt with lastNumber remainingPortion has to remove this
               
                remainingPortion=remainingPortion/10;   //now 78
                System.out.println(remainingPortion);
               
                grandTotal = remainingPortion + grandTotal;
               
                System.out.println("NEW: " + grandTotal);
                 System.out.println("finished3");
            System.out.println(grandTotal);
                //System.exit(0);
                    return grandTotal;
            }
           
                                     //so 98499 948  
                                      //    +    71   = 9 8500 019
           
           if (!finishedExecution)
           {
            System.out.println("curent ggrand: " + grandTotal);
            System.out.println("current remain: " + remainingPortion);
           
            grandTotal=remainingPortion+grandTotal;
            return grandTotal;
           }
        }
       
    }
   
        }
    }  //end of else
   
    return grandTotal;
   
}
   
   
}


