def test_execute_program_day_2_solution(input_day_2): memory = input_day_2.copy() memory[1:3] = [12, 2] __ = list(execute_program(memory, [])) assert memory[0] == 3706713 memory = input_day_2.copy() memory[1:3] = [86, 9] __ = list(execute_program(memory, [])) assert memory[0] == 19690720
def test_execute_program_day_9_examples(): memory = [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ] output = list(execute_program(memory.copy(), [])) assert output == memory memory = [1102, 34915192, 34915192, 7, 4, 7, 99, 0] output = list(execute_program(memory.copy(), [])) assert len(output) == 1 assert len(str(output[0])) == 16 memory = [104, 1125899906842624, 99] output = list(execute_program(memory.copy(), [])) assert len(output) == 1 assert output[0] == 1125899906842624
def __init__(self, memory, grid_size): self.grid = [[0] * grid_size for __ in range(grid_size)] self.x = grid_size // 2 self.y = grid_size // 2 # mark starting position in the maze self.grid[self.y][self.x] = 4 self.input_buffer = [] self.program = execute_program(memory, self.input_buffer) self.max_depth = 0 self.oxygen_found = False self.path_to_oxygen = []
def play_game(grid: list, memory: list) -> int: input_buffer = [0] game_program = execute_program(memory, input_buffer) score = 0 paddle_x = None ball_x = None frame = 0 while True: try: x = next(game_program) y = next(game_program) id = next(game_program) except StopIteration: break if x == -1 and y == 0: score = id continue grid[y][x] = id if id == 3: paddle_x = x if id == 4: if x != ball_x and ball_x: # execute when the ball moved, but not when it is being # drawn for the first time if paddle_x < x: input_buffer.append(1) elif paddle_x > x: input_buffer.append(-1) else: input_buffer.append(0) save_grid(frame, score, grid) frame += 1 ball_x = x # save the winning screen save_grid(frame, score, grid) return score
def run_feedback_loop(memory, phases): memories = [memory.copy() for __ in range(5)] input_buffers = [[] for __ in range(5)] amplifiers = [] value = 0 init_phase = True for amp in cycle(range(5)): if init_phase: input_buffers[amp].extend([phases[amp], value]) amplifiers.append(execute_program(memories[amp], input_buffers[amp])) else: input_buffers[amp].append(value) if amp == 4: init_phase = False try: value = next(amplifiers[amp]) except StopIteration: return value
def test_execute_program_day_7_solution(input_day_7): output = list(execute_program(input_day_7.copy(), [2, 0])) assert output == [3] output = list(execute_program(input_day_7.copy(), [0, 3])) assert output == [27] output = list(execute_program(input_day_7.copy(), [1, 27])) assert output == [688] output = list(execute_program(input_day_7.copy(), [4, 688])) assert output == [41316] output = list(execute_program(input_day_7.copy(), [3, 41316])) assert output == [206580] output = list( execute_program( input_day_7.copy(), [8, 16, 69, 558, 2242, 8974, 35916, 35922, 71854, 574848, 1149703 ])) assert output == [ 32, 138, 559, 2243, 8976, 35917, 35923, 71855, 1149696, 2299406 ]
from itertools import product from intcode_computer import execute_program if __name__ == '__main__': # Part 1 with open('puzzle_2_input') as f: memory = [int(elem) for elem in f.read().split(',')] memory[1:3] = [12, 2] __ = list(execute_program(memory, [])) print(f'Solution: {memory[0]}') # Part 2 with open('puzzle_2_input') as f: initial_state = [int(elem) for elem in f.read().split(',')] # brute force search for the desired output of 19690720 for noun, verb in product(range(100), range(100)): memory = initial_state[:] memory[1:3] = [noun, verb] __ = list(execute_program(memory, [])) if memory[0] == 19690720: print(f'Solution: {100*noun + verb}') break
def get_output_signal(memory, phases): value = 0 for phase in phases: value = next(execute_program(memory.copy(), [phase, value])) return value
def test_execute_program_day_5_examples(): # opcode 3 and 4 memory = [3, 0, 4, 0, 99] output = list(execute_program(memory, [68])) assert output == [68] assert memory == [68, 0, 4, 0, 99] # opcode 2, position + immediate memory = [1002, 4, 3, 4, 33] output = list(execute_program(memory, [])) assert output == [] assert memory == [1002, 4, 3, 4, 99] # opcode 5 immediate, test 0 is non-zero memory = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] input_buffer = [0] output = list(execute_program(memory, input_buffer)) assert output == [0] # opcode 5 immediate, test 68 is non-zero memory = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] input_buffer = [68] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 6, test 0 is zero memory = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9] input_buffer = [0] output = list(execute_program(memory, input_buffer)) assert output == [0] # opcode 6, test 68 is zero memory = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9] input_buffer = [68] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 7, test 5 < 8 memory = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] input_buffer = [5] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 7, test 8 < 8 memory = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] input_buffer = [8] output = list(execute_program(memory, input_buffer)) assert output == [0] # opcode 7 immediate, test 5 < 8 memory = [3, 3, 1107, -1, 8, 3, 4, 3, 99] input_buffer = [5] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 7 immediate, test 8 < 8 memory = [3, 3, 1107, -1, 8, 3, 4, 3, 99] input_buffer = [8] output = list(execute_program(memory, input_buffer)) assert output == [0] # opcode 8, test 8 == 8 memory = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] input_buffer = [8] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 8, test 5 == 8 memory = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] input_buffer = [5] output = list(execute_program(memory, input_buffer)) assert output == [0] # opcode 8 immediate, test 8 == 8 memory = [3, 3, 1108, -1, 8, 3, 4, 3, 99] input_buffer = [8] output = list(execute_program(memory, input_buffer)) assert output == [1] # opcode 8 immediate, test 5 == 8 memory = [3, 3, 1108, -1, 8, 3, 4, 3, 99] input_buffer = [5] output = list(execute_program(memory, input_buffer)) assert output == [0] # input below 8 memory = [ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 ] input_buffer = [6] output = list(execute_program(memory, input_buffer)) assert output == [999] # input 8 memory = [ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 ] input_buffer = [8] output = list(execute_program(memory, input_buffer)) assert output == [1000] # input below 8 memory = [ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 ] input_buffer = [68] output = list(execute_program(memory, input_buffer)) assert output == [1001]
def test_execute_program_day_9_solution(input_day_9): output = list(execute_program(input_day_9.copy(), [1])) assert output == [4006117640] output = list(execute_program(input_day_9.copy(), [2])) assert output == [88231]
def test_execute_program_day_5_solution(input_day_5): output = list(execute_program(input_day_5.copy(), [1])) assert output == [0, 0, 0, 0, 0, 0, 0, 0, 0, 16348437] output = list(execute_program(input_day_5.copy(), [5])) assert output == [6959377]
from intcode_computer import execute_program if __name__ == '__main__': with open('puzzle_5_input') as f: initial_memory = [int(elem) for elem in f.read().split(',')] # Part 1 outputs = execute_program(initial_memory.copy(), [1]) print(list(outputs)) # Part 2 outputs = execute_program(initial_memory.copy(), [5]) print(list(outputs))
from intcode_computer import execute_program if __name__ == '__main__': with open('puzzle_9_input') as f: memory = [int(elem) for elem in f.read().split(',')] # Part 1 outputs = list(execute_program(memory.copy(), [1])) print(f'Solution: {outputs[0]}') # Part 2 outputs = list(execute_program(memory.copy(), [2])) print(f'Solution: {outputs[0]}')
def __init__(self, x: int, y: int, memory: list) -> None: self.x = x self.y = y self.direction = (0, -1) self.input = [] self.program = execute_program(memory, self.input)
from intcode_computer import execute_program if __name__ == '__main__': with open('puzzle_19_input') as f: memory = [int(elem) for elem in f.read().split(',')] result = sum( next(execute_program(memory.copy(), [x, y])) for y in range(50) for x in range(50)) print(f'Solution: {result}') for y in range(1450, 2000): for x in range(1200, 1300): if next(execute_program(memory.copy(), [x, y])) \ and next(execute_program(memory.copy(), [x + 99, y])) \ and next(execute_program(memory.copy(), [x, y + 99])): print(f'Solution: {x*10000 + y}') break else: continue break # with open('img/puzzle_19_image_raw', 'wb') as f: # for y in range(1570): # print(y) # f.write(bytes(next(execute_program(memory.copy(), [x, y])) # for x in range(1330)))