Example #1
0
# Initialize the amps.
amps = []
for _ in range(0, 5):
    amp = Computer()
    amp.loadProgram(program); 
    amps.append(amp)

maxOutput = 0
sequences = list(itertools.permutations([5, 6, 7, 8, 9]))

for sequence in sequences:

    for i in range(0, 5):
        amp = amps[i]
        amp.resetProgram()
        amp.pushInput([sequence[i]])

    lastOutput = 0
    while not amps[4].finished:
        for i in range(0, 5):
            amp = amps[i]
            amp.pushInput([lastOutput])
            amp.runProgram()

            lastOutput = amp.outputBuffer[-1]

    if amps[4].outputBuffer[-1] > maxOutput:
        maxOutput = lastOutput

print(maxOutput)
Example #2
0
from common.computer import Computer

# Read program data from a text file.
with open("Day2/i1.txt") as input:
    program = input.read()

# Initialize the program.
computer = Computer()
computer.loadProgram(program)

# Run the program until the correct inputs are found.
for i in range(99):
    for j in range(99):

        # Set the initial state.
        computer.program[1] = i
        computer.program[2] = j

        computer.runProgram()

        # Test if the desired output was found.
        if (computer.program[0] == 19690720):
            print(100 * i + j)
            exit()

        computer.resetProgram()