def Day02(program_file, part1=True, target=None): if part1: VM = IntcodeComputer(program_file) VM.override({1: 12, 2: 2}) VM.run() return VM.P[0] for noun in range(100): for verb in range(100): VM = IntcodeComputer(program_file) VM.override({1: noun, 2: verb}) VM.run() if VM.P[0] == target: return (100 * noun) + verb
def Day13(program_file, grid_init, part2=False, plot=False): def get_joystick(): """Simple AI tracking the ball position with the paddle.""" return np.sign(ball - paddle) # Set up grid (determined from wall positions) R, C = grid_init G = [[0 for _ in range(C)] for _ in range(R)] # Initialise Intcode program if part2: VM = IntcodeComputer(program_file, input=get_joystick) VM.override({0: 2}) # Buffer for score display at the top of screen row_buffer = 2 else: VM = IntcodeComputer(program_file) row_buffer = 0 # Carry on receiving output from program until it halts while True: c, r, tile = [VM.run() for _ in range(3)] if c == -1: score = tile if VM.halted: break elif tile == 3: paddle = c # Save paddle x-position elif tile == 4: ball = c # Save ball x-position G[r + row_buffer][c] = tile # Matplotlib plot if plot: plt.figure() plt.imshow(G, cmap='gist_stern') plt.axis('off') if part2: plt.text( 42.2, 0.6, "Score: " + str(score), fontsize=9, family='Consolas', color='white', horizontalalignment='right' ) if part2: return score return sum(x.count(2) for x in G)
def Day17(program_file, part1=True, debug=False, plot=None, routine_file=None): # Constants MAP = {'.': 0, '#': 1, '^': 2, 'v': 2, '<': 2, '>': 2} if routine_file is None: ROUTINE = get_routine('routine.txt') else: ROUTINE = get_routine(routine_file) def get_input(): """Function used as input to Intcode VM.""" out = next(ROUTINE) if debug: print(chr(out), end='') return out def scaffold_grid(scaffolds): """Convert scaffolds string into numeric grid.""" return [[MAP[ch] for ch in line] for line in scaffolds.strip('\n').split('\n')] def get_intersections(scaffolds): """Return set of coordinates of all scaffold intersections.""" G = scaffold_grid(scaffolds) R, C = len(G), len(G[0]) intersections = set() dirs = [ (-1, 0), # Up (0, 1), # Right (1, 0), # Down (-1, 0) # Left ] for r, c in itertools.product(range(1, R - 1), range(1, C - 1)): if G[r][c] == MAP['#']: if all(G[r + dr][c + dc] == MAP['#'] for (dr, dc) in dirs): intersections.add((r, c)) return intersections def plot_scaffolds(scaffolds, intersections=None): """Matplotlib plot of scaffolds from string representation.""" G = scaffold_grid(scaffolds) colours = [ 'white', # Space 'midnightblue', # Scaffold 'dodgerblue', # Robot ] if intersections is not None: # Append intersection colour to colour map colours.append('lightsteelblue') intersection = max(MAP.values()) + 1 # Amend intersection values for r, c in intersections: G[r][c] = intersection # Show plot plt.figure(figsize=(8, 8)) plt.imshow(G, cmap=ListedColormap(colours)) plt.axis('off') return None # Initialise Intcode program VM = IntcodeComputer(program_file, input=get_input) if not part1: VM.override({0: 2}) scaffolds = debug_out = "\n" while not VM.halted: out = VM.run() if out not in range(128): break scaffolds += chr(out) # Full debug output if debug: debug_out += chr(out) if out == 10: print(debug_out, end='') debug_out = "" if part1: intersections = get_intersections(scaffolds) # Plot if plot: plot_scaffolds(scaffolds, intersections) return sum(r * c for r, c in intersections) return out