def task_two(): instructions = load_data() nbr_instructions = len(instructions) for i in range(nbr_instructions): loop_resolved = False new_instructions = deepcopy(instructions) if instructions[i][0] == "jmp": new_instructions[i][0] = "nop" elif instructions[i][0] == "nop": new_instructions[i][0] = "jmp" computer = Computer(new_instructions) performed_instructions = set() loop = False index = 0 while not loop: performed_instructions.add(index) computer.run_one_instruction() index = computer.get_instruction_index() if index in performed_instructions: loop = True elif index >= nbr_instructions: loop_resolved = True break if loop_resolved: break acc = computer.get_accumulator() print("Accumulator value after finished program:", acc)
def task_one(): instructions = load_data() computer = Computer(instructions) performed_instructions = set() loop = False index = computer.get_instruction_index() while not loop: performed_instructions.add(index) computer.run_one_instruction() index = computer.get_instruction_index() if index in performed_instructions: loop = True acc = computer.get_accumulator() print("Accumulator value before loop:", acc)
def __post_init__(self): self.computer = Computer(Memory(map(int, self.program.split(","))))
from common.computer import Computer # Read program data from a text file. with open("Day9/i1.txt") as input: program = input.read() # Initialize the program. computer = Computer() computer.loadProgram(program); computer.pushInput([1]) # Run the program. computer.runProgram() # Print the output. computer.printOutput()
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) # Set the initial state. computer.program[1] = 12 computer.program[2] = 2 # Run the program. computer.runProgram() print(computer.program[0])
from common.computer import Computer import itertools # Read program data from a text file. with open("Day7/i1.txt") as input: program = input.read() # 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()
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()