def get_map(cls, program: typing.List[int], draw_mode=False) -> "Map": computer = IntComputer(program, wait_after_output=True, wait_for_input=True) visited = defaultdict(int) droid_map = Map() steps = 0 while not computer.finished and steps < 6000: direction = droid_map.get_best_direction(visited) #direction = int(input('Input: ')) steps += 1 computer.inputs = [direction] computer.run() while not computer.waiting: computer.run() output = computer.output if output == OUTPUT_WALL: droid_map.wall_hit([NORTH, SOUTH, WEST, EAST][direction - 1]) if output == OUTPUT_FREE: [droid_map.north, droid_map.south, droid_map.west, droid_map.east][direction - 1]() droid_map.set_position(droid_map.position, FREE) if output == OUTPUT_OXYGEN: [droid_map.north, droid_map.south, droid_map.west, droid_map.east][direction - 1]() droid_map.set_position(droid_map.position, OXYGEN) visited[droid_map.position] += 1 if draw_mode: droid_map.render() return droid_map
def part_2(): grid = defaultdict(lambda _: '.') dim = 100 cur_x, cur_y = 100, 100 while True: # Do the corner bot = IntComputer(program, wait_for_input=False, wait_after_output=False) bot.inputs = [cur_x, cur_y] grid[(cur_x, cur_y)] = '#' if bot.run() else '.' del bot # do the edges for i in range(1, dim): bot_x = IntComputer(program, wait_for_input=False, wait_after_output=False) bot_y = IntComputer(program, wait_for_input=False, wait_after_output=False) bot_x.inputs = [cur_x - i, cur_y] bot_y.inputs = [cur_x, cur_y - i] grid[(cur_x - i, cur_y)] = '#' if bot_x.run() else '.' grid[(cur_x, cur_y - i)] = '#' if bot_y.run() else '.' del bot_x, bot_y #Check if we've achieved our goal for coord, state in grid.items(): if coord[1] < 100: continue #start from a tractor spot, check that it's on the left edge, #go up and right 100 units to find the other corner and make sure it's a tractor spot, #and make sure that spot is a right edge if grid[coord] == '#' \ and grid[(coord[0] - 1, coord[1])] == '.' \ and grid[(coord[0] + 100, coord[1] - 100)] == '#' \ and grid[(coord[0] + 101, coord[1] - 100)] == '.': return 10000 * coord[0] + (coord[1] - 100) cur_x += 1 cur_y += 1 dim += 1
def part_1(): total_pull_nodes = 0 graph = '' for y in range(50): for x in range(50): bot = IntComputer(program, wait_for_input=False, wait_after_output=False) bot.inputs = [x, y] output = bot.run() total_pull_nodes += output graph += '#' if output == 1 else '.' graph += '\n' print(graph) print(f"The total number of nodes that pull is: {total_pull_nodes} .")
def part_2(): program = fh.csv_to_list('day17-input.txt') program[0] = 2 ASCII = IntComputer(program, wait_after_output=False, wait_for_input=False) def to_ascii(l): return [ord(c) for c in l] # R, 8, R, 10, R, 10 mov_func_A = ['R', ',', '8', ',', \ 'R', ',', '1', '0', ',', \ 'R', ',', '1', '0', '\n'] mov_func_A = to_ascii(mov_func_A) # R, 4, R, 8, R, 10, R, 12 mov_func_B = ['R', ',', '4', ',', \ 'R', ',', '8', ',', \ 'R', ',', '1', '0', ',', \ 'R', ',', '1', '2', '\n'] mov_func_B = to_ascii(mov_func_B) # R, 12, R, 4, L, 12, L, 12 mov_func_C = ['R', ',', '1', '2', ',', \ 'R', ',', '4', ',', \ 'L', ',' ,'1', '2', ',', 'L', ',', '1', '2', '\n'] mov_func_C = to_ascii(mov_func_C) # A, B, A, C, A, B, C, A, B, C main_func = ['A', ',', 'B', ',', \ 'A', ',', 'C', ',', \ 'A', ',', 'B', ',', \ 'C', ',', 'A', ',', \ 'B', ',', 'C', '\n'] main_func = to_ascii(main_func) # y or n if you want to see a continuous feed of what's happening cont_feed = ['n', '\n'] cont_feed = to_ascii(cont_feed) # input order: MAIN, MOV A, MOV B, MOV C, ASCII.inputs = main_func + mov_func_A + mov_func_B + mov_func_C + cont_feed return ASCII.run()
return grid[current_pos] else: return 0 def concatenate(l): s = '' for i in l: s += i return s while True: current_color = getColor() #LOG.write(f"Currently at: {current_pos}, Color: {current_color}, Facing: {current_heading}\n") robot.inputs = [current_color] next_color = robot.run() #LOG.write(f"\tNext color: {next_color}\n") if robot.finished: # After the last input, before halting, the robot pauses. Need to check if it's halted print("DONE") break turn = robot.run() #LOG.write(f"\tTurn: {turn}\n") grid[current_pos] = next_color current_heading = turnRobot(current_heading, turn) #LOG.write(f"\tPainted here: {grid[current_pos]}, Turned toward: {current_heading}\n") current_pos = moveRobot(current_pos, current_heading) cols, rows = zip(*grid.keys()) min_cols = -min(cols) if min(cols) < 0 else 0