import java.util.*;
import java.io.*;

public class MoonSystem
{
    public static final String X_VALUE = "x=";
    public static final String Y_VALUE = "y=";
    public static final String Z_VALUE = "z=";

    public static final String DEMARCATION = ",";

    public MoonSystem (String dataFile, boolean debug)
    {
        _system = new Vector<Moon>();
        _dataFile = dataFile;
        _debug = debug;

        createSystem();
    }

    public final Vector<Moon> getMoons ()
    {
        return _system;
    }
    
    /*
     * Brute force won't work in any meaningful time! However, the motion of each moon in each dimension
     * is independent of motion in other dimensions so we can simulate each dimension independently. Also
     * the simulation equations are time-symmetical, i.e., the first repeated state is the initial state.
     */

     // do tbis for all axes

    public final long[] periodicity ()
    {
        long[] periods = new long[3];
        Vector<Moon> originalSystem = _system;

        periods[0] = periodicityOfAxis('x');
        
        _system = originalSystem;
        periods[1] = periodicityOfAxis('y');

        _system = originalSystem;
        periods[2] = periodicityOfAxis('z');

        return periods;
    }

    /*
     * Initially we won't store the data for each step but simply
     * update position and velocity in situ.
     */

    public final void applyGravity ()
    {
        for (int i = 0; i < _system.size(); i++)
        {
            Moon currentMoon = _system.elementAt(i);

            for (int j = 0; j < _system.size(); j++)
            {
                Moon nextMoon = _system.elementAt(j);

                currentMoon.updateVelocity(nextMoon);
            }
        }

        for (int i = 0; i < _system.size(); i++)
        {
            Moon currentMoon = _system.elementAt(i);

            currentMoon.updatePosition();
        }
    }

    public String moonCoordinates ()
    {
        Enumeration<Moon> iter = _system.elements();
        String toReturn = "";

        while (iter.hasMoreElements())
        {
            toReturn += iter.nextElement().getPosition()+"\n";
        }

        return toReturn;
    }

    public final String moonVelocities ()
    {
        Enumeration<Moon> iter = _system.elements();
        String toReturn = "";

        while (iter.hasMoreElements())
        {
            toReturn += iter.nextElement().getVelocity()+"\n";
        }

        return toReturn;
    }

    public final int totalEnergy ()
    {
        Enumeration<Moon> iter = _system.elements();
        int total = 0;

        while (iter.hasMoreElements())
        {
            total += iter.nextElement().getTotalEnergy();
        }

        return total;
    }

    @Override
    public String toString ()
    {
        Enumeration<Moon> iter = _system.elements();
        String toReturn = "The system: ";

        while (iter.hasMoreElements())
        {
            toReturn += iter.nextElement() + ", ";
        }

        return toReturn;
    }

    private final long periodicityOfAxis (char axis)
    {
        Vector<Moon> tempSystem = new Vector<Moon>();
        Enumeration<Moon> iter = _system.elements();
        Moon[] theState = new Moon[_system.size()];
        int index = 0;
        boolean found = false;

        while (iter.hasMoreElements())
        {
            Moon currentMoon = iter.nextElement();
            Moon replacement = null;
            
            if (axis == 'x')
                replacement = new Moon(currentMoon.getPosition().getX(), 0, 0);
            else
            {
                if (axis == 'y')
                    replacement = new Moon(0, currentMoon.getPosition().getY(), 0);
                else
                    replacement = new Moon(0, 0, currentMoon.getPosition().getZ());
            }

            theState[index] = new Moon(replacement);
            tempSystem.add(replacement);

            index++;
        }

        _system = tempSystem;

        long period = 0;

        while (!found)
        {
            applyGravity();
            period++;

            for (int i = 0; i < theState.length; i++)
            {
                System.out.println("**comparing "+_system.elementAt(i)+" and "+theState[i]);

                if (_system.elementAt(i).equals(theState[i]))
                    found = true;
                else
                    found = false;
            }
        }

        return period;
    }

    private void createSystem ()
    {
        /*
         * Open the data file and read it in.
         */

        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(_dataFile));
            String line = null;

            while ((line = reader.readLine()) != null)
            {
                parseLine(line);
            }
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (Throwable ex)
            {
            }
        }
    }

    private void parseLine (String line)
    {
        if (_debug)
            System.out.println("Parsing data: "+line);

        int endX = line.indexOf(DEMARCATION);
        String xData = line.substring(X_VALUE.length()+1, endX);

        if (_debug)
            System.out.println("xData "+xData);

        int startY = endX +2;
        String yData = line.substring(startY+Y_VALUE.length(), line.indexOf(DEMARCATION, startY));

        if (_debug)
            System.out.println("yData "+yData);

        int endZ = line.length()-1;
        String zData = line.substring(line.indexOf(Z_VALUE)+Z_VALUE.length(), endZ);

        if (_debug)
            System.out.println("zData "+zData);

        _system.add(new Moon(Integer.parseInt(xData), Integer.parseInt(yData), Integer.parseInt(zData)));
    }
    
    private Vector<Moon> _system;
    private String _dataFile;
    private boolean _debug;
}
@@ -3283,39 +3283,8 @@
 = 0;
-%0A        boolean found = false;
 %0A%0A  
@@ -3418,28 +3418,16 @@
 = null;%0A
-            
 %0A       
@@ -3974,24 +3974,55 @@
  period = 0;
+%0A        boolean found = false;
 %0A%0A        wh
@@ -4036,32 +4036,70 @@
 ound)%0A        %7B%0A
+            int numberOfMatches = 0;%0A%0A
             appl
@@ -4106,24 +4106,25 @@
 yGravity();%0A
+%0A
             
@@ -4134,16 +4134,82 @@
 iod++;%0A%0A
+            System.out.println(%22**state size %22+theState.length);%0A%0A
         
@@ -4448,20 +4448,25 @@
     
-found = true
+numberOfMatches++
 ;%0A  
@@ -4471,34 +4471,79 @@
             
+%7D%0A%0A
     
-else%0A 
+        if (numberOfMatches == theState.length)%0A
             
@@ -4550,19 +4550,16 @@
     
-   
 found = 
 fals
@@ -4554,20 +4554,19 @@
 found = 
-fals
+tru
 e;%0A     
@@ -4568,35 +4568,140 @@
             
+  
-%7D%0A        %7D
+  %0A            System.out.println(%22**found %22+found);%0A        %7D%0A%0A        System.out.println(%22**period is %22+period);
 %0A%0A        re

fix checker