<commit_before>import java.io.*;

public class Intcode
{
    /*
     * Could be enum values but ints are just so much easier!
     */

     /**
      * The Opcodes for the computer.
      */

    public static final int ADD = 1;
    public static final int MULTIPLY = 2;
    public static final int INPUT_AND_STORE = 3;
    public static final int OUTPUT = 4;
    public static final int HALT = 99;
   
    /**
     * The Parameter modes.
     */

    public static final int POSITION_MODE = 0;  // parameter is interpreted as a position
    public static final int IMMEDIATE_MODE = 1; // parameter is interpreted as a value

    private static final int MAX_PARAMETERS = 4;

    public static final String DELIMITER = ",";

    /*
     * This implementation is stateless other than being placed
     * into debug mode where it will output whatever action it
     * takes.
     */

    public Intcode (boolean debug)
    {
        _debug = debug;
    }

    public String parseAndExecute (String[] values, int initialInput)
    {
        String returnValue = null;

        for (int i = 0; i < values.length; i++)
        {
            String str = getOpcode(values[i]);
            int[] modes = getModes(values[i]);

            if (_debug)
            {
                System.out.println("Working on entry "+i+" with command "+str+
                                        " and parameter modes ...");
                printModes(modes);
            }

            /*
             * Now factor in the parameter modes.
             */

            switch (Integer.valueOf(str))
            {
                case Intcode.ADD:
                {
                    /*
                     * Opcode 1 adds together numbers read from two positions
                     * and stores the result in a third position. The three integers
                     * immediately after the opcode tell you these three positions - the
                     * first two indicate the positions from which you should read the
                     * input values, and the third indicates the position at which
                     * the output should be stored.
                     */

                     int param1 = Integer.valueOf(values[i+1]);
                     int param2 = Integer.valueOf(values[i+2]);
                     int param3 = Integer.valueOf(values[i+3]);

                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);

                     if (modes[1] == POSITION_MODE)
                        param2 = Integer.valueOf(values[param2]);

                     if (modes[2] == POSITION_MODE)
                        param3 = Integer.valueOf(values[param3]);

                     if (_debug)
                        System.out.println("Adding "+param1+" and "+param2);

                     int sum = param1+param2;

                     if (_debug)
                        System.out.println("Storing "+sum+" at position "+param3);

                     values[param3] = String.valueOf(sum);

                     i = i+3;  // move the pointer on.
                }
                break;
                case Intcode.MULTIPLY:
                {
                    /*
                     * Opcode 2 works exactly like opcode 1, except it multiplies the
                     * two inputs instead of adding them. Again, the three integers after
                     * the opcode indicate where the inputs and outputs are, not their values.
                     */

                    int param1 = Integer.valueOf(values[i+1]);
                    int param2 = Integer.valueOf(values[i+2]);
                    int param3 = Integer.valueOf(values[i+3]);

                    if (modes[0] == POSITION_MODE)
                    {
                        System.out.println("Param1 "+param1+" is POSITION_MODE");

                        param1 = Integer.valueOf(values[param1]);
                    }
                    else
                        System.out.println("Param1 "+param1+" is IMMEDIATE_MODE");

                    System.out.println("Param1 "+param1);

                    if (modes[1] == POSITION_MODE)
                    {
                        System.out.println("Param2 "+param2+" is POSITION_MODE");

                        param2 = Integer.valueOf(values[param2]);
                    }

                    System.out.println("Param2 "+param2);

                    if (modes[2] == POSITION_MODE)
                    {
                        System.out.println("Param3 "+param3+" is POSITION_MODE");

                        param3 = Integer.valueOf(values[param3]);
                    }

                    System.out.println("Param3 "+param3);

                    if (_debug)
                        System.out.println("Multiplying "+param1+" and "+param2);

                    int product = Integer.valueOf(param1)*Integer.valueOf(param2);

                    if (_debug)
                        System.out.println("Storing "+product+" at position "+param3);

                    values[param3] = String.valueOf(product);

                    i = i+3;  // move the pointer on.
                }
                break;
                case Intcode.INPUT_AND_STORE:
                {
                    /*
                     * Opcode 3 takes a single integer as input and saves it to
                     * the position given by its only parameter.
                     */

                     int param1 = Integer.valueOf(values[i+1]);

                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);

                     if (_debug)
                        System.out.println("Storing "+initialInput+" at position "+param1);

                     values[param1] = String.valueOf(initialInput);

                     i = i+1;  // move the pointer on.
                }
                break;
                case Intcode.OUTPUT:
                {
                    /*
                     * Opcode 4 outputs the value of its only parameter.
                     */

                     int param1 = Integer.valueOf(values[i+1]);

                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);

                     if (_debug)
                        System.out.println("Pulling value from entry "+param1);

                     System.out.println("Output: "+String.valueOf(values[param1]));

                     returnValue = values[param1];

                     i = i+1;  // move the pointer on.
                }
                break;
                case Intcode.HALT:
                {
                    /*
                     * Means that the program is finished and should immediately halt.
                     */

                     if (_debug)
                        System.out.println("Halting execution.");

                     return returnValue;
                }
                default:<commit_msg>Update Intcode.java<commit_after>import java.io.*;

public class Intcode
{
    /*
     * Could be enum values but ints are just so much easier!
     */

     /**
      * The Opcodes for the computer.
      */

    public static final int ADD = 1;
    public static final int MULTIPLY = 2;
    public static final int INPUT_AND_STORE = 3;
    public static final int OUTPUT = 4;
    public static final int HALT = 99;
   
    /**
     * The Parameter modes.
     */

    public static final int POSITION_MODE = 0;  // parameter is interpreted as a position
    public static final int IMMEDIATE_MODE = 1; // parameter is interpreted as a value

    private static final int MAX_PARAMETERS = 4;

    public static final String DELIMITER = ",";

    /*
     * This implementation is stateless other than being placed
     * into debug mode where it will output whatever action it
     * takes.
     */

    public Intcode (boolean debug)
    {
        _debug = debug;
    }

    public String parseAndExecute (String[] values, int initialInput)
    {
        String returnValue = null;

        for (int i = 0; i < values.length; i++)
        {
            String str = getOpcode(values[i]);
            int[] modes = getModes(values[i]);

            if (_debug)
            {
                System.out.println("Working on entry "+i+" with command "+str+
                                        " and parameter modes ...");
                printModes(modes);
            }

            /*
             * Now factor in the parameter modes.
             */

            switch (Integer.valueOf(str))
            {
                case Intcode.ADD:
                {
                    /*
                     * Opcode 1 adds together numbers read from two positions
                     * and stores the result in a third position. The three integers
                     * immediately after the opcode tell you these three positions - the
                     * first two indicate the positions from which you should read the
                     * input values, and the third indicates the position at which
                     * the output should be stored.
                     */

                     int param1 = Integer.valueOf(values[i+1]);
                     int param2 = Integer.valueOf(values[i+2]);
                     int param3 = Integer.valueOf(values[i+3]);

                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);

                     if (modes[1] == POSITION_MODE)
                        param2 = Integer.valueOf(values[param2]);

                     if (modes[2] == POSITION_MODE)
                        param3 = Integer.valueOf(values[param3]);

                     if (_debug)
                        System.out.println("Adding "+param1+" and "+param2);

                     int sum = param1+param2;

                     if (_debug)
                        System.out.println("Storing "+sum+" at position "+param3);

                     values[param3] = String.valueOf(sum);

                     i = i+3;  // move the pointer on.
                }
                break;
                case Intcode.MULTIPLY:
                {
                    /*
                     * Opcode 2 works exactly like opcode 1, except it multiplies the
                     * two inputs instead of adding them. Again, the three integers after
                     * the opcode indicate where the inputs and outputs are, not their values.
                     */

                    int param1 = Integer.valueOf(values[i+1]);
                    int param2 = Integer.valueOf(values[i+2]);
                    int param3 = Integer.valueOf(values[i+3]);

                    if (modes[0] == POSITION_MODE)
                    {
                        System.out.println("Param1 "+param1+" is POSITION_MODE");

                        param1 = Integer.valueOf(values[param1]);
                    }
                    else
                        System.out.println("Param1 "+param1+" is IMMEDIATE_MODE");

                    System.out.println("Param1 "+param1);

                    if (modes[1] == POSITION_MODE)
                    {
                        System.out.println("Param2 "+param2+" is POSITION_MODE");

                        param2 = Integer.valueOf(values[param2]);
                    }

                    System.out.println("Param2 "+param2);

                    if (modes[2] == POSITION_MODE)
                    {
                        System.out.println("Param3 "+param3+" is POSITION_MODE");

                        param3 = Integer.valueOf(values[param3]);
                    }

                    System.out.println("Param3 "+param3);

                    if (_debug)
                        System.out.println("Multiplying "+param1+" and "+param2);

                    int product = Integer.valueOf(param1)*Integer.valueOf(param2);

                    if (_debug)
                        System.out.println("Storing "+product+" at position "+param3);

                    values[param3] = String.valueOf(product);

                    i = i+3;  // move the pointer on.
                }
                break;
                case Intcode.INPUT_AND_STORE:
                {
                    /*
                     * Opcode 3 takes a single integer as input and saves it to
                     * the position given by its only parameter.
                     */

                     int param1 = Integer.valueOf(values[i+1]);

                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);

                     if (_debug)
                        System.out.println("Storing "+initialInput+" at position "+param1);

                     values[param1] = String.valueOf(initialInput);

                     i = i+1;  // move the pointer on.
                }
                break;
                case Intcode.OUTPUT:
                {
                    /*
                     * Opcode 4 outputs the value of its only parameter.
                     */

                     int param1 = Integer.valueOf(values[i+1]);

                     /*
                     if (modes[0] == POSITION_MODE)
                        param1 = Integer.valueOf(values[param1]);
*/

                     if (_debug)
                        System.out.println("Pulling value from entry "+param1);

                     System.out.println("Output: "+String.valueOf(param1));

                     returnValue = Integer.toString(param1);

                     i = i+1;  // move the pointer on.
                }
                break;
                case Intcode.HALT:
                {
                    /*
                     * Means that the program is finished and should immediately halt.
                     */

                     if (_debug)
                        System.out.println("Halting execution.");

                     return returnValue;
                }
                default:
{'fraction_non_alphanumeric': 0.08736969009290121, 'fraction_numerical': 0.012765052123962839, 'mean_word_length': 1.1279613701524067, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '346648', 'n_tokens_mistral': 3529, 'n_tokens_neox': 3133, 'n_words': 948}