Ejemplo n.º 1
0
def executeIntCode(intCodeProgram, noun=12, verb=2):

    if (len(intCodeProgram) < 3):
        saveExit("intCodeProgram empty")

    # set noun and verb:
    intCodeProgram[1] = noun
    intCodeProgram[2] = verb

    return executeUnchangedIntCode(intCodeProgram)
def executeInstruction(intCodeProgram, index, opcode, modes=[0, 0, 0]):
    if opcode == 'add':
        return handleAddition(intCodeProgram, modes, index)
    if opcode == 'multiply':
        return handleMultiplication(intCodeProgram, modes, index)
    if opcode == 'input':
        return handleInput(intCodeProgram, modes, index)
    if opcode == 'output':
        return handleOutput(intCodeProgram, modes, index)
    if opcode == 'jumpIfTrue':
        return handleJumpIfTrue(intCodeProgram, modes, index)
    if opcode == 'jumpIfFalse':
        return handleJumpIfFalse(intCodeProgram, modes, index)
    if opcode == 'lessThan':
        return handleLessThan(intCodeProgram, modes, index)
    if opcode == 'equals':
        return handleEquals(intCodeProgram, modes, index)

    saveExit("Unknown opcode while executing instruction")
Ejemplo n.º 3
0
def executeUnchangedIntCode(intCodeProgram):
    # Check first opcode
    index = 0
    opcode, modes = checkOpcode(intCodeProgram[index])

    # loop through code
    while opcode != 'error' and opcode != 'abort':
        # execute instruction and move index
        success, index = executeInstruction(intCodeProgram, index, opcode,
                                            modes)
        if success != True:
            opcode = 'error'
            break

        # check next opcode
        if (len(intCodeProgram) <= index):
            saveExit("Index out of bounds when checking opcode")
        opcode, modes = checkOpcode(intCodeProgram[index])

    if (opcode == 'abort'):
        return True
    else:
        return False
def getParam(intCodeProgram, index):
    if index + 1 >= len(intCodeProgram):
        saveExit("Index out of bounds in 1 parameters instruction")
    # get numbers and indices
    return intCodeProgram[index + 1]
def getParams(intCodeProgram, index, amnt):
    if index + amnt >= len(intCodeProgram):
        saveExit("Index out of bounds in", amnt, "parameters instruction")
    # get numbers and indices
    return intCodeProgram[index + 1:index + amnt + 1]