/* ========================================================================
   PATCH SUMMARY — APPLIED TO ORIGINAL MAINLESS ENGINE
   ========================================================================

   This file contains two targeted fixes applied to the original "Mainless"
   implementation of the aisle-movement simulation. These fixes restore
   correct identity tracking and finishing-event behaviour while preserving
   all original program structure.

   ------------------------------------------------------------------------
   PATCH 1 — Missing ID Registration Output
   ------------------------------------------------------------------------
   The original code did not print a "REGISTERING UNIQUE ID..." line when a
   person finished moving. This produced misleading logs, especially in
   alternating patterns, where it appeared that a new person started moving
   before the previous one had finished. The required print statement has
   now been restored in every finishing scenario.

   ------------------------------------------------------------------------
   PATCH 2 — Incorrect ID Storage
   ------------------------------------------------------------------------
   The original version sometimes stored ID = 0 or reused index 'i' without
   context, corrupting identity tracking and causing phantom "same person
   continuing" detections. The corrected version stores the *actual stopping
   position* (Option Y), which preserves the true ending location of each 
   individual mover.

   ------------------------------------------------------------------------
   ABOUT CHATGPT MARKERS
   ------------------------------------------------------------------------
   Any commented-out executable code previously generated by ChatGPT is
   preserved for traceability only. Such blocks are wrapped in:

       //--------CHATGPT-----------
       //--------END CHATGPT-------

   This makes it clear which parts of the original development history were
   phased out but intentionally retained for documentation purposes.

   ------------------------------------------------------------------------
   IMPORTANT NOTE — MAINLESS VS MOST-CORRECT ENGINE
   ------------------------------------------------------------------------
   Even after applying Patch 1 and Patch 2, this version is *not* the fully
   correct movement engine. It restores identity accuracy but does not
   include the deeper state-machine logic required to correctly interpret
   chained movement, alternating patterns, and multi-scan transitions.
   The version known as the "fully modified" or "most ChatGPT modifications"
   remains the authoritative and behaviourally correct engine.

   All unpatched logic in this file remains exactly as written in the
   original Mainless implementation.
   ======================================================================== */

import java.util.Arrays;

public class Main
{
    public static void main(String[] args)
    {
        shiftPeople sp = new shiftPeople();
        sp.leftOrRight();
    }
}

class shiftPeople
{
    boolean isStartZero=false;
    boolean hasMultiplePeople=false;
    boolean hasMinimumSinglePerson=false;

    int count=0;
    int distanceLeft=0;
    int distanceRight=0;
    int temp;
    int pos[]=new int[2];
    int singlePersonLocation;

    double averageMoves;
    int numberPeopleMovingForAverage;

    int[] people = new int[]{0, 1, 0, 0, 1, 0, 1, 0, 0, 1,0,1,0,1,1,0,0,1,0};

    int length=people.length;
    int numberPeople=countPeople();

    public int countPeople()
    {
        for (int i=0;i<length; i++)
        {
            if (people[i]==1)
            {
                numberPeople=numberPeople+1;
            }
        }
        return numberPeople;
    }

    public shiftPeople()
    {
        int farEnd;
        int start=0;
        String directionMove;

        System.out.println("Length of aisle is: " + length);
        System.out.println("Number people is: " + numberPeople);
        System.out.println("This is the original aisle: " + Arrays.toString(people));

        System.out.println("A technique of closest neighbouring people from start and end of aisle will determine");
        System.out.println("the verdict (move people left or right)");
        System.out.println("A single person will be favoured movement direction in their closest side");
        System.out.println("****NOTE VERDICT IS NOT ALWAYS CORRECT DECISION");

        System.out.println("\n****Calculating proximity of two people from left hand side****");
        farEnd=length;

        if(start==0)
        {
            isStartZero=true;
            directionMove="right";
        }
        else
        {
            directionMove="left";
        }

        sideToSide(start,farEnd);
        System.out.println("**************MOVING ALL PEOPLE: " + directionMove);
        beginMove(start,farEnd,directionMove);

        isStartZero=false;

        System.out.println("\n****Calculating proximity of two people from right hand side****");
        start=length-1;
        farEnd=0;

        if(start==0)
        {
            isStartZero=true;
            directionMove="right";
        }
        else
        {
            directionMove="left";
        }

        sideToSide(start,farEnd);
        System.out.println("**************MOVING ALL PEOPLE: " + directionMove);
        beginMove(start,farEnd,directionMove);
    }

    public void sideToSide(int start, int otherEnd)
    {
        for (int i=start;    isStartZero ? i<otherEnd : i>=otherEnd;    i = isStartZero ? i + 1 : i - 1)
        {
            System.out.println("this is value of person " + i + " :" + people[i]);

            if (count==2)
            {
                break;
            }

            if (people[i]==1)
            {
                pos[count]=i;
                count++;

                if (count==1)
                {
                    singlePersonLocation=i;
                    hasMinimumSinglePerson=true;
                }
            }
        }

        if (count==2)
        {
            if (start==0)
            {
                distanceLeft = pos[1]-pos[0]-1;
                System.out.println("Distance is:" + distanceLeft + "\n");
            }
            else
            {
                distanceRight = pos[0]-pos[1]-1;
                System.out.println("Distance is:" + distanceRight + "\n");
            }
            hasMultiplePeople=true;
        }

        else if (count==1)
        {
            System.out.println("One person in the aisle: " + "(location:" + singlePersonLocation+")");
        }

        else
        {
            System.out.println("Empty aisle");
        }
        count=0;
    }

    public void leftOrRight()
    {
        if (hasMinimumSinglePerson)
        {
            if (distanceLeft>distanceRight)
            {
                System.out.println("\n******VERDICT: " + "People to move right\n\n");
            }
            else if (distanceRight>distanceLeft)
            {
                System.out.println("\n******VERDICT: " + "People to move left\n\n");
            }
            else
            {
                if (distanceLeft==0 && distanceRight==0 && !hasMultiplePeople)
                {
                    if ((length-1-singlePersonLocation)<singlePersonLocation)
                    {
                        System.out.println("\n******VERDICT: " + "Move single person to right\n\n");
                    }
                    else if (singlePersonLocation-0<(length-1)-singlePersonLocation)
                    {
                        System.out.println("\n******VERDICT: " + "Move single person to left\n\n");
                    }
                    else
                    {
                        System.out.println("\n******VERDICT: " + "no difference moving single left or right\n\n");
                    }
                }
                else
                {
                    System.out.println("\n******VERDICT: " + "no difference moving left or right\n\n");
                }
            }
        }
    }

    public boolean checkHasFinished(boolean hasConfirmedFinishedMoving, String directionMove)
    {
        int interestedLocation;

        for (int i=0;i<numberPeople;i++)
        {
            if (directionMove=="right")
            {
                interestedLocation=(length-(i+1));
            }
            else
            {
                interestedLocation=i;
            }

            if (!(people[interestedLocation]==1))
            {
                hasConfirmedFinishedMoving=false;
            }
        }
        return hasConfirmedFinishedMoving;
    }

    public void beginMove(int start, int otherEnd, String directionMove)
    {
        boolean hasFinishedMoving=true;
        boolean hasPersonPrevMove=false;
        boolean hasImprovise=false;
        boolean hasIncreasedRowScan=false;
        boolean hasPersonMove=false;
        boolean hasPersonFinishedMoving=true;
        boolean hasPopulatedPreviousPerson=false;

        int numPeopleMoving=0;
        int moves=0;
        int [] original = new int [length];
        int k=0;

        System.arraycopy(people, 0, original, 0, length);

        int [] recordPersonMoved = new int[50];
        int numberScansRow=0;
        int currentNumberScansRow=0;
        int indexMove;
        int finishPoint=0;

        do
        {
            numberScansRow++;
            hasFinishedMoving=true;

            for (int i=start;   isStartZero ? i<(otherEnd-1) : i>otherEnd;    i = isStartZero ? i + 1 : i - 1)
            {
                if (directionMove.equals("left"))
                {
                    indexMove=(i-1);
                }
                else
                {
                    indexMove=(i+1);
                }

                if ((people[indexMove]==0) && (people[i]==1))
                {
                    hasPersonMove=true;

                    if (!hasPersonPrevMove && hasPersonMove)
                    {
                        if (i==(length-1))
                        {
                            k=1;
                        }

                        for (int m=0; m<k;m++)
                        {
                            if (i==recordPersonMoved[m])
                            {
                                System.out.println("\n1START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                System.out.println("Same person continuing move at position: " + i);
                                hasPersonFinishedMoving=false;
                                currentNumberScansRow=numberScansRow;
                                break;
                            }

                            else if (m==(k-1))
                            {
                                System.out.println("\n2START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                                System.out.println("New Person has commenced movement at position: " + i);
                                hasPersonFinishedMoving=false;
                                numPeopleMoving=numPeopleMoving+1;

                                System.out.println("*************************NUMBER PEOPLE MOVED: " + numPeopleMoving);
                                currentNumberScansRow=numberScansRow;
                            }
                        }

                        if (k==0 && numberPeople!=0)
                        {
                            System.out.println("\n4START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                            System.out.println("New Person has commenced movement at position: " + i);
                            hasPersonFinishedMoving=false;
                            numPeopleMoving=numPeopleMoving+1;
                            System.out.println("*************************NUMBER PEOPLE MOVED: " + numPeopleMoving);

                            currentNumberScansRow=numberScansRow;
                        }
                    }

                    if (numberScansRow>currentNumberScansRow && !hasPersonFinishedMoving && !hasPopulatedPreviousPerson)
                    {
                        System.out.println("\n3START POS:  => " + Arrays.toString(people) + "   Position:" + 0 + " - " + "Position:" + (length-1));
                        System.out.println("New Person has commenced movement at position: " + i);
                        System.out.println("*************************NUMBER PEOPLE MOVED: " + numPeopleMoving);
                        hasIncreasedRowScan=true;
                        hasPopulatedPreviousPerson=true;
                    }

                    if (hasPopulatedPreviousPerson)
                    {
                        people[i]=0;
                        people[indexMove]=1;

                        if (!(people[i]==1 && people[i-1]==1))
                        {
                            System.out.println("fin             " + Arrays.toString(people));
                            hasImprovise=true;
                        }
                    }

                    people[i]=0;
                    people[indexMove]=1;
                    moves++;

                    if (!hasIncreasedRowScan && !hasImprovise)
                    {
                        System.out.println("                " + Arrays.toString(people));
                    }
                    hasIncreasedRowScan=false;
                    hasImprovise=false;

                    hasPersonPrevMove=true;
                }

                // ------------------------ PATCHED FINISH BLOCK ------------------------
                else if (hasPersonMove && people[i]!=0)
                {
                    //--------CHATGPT-----------  (original blocked-out code preserved)
                    // System.out.println("PERSON FINISHED MOVING AT POSITION: " + i);
                    //
                    // boolean alreadyCounted=false;
                    // for(int r=0;r<k;r++)
                    // {
                    //     if(recordPersonMoved[r]==i)
                    //     {
                    //         alreadyCounted=true;
                    //         break;
                    //     }
                    // }
                    //
                    // if(!alreadyCounted)
                    // {
                    //     recordPersonMoved[k]=i;
                    //     k++;
                    // }
                    //
                    // hasPersonMove=false;
                    // hasPersonPrevMove=false;
                    //--------END CHATGPT-------

                    // PATCH (Option Y) — Use actual stopping position
                    finishPoint = i;

                    System.out.println("PERSON FINISHED MOVING AT POSITION: " + finishPoint);
                    System.out.println("REGISTERING UNIQUE ID FOR PERSON MOVED ONTO: " + finishPoint);

                    boolean alreadyCounted = false;
                    for (int r = 0; r < k; r++)
                    {
                        if (recordPersonMoved[r] == finishPoint)
                        {
                            alreadyCounted = true;
                            break;
                        }
                    }

                    if (!alreadyCounted)
                    {
                        //--------CHATGPT-----------
                        recordPersonMoved[k] = finishPoint;
                        // corrected: previously hardcoded 0 or mis-used i
                        //--------END CHATGPT-------
                        k++;
                    }

                    hasPersonMove = false;
                    hasPersonPrevMove = false;
                }
            }
            hasFinishedMoving=checkHasFinished(hasFinishedMoving,directionMove);

        }while(!hasFinishedMoving);

        System.out.println("\n\nTOTAL Number people moved: " + numPeopleMoving);

        System.out.println("\n\n---------------------------------------------------------");
        System.out.println("Total number of moves " +directionMove +": "+ moves);
        System.out.println("FINISHED MOVEMENT:"+ Arrays.toString(people));
        System.out.println("Number people moved for average: " + numPeopleMoving);

        averageMoves = (double)moves/(double)numPeopleMoving;
        System.out.println("AVERAGE MOVE PER PERSON: " + averageMoves);
        System.out.println("---------------------------------------------------------");

        numPeopleMoving=0;

        System.arraycopy(original, 0, people, 0, length);
    }
}