from machine import IntcodeMachine program = open('17.txt').read() machine = IntcodeMachine(program) machine.a[0] = 2 outputs = '''#######...##### #.....#...#...# #.....#...#...# ......#...#...# ......#...###.# ......#.....#.# ^########...#.# ......#.#...#.# ......######### ........#...#.. ....#########.. ....#...#...... ....#...#...... ....#...#...... ....#####......'''.split('\n') outputs = ''.join([chr(code) for code in machine.run()]).split('\n')[:-3] directions = ['>', 'v', '<', '^'] def get_intersections(outputs): intersections = set() for y in range(len(outputs)): for x in range(len(outputs[0])):
def get_value(x, y): machine = IntcodeMachine(program) machine.inputs = [x, y] machine.run() return machine.outputs.pop(0)
from machine import IntcodeMachine program = open('15.txt').read() machine = IntcodeMachine(program) start_position = (0, 0) oxygen_position = None opposite_movements = { 1: 2, 2: 1, 3: 4, 4: 3, } current_position = start_position backtrack = [] walls = set() dead_ends = set() walkable = set({(0, 0)}) all_positions = set() def get_shortest_path(origin, target): global walkable to_check = [origin] seen_from = {origin: None} while to_check: cx, cy = to_check.pop(0)
from itertools import permutations from machine import IntcodeMachine program = open('7.txt').read() max_output = None for modes in permutations(range(5, 10)): output = 0 machines = [IntcodeMachine(program, inputs=[mode]) for mode in modes] is_running = True while is_running: for machine in machines: machine.inputs.append(output) outputs = machine.run() if not outputs: is_running = False break output = outputs[0] if max_output is None or output > max_output: max_output = output print(max_output, modes)
from machine import IntcodeMachine program = open('13.txt').read() machine = IntcodeMachine(program) machine.run() print([x for x in machine.outputs[2::3]].count(2))
from machine import IntcodeMachine program = open('17.txt').read() machine = IntcodeMachine(program) outputs = ''.join([chr(code) for code in machine.run()]).split('\n')[:-2] def get_intersections(outputs): intersections = set() for y in range(len(outputs)): for x in range(len(outputs[0])): if outputs[y][x] == '#': n = 0 for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: if 0 <= x + dx < len(outputs[0]) and 0 <= y + dy < len( outputs) and outputs[y + dy][x + dx] == '#': n += 1 if n == 4: intersections.add((x, y)) return intersections def print_map(outputs): intersections = get_intersections(outputs) lines = [] for y in range(len(outputs)): line = [] for x in range(len(outputs[0])): if (x, y) in intersections: line.append('O')
from machine import IntcodeMachine program = open('13.txt').read() objects = [' ', '#', '□', '▬', 'o'] w, h = 46, 26 # educated guess machine = IntcodeMachine(program) machine.a[0] = 2 # put the coins in machine.run() def draw(screen): global objects lines = [] for y in range(h): line = [] for x in range(w): line.append(objects[screen.get((x, y), 0)]) lines.append(''.join(line)) print('\n'.join(lines)) def analyse(screen): ball_x = paddle_x = None number_of_blocks = 0 for (x, y), o in screen.items(): if o == 4: ball_x = x elif o == 3: