def solve(): # Setup computer print("Solving Day 13") program = list(map(int, aoc.read_program('13.txt'))) computer = intcode.IntCodeComputer(program, pause_on_output=True) state = defaultdict[int] while computer.status != intcode.StatusFlag.FINISHED: # parse 3 output values for i in range(3): try: computer.run() except intcode.ProgramFinished: print("Program end found") break if computer.status != intcode.StatusFlag.FINISHED: x, y, t_id = computer.output_value computer.output_value = None state[(x, y)] = t_id total_block_tiles = sum( [t_id == TileID.BLOCK.value for t_id in state.values()]) print(f"Total Blocks: {total_block_tiles}")
def star1(): print("Star 1") computer = intcode.IntCodeComputer() with open('puzzles/day5.txt', 'r') as f: program = f.readline() computer.loadProgram(program) computer.process('1', False)
def star1(): print("Star 1") with open("puzzles/day9.txt") as f: programText = f.readline() computer = intcode.IntCodeComputer() computer.loadProgram(programText) computer.process('1', False)
def star2(): print("Star 2") # Get all permutations perm = permutations([5, 6, 7, 8, 9]) stages = [] for i in range(5): stages.append(intcode.IntCodeComputer()) with open('puzzles/day7.txt', 'r') as f: program = f.readline() maxOutput = 0 # Iterate over permutations of phase codes for p in list(perm): prevStageOutput = 0 for i in range(len(stages)): # For each set of phase codes, load the program # and add the phase code as the first input stages[i].loadProgram(program) stages[i].addInput(p[i]) while not stages[0].isTerminated: for i in range(len(stages)): #print("&&& PROCESSING {0} &&&".format(i)) stages[i].process(prevStageOutput, silent=True) #print("Stage {0} Complete. Instruction Pointer: {1}. Last Output: {2}".format(i, stages[i].instrPtr, stages[i].lastOutput)) prevStageOutput = stages[i].lastOutput finalStageOutput = prevStageOutput if finalStageOutput > maxOutput: maxOutput = finalStageOutput print("Max Thruster Output:", maxOutput)
async def run_amplifiers(program, phase_start, phase_end): loop = asyncio.get_event_loop() outputs = dict() number_computers = 5 permutations = list( itertools.permutations(range(phase_start, phase_end + 1))) for phase_setting in permutations: queues = [asyncio.Queue(loop=loop) for i in range(number_computers)] def queue_input(queue: asyncio.Queue): return queue.get def queue_output(queue: asyncio.Queue): return queue.put for i in range(number_computers): setting = list(phase_setting) queues[i].put_nowait(setting[i]) queues[0].put_nowait(0) computers = [ intcode.IntCodeComputer( program, queue_input(queues[i]), queue_output(queues[(i + 1) % len(queues)]), ) for i in range(number_computers) ] tasks = [asyncio.create_task(computer.run()) for computer in computers] await asyncio.gather(*tasks) outputs[phase_setting] = await queues[0].get() print("Highest output: ", max(outputs.values())) print("Phase setting: ", max(outputs, key=outputs.get))
async def puzzle1(program): async def simple_input(): return 1 async def simple_output(operand): print("Output: ", operand) computer = intcode.IntCodeComputer(program, simple_input, simple_output) await computer.run()
async def puzzle1(program): computers = [ intcode.IntCodeComputer( program, input_trackers[i].get_value, output_trackers[i].add, ) for i in range(number_computers) ] tasks = [asyncio.create_task(computer.run()) for computer in computers] await asyncio.gather(*tasks)
async def puzzle2(program): print("Testing with new IntCode class") async def simple_input(): return 5 async def simple_output(operand): print("Output: ", operand) computer = intcode.IntCodeComputer(program, simple_input, simple_output) await computer.run()
async def puzzle1(program): out = [] async def simple_output(operand): out.append(operand) computer = intcode.IntCodeComputer(program, None, simple_output) await computer.run() triples = [] for i in range(0, len(out) - 2, 3): triples.append((out[i], out[i + 1], out[i + 2])) blocks = [triple for triple in triples if triple[2] == 2] print("Blocks: ", len(blocks))
def star1(): print("Star 1") # Get all permutations of [0, 1, 2, 3, 4] perm = permutations([0, 1, 2, 3, 4]) stages = [] for i in range(5): stages.append(intcode.IntCodeComputer()) with open('puzzles/day7.txt', 'r') as f: program = f.readline() maxOutput = 0 for p in list(perm): prevStageOutput = 0 for i in range(len(stages)): stages[i].loadProgram(program) stages[i].process(str(p[i]) + '\n' + str(prevStageOutput)) prevStageOutput = stages[i].lastOutput finalStageOutput = stages[-1].lastOutput if finalStageOutput > maxOutput: maxOutput = finalStageOutput print("Max Thruster Output:", maxOutput)
def __init__(self, board_dim=(25, 25), ai=False): self.automated = ai game_input = self.game_input() next(game_input) program = list(map(int, aoc.read_program('13.txt'))) program[0] = 2 # Set up for unlimited play self.computer = intcode.IntCodeComputer(program, input_user=game_input, pause_on_output=True) self.state = defaultdict(int) self.score = 0 self.last_input = None self.board_dim = board_dim self.char_map = { 0: ' ', 1: '\u2588', 2: '\u2591', 3: '\u2501', 4: '\u25cf' }