/*
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
//but written from scratch.
//But much easier to the eye, but also seems just as long!

//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
//it can be seen henceforth I reduced the final test points
//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*** via final test 0,1,3,4,7,8,9,10,11,17,18,21
        //String number2="99";
        //String number1= "1";
        
        //***PASS**1764*** via final test 0,1,2,3,4,5,6
        //String number2="993";
        //String number1="771";
        
        //***PASS**1764*** via final test via final test 0,1,2,3,4,5,6
        //String number2="243";
        //String number1="541";
        
        //PASS*****160****** - via final test 0,1,3,4,5,7,8,9,14,21
        //String number2 = "136";
        //String number1 =  "24";
        
        //PASS******846370032454542****** via final test 0,3,4,7,8,9,10,12,13,17,18,21
        //String number2 = "846369999999999";
        //String number1 =        "32454543";
        
        //PASS****82160**** via final test 0,1,3,4,5,7,8,9,14,15,21
        //String number2 = "81636";   
        //String number1 =   "524";
        
        //PASS******* - 400219  via final test 0,1,3,4,5,7,8,9,10,11,12,17,18,21
        //String number2 = "399627";   
        //String number1 =    "592";
        
        //pass********* - 400019 via final test 0,1,3,4,5,7,8,9,10,11,12,17,18,21,
        //String number2 = "399947";   
        //String number1 =     "72";
        
        //PASS***4962****** getting via final test 0,1,3,4,5,7,8,9,14,21
        //String number2 =   "3970";   
        //String number1 =    "992";
        
        //PASS******  562  via final test 0,1,2,3,4,5,6,
        //String number1 =   "370";   
        //String number2 =   "192";
        
        //PASS**** - 1000  via final test 0,1,4,7,8,9,10,11,12,21
        //String number2 =   "999";   
        //String number1 =     "1";
        
        //PASS*****  1998 via final test 
        //String number1 =   "999";   
        //String number2 =   "999";
        
        //PASS ******* 1098 via final test 0,2,3,4,7,8,9,10,11,17,18,21
        //String number1 =   "999";   
        //String number2 =    "99";
        
        //***PASS***108 via final test 0,2,3,4,7,8,9,10,11,18,19,21
        //String number1 =   "99";   
        //String number2 =   "9";
    
         //***PASS**** 1400019  via final test 0,1,3,4,5,7,8,9,10,12,13,21
         //String number2 = "1399947";   
         //String number1 =      "72";
         
        //***PASS****5210*** via final test 0,1,3,4,7,8,9,14,21, 
        //String number2 = "4666";
        //String number1 =  "544";
        
        //***PASS***711**** via final test 0,1,3,4,7,8,9,14,21 
        //String number2 =  "652";
        //String number1 =   "59";
        
        //PASS****1390219 ***** via final test 0,3,4,5,7,8,9,10,12,13,17,18,21
        //String number2 = "1389627";   
        //String number1 =     "592";        

        //PASS***********22023**** via final test 0,2,3,4,7,8,9,10,12,13,17,18,21
        //String number1 = "21999";   
        //String number2 =     "24";
        
        //PASS****2*******  via final test 0,1,2,3,5,6
        //String number1 = "1";   
        //String number2 = "1";
        
        //PASS***10********  via final test 0,1,2,4,6,
        //String number1 = "1";   
        //String number2 = "9";
        
        //PASS***95759******** via final test 0,2,3,5
        //String number1 = "95427";   
        //String number2 =   "332";
        
        //PASS*****9719******   via final test 0,2,3,4,5,7,8,9,14,15,21
        //String number1 = "9647";   
        //String number2 =   "72";
        
        //PASS****100119*******   via  final test 0,2,3,4,5,7,8,9,10,11,12,17,18,21
        //String number1 = "99647";   
        //String number2 =   "472";
        
        //**PASS** 10009 via  final test 0,2,3,4,5,7,8,9,10,11,12,17,18,21
        //String number1 ="9947"; 
        //String number2 = "62";
        
        //PASS via  final test 0,2,3,4,7,8,9,10,12,13,17,18,21,
        //***PASS  22023
        //String number1 = "21999";   
        //String number2 =     "24";
        
         //***PASS***108 via final test 0,2,3,4,7,8,9,10,11,18,19,21
        //String number1 = "99";   
        //String number2 =  "9";
        
        //PASS getting 1000  via final test 0,2,3,4,7,8,9,10,11,12,17,18,21
        //String number1 = "999";
        //String number2 = "1";
        
        //PASS via final test 0,2,3,5,7,8,9,14,15,21
        //String number1 = "888";
        //String number2 = "1";
        
        //FAIL NumberFormatException  remainingPortionFirstNumber=Long.valueOf(number1);
        //String number1 = "99999999999999999999999999999999999999999999999999999999";
        //String number2 = "32423543534543532423432423432494355443534532235324324333";
       
       //shortened the numbers same outcome 
        //FAIL NumberFormatException  remainingPortionFirstNumber=Long.valueOf(number1);
        //String number1 = "99999999999999999999999999999999999";
        //String number2 = "32423543534543532";
        
        //shortened the numbers same outcome 
        //FAIL NumberFormatException  remainingPortionFirstNumber=Long.valueOf(number1);
        //String number1 = "321326263232722172323121232212212212";
        //String number2 = "32423543534543531";
        
        //shortened the numbers, any longer than this and program will render 
        //FAIL NumberFormatException  remainingPortionFirstNumber=Long.valueOf(number1);
        //***PASS*** 1845561698584545666  via final test 0,1,2,3,4,5,6
        //String number1 = "921326263239897898";
        //String number2 = "924235435344647768";
        
        //**************INTRODUCING FEW EXTRA TEST CASES
        
        //***PASS   1000000  via final test 0,2,3,4,7,8,9,10,11,12,17,18,21
        //String number1 = "999999";
        //String number2 = "1";
        
        //****PASS  10001424201998***  via final test 0,2,3,4,5,7,8,9,11,12,17,18,21
        //String number1 = "9999990767676";
        //String number2 = "1433434322";
        
        //****PASS  giving 9999990767677***  NOW FIXED via final test 0,2,3,5,7,8,9,14,15,21
        //similar issues below previously
        //String number1 = "9999990767676";
        //String number2 = "1";
        
        //****PASS  giving 7899990767677***  NOW FIXED via final test 0,2,3,5,7,8,9,14,15,21
        //String number1 = "7899990767676";
        //String number2 = "1";
        //hence issue with transition from non-9s to 9s
        //so I am getting the code to print GrandTotal
        //at all the final test locations in which
        //it modifies the grandTotal
        //it is bizarre that it has not shown system.out.println() other
        //than grandTotal=7
        
        //PASS  ***78990767677****   via final test 0,2,3,5,7,8,9,14,15,21
        //String number1 = "78990767676";
        //String number2 = "1";
        
        //****PASS  giving 97653*** via final test 0,2,5,7,8,9,14,15,21
        String number1 = "97652";
        String number2 = "1";
        
        //starts the clock
        start();
        System.out.println(number1 + "+" + number2 + "= " + addition(number, number1, number2));
        System.out.println(number1 + "+" + number2 + "= " + grandTotal);
        
        //stops the clock
        stop();
        System.out.println("Elapsed time in seconds: " + getElapsedTimeInSeconds());
       
    }
   
    public static String addition (String number, String number1, String number2)
    {
        System.out.println("final test 0");
        //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("final test 1");
            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("final test 2");
            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("final test 3");
            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("final test 4");
                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("final test 5");
                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("final test 6");
                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("final test 7");
                System.out.println("NOT HERE2");
                
                if (!finishedExecution)
                {
                    System.out.println("final test 8");
                    //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)
                    {
                        System.out.println("final test 9");
                        //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("final test 10");
                            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("final test 11");
                                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("final test 12");
                                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
                            {
                                System.out.println("final test 13");
                                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("final test 14");
                            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)
                            {
                                System.out.println("final test 15");
                                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;
                            
                        }
                                     //so  784948  
                                     //    +   71   = 785 019
                        if (lastDigitNumber+carryForward>9 && !finishedExecution)     //lastdigit = 9
                        {
                            System.out.println("final test 17");
                            //checks next highest MSD for another 9
                            if (remainingPortion%10==9)
                            {
                                System.out.println("final test 18");
                                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("final test 19");
                            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("final test 20");
                            System.out.println("curent ggrand: " + grandTotal);
                            System.out.println("current remain: " + remainingPortion);
                            grandTotal=remainingPortion+grandTotal;
                            return grandTotal;
                            
                        }
                            
                        }
                    }
                }
            }  //end of else
            System.out.println("final test 21");
            
            return grandTotal;
    }
   
}