<commit_before>package day05;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class IntcodeComputer extends day02.IntcodeComputer {

    public final static int OPCODE_INPUT = 3;
    public final static int OPCODE_OUTPUT = 4;

    protected Queue<Integer> inputs;
    protected List<Integer> outputs;

    public class InvalidImmediateException extends IntcodeException {}

    public IntcodeComputer(int[] positions) {
        super(positions);
    }

    public int process(int[] inputs) throws IntcodeException {
        this.inputs = new LinkedList<>();
        for (int input : inputs) {
            this.inputs.add(input);
        }
        outputs = new ArrayList<>();

        int opcode = positions[instructionPointer];
        while (opcode != OPCODE_HALT) {
            handleOpcode(opcode);
            opcode = positions[instructionPointer];
        }
        // Return the last output
        return outputs.get(outputs.size() - 1);
    }

    protected void handleOpcode(int opcode) throws IntcodeException {
        Instruction instruction = new Instruction(opcode);
        int param1 = positions[instructionPointer + 1];
        int item1, item2;
        switch (instruction.opcode) {
            case OPCODE_INPUT:
                System.out.println(opcode + " " + param1);
                positions[param1] = inputs.remove();
                instructionPointer += 2;
                return;
            case OPCODE_OUTPUT:
                System.out.println(opcode + " " + param1);
                outputs.add(positions[param1]);
                System.out.println("OUTPUT " + positions[param1] + "\n");
                instructionPointer += 2;
                return;
        }
        int param2 = positions[instructionPointer + 2];
        int param3 = positions[instructionPointer + 3];

        System.out.println(opcode + " " + param1 + " " + param2 + " " + param3);

        item1 = instruction.modeParam1 == Instruction.PARAMETER_MODE_POSITION ? positions[param1] : param1;
        item2 = instruction.modeParam2 == Instruction.PARAMETER_MODE_POSITION ? positions[param2] : param2;
        // Confirm that param 3 can never be in immediate mode
        if (instruction.modeParam3 == Instruction.PARAMETER_MODE_IMMEDIATE) {
            throw new InvalidImmediateException();
        }

        switch (instruction.opcode) {
            case OPCODE_ADD:
                positions[param3] = item1 + item2;
                break;
            case OPCODE_MULTIPLY:
                positions[param3] = item1 * item2;
                break;
            default:
                throw new UnknownOpcodeException();
        }
        instructionPointer += 4;
    }
}<commit_msg>One bugfix for that day5 intcode computer<commit_after>package day05;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class IntcodeComputer extends day02.IntcodeComputer {

    public final static int OPCODE_INPUT = 3;
    public final static int OPCODE_OUTPUT = 4;

    protected Queue<Integer> inputs;
    protected List<Integer> outputs;

    public class InvalidImmediateException extends IntcodeException {}

    public IntcodeComputer(int[] positions) {
        super(positions);
    }

    public int process(int[] inputs) throws IntcodeException {
        this.inputs = new LinkedList<>();
        for (int input : inputs) {
            this.inputs.add(input);
        }
        outputs = new ArrayList<>();

        int opcode = positions[instructionPointer];
        while (opcode != OPCODE_HALT) {
            handleOpcode(opcode);
            opcode = positions[instructionPointer];
        }
        // Return the last output
        return outputs.get(outputs.size() - 1);
    }

    protected void handleOpcode(int opcode) throws IntcodeException {
        Instruction instruction = new Instruction(opcode);
        int param1 = positions[instructionPointer + 1];
        int item1 = instruction.modeParam1 == Instruction.PARAMETER_MODE_POSITION ? positions[param1] : param1;
        switch (instruction.opcode) {
            case OPCODE_INPUT:
                System.out.println(opcode + " " + param1);
                positions[param1] = inputs.remove();
                instructionPointer += 2;
                return;
            case OPCODE_OUTPUT:
                System.out.println(opcode + " " + param1);
                outputs.add(item1);
                System.out.println("OUTPUT " + item1 + "\n");
                instructionPointer += 2;
                return;
        }
        int param2 = positions[instructionPointer + 2];
        int param3 = positions[instructionPointer + 3];

        System.out.println(opcode + " " + param1 + " " + param2 + " " + param3);

        int item2 = instruction.modeParam2 == Instruction.PARAMETER_MODE_POSITION ? positions[param2] : param2;
        // Confirm that param 3 can never be in immediate mode
        if (instruction.modeParam3 == Instruction.PARAMETER_MODE_IMMEDIATE) {
            throw new InvalidImmediateException();
        }

        switch (instruction.opcode) {
            case OPCODE_ADD:
                positions[param3] = item1 + item2;
                break;
            case OPCODE_MULTIPLY:
                positions[param3] = item1 * item2;
                break;
            default:
                throw new UnknownOpcodeException();
        }
        instructionPointer += 4;
    }
}
{'fraction_non_alphanumeric': 0.09244604316546763, 'fraction_numerical': 0.014928057553956835, 'mean_word_length': 2.1418079096045197, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697213', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1450, 'n_words': 353}