def get_tractor_pull(x, y): global memory drone = IntCode(memory, 0) drone.write_input(x) drone.write_input(y) drone.run() return int(drone.read_output())
def run_test(phases: Tuple, init_strength: int): strength = str(init_strength) # Loop through phases given for phase in phases: intcode = IntCode(memory, 0) intcode.write_input(phase) intcode.write_input(strength) intcode.run() strength = int(intcode.read_output()) # Return the output strength return int(strength)
def main1(): interpreter = IntCode() interpreter.load_program('input') interpreter.run_program(1) return interpreter.read_output()
# 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])}") # input()
if __name__ == "__main__": global intcode # Read input memory = [] with open("day17/input.txt") as f: for line in f.readlines(): memory.extend([int(x) for x in line.split(",")]) intcode = IntCode(memory, 0) scaffold = [] scaffold_line = [] intcode.run() while not intcode.is_halted(): char = int(intcode.read_output()) if char == 10: scaffold.append(scaffold_line.copy()) scaffold_line.clear() else: scaffold_line.extend(chr(char)) intcode.run() intersections = 0 for y in range(1, len(scaffold) - 2): scaffold_line = scaffold[y] for x in range(len(scaffold_line) - 1): if "".join(scaffold_line[x : x + 3]) == ".#.": scaffold_line_2 = scaffold[y + 1] if "".join(scaffold_line_2[x : x + 3]) == "###": scaffold_line_3 = scaffold[y + 2]
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 # Turn direction = (direction + turn) % 4 # Move x += xmove[direction] y += ymove[direction]