def part_2(data): data[0] = 2 game = IntCode(data, []) game.calculate() i = 0 cnt = 0 GRID = [[' . ' for i in range(44)] for j in range(23)] while not game.is_halted() and cnt < 10: screen = game.get_output(-1) i = 0 while i < len(screen): [x, y, tile] = screen[i:i + 3] if x == -1 and y == 0: print(tile) i += 3 continue GRID[y][x] = t[tile] i += 3 disp(GRID) game.unpause([0, 0, 0]) game.calculate() cnt += 1 print('END OF PART2') return
def part_2(data): GRID = [['.' for i in range(100)] for j in range(100)] code = IntCode(data) r_pos = (0, 0) r_d = 'N' panels = {r_pos: 1} while not code.is_halted(): try: in_data = [panels[r_pos]] except: in_data = [0] code.unpause(in_data) code.calculate() [color, d] = code.get_output(2) panels[r_pos] = color GRID[50 + r_pos[1]][50 + r_pos[0]] = '#' if color == 1 else '.' r_pos, r_d = get_new_pos(r_pos, r_d, d) disp(GRID) print('END OF PART2') #CBLPJZCU return
def part_1(data): code = IntCode(data) r_pos = (0, 0) r_d = 'N' panels = {r_pos: 0} painted = set() while not code.is_halted(): try: in_data = [panels[r_pos]] except: in_data = [0] code.unpause(in_data) code.calculate() [color, d] = code.get_output(2) painted |= {r_pos} panels[r_pos] = color r_pos, r_d = get_new_pos(r_pos, r_d, d) print(len(painted)) print('END OF PART1') return
# Read input memory = [] with open("day21/input.txt") as f: for line in f.readlines(): memory.extend([int(x) for x in line.split(",")]) # Part 1 # spring_prog = "OR A J\nAND B J\nAND C J\nNOT J J\nAND D J\nWALK\n" # Part 2 spring_prog = "NOT H J\nOR C J\nAND B J\nAND A J\nNOT J J\nAND D J\nRUN\n" output_chars = [] springbot = IntCode(memory, 0) for ch in spring_prog: springbot.write_input(str(ord(ch))) springbot.run() output = int(springbot.read_output()) while not springbot.is_halted() and output < 256: output_chars.append(chr(output)) springbot.run() if not springbot.is_halted(): output = int(springbot.read_output()) if output > 255: print(f"Damage: {output}") else: print(f"Hull: {''.join(output_chars)}")
# game_data[(x_pos, y_pos)] = tile_id # tile_count = [0] * 5 # for id in game_data.values(): # tile_count[id] += 1 # print(f"Part 1: Count of '2's = {tile_count[2]}") # # Part 2 memory[0] = 2 intcode = IntCode(memory, 0) # intcode.reset() game_field = [[" " for _ in range(38)] for _ in range(38)] intcode.run() while not intcode.is_halted(): x_pos = int(intcode.read_output()) intcode.run() y_pos = int(intcode.read_output()) intcode.run() tile_id = int(intcode.read_output()) intcode.run() # update_game_data(game_data, x_pos, y_pos, tile_id) # draw_game_field(game_data, game_field) if (x_pos, y_pos) == (-1, 0): print(f"Score: {tile_id}") else: update_game_data(game_field, x_pos, y_pos, tile_id) for y in range(22): print(f"{''.join(game_field[y])}")
robot = IntCode(memory, 0) # We start at (0,0) facing up x, y = 0, 0 direction = 1 # left=0, up=1, right=2, down=3 hull = defaultdict(int) xmove = [-1, 0, 1, 0] ymove = [0, -1, 0, 1] # Part 2 hull[(0, 0)] = 1 xmin, xmax, ymin, ymax = 0, 0, 0, 0 # Run and get two pieces of output while not robot.is_halted(): # Provide our current color robot.write_input(hull[x, y]) # Run until we get the new color output robot.run() if robot.is_halted(): break color = int(robot.read_output()) # Run again until we get the new direction robot.run() turn = (int(robot.read_output()) * 2) - 1 # Paint the hull hull[(x, y)] = color