def paint(code, painted=[]): hull = defaultdict(int) for point in painted: hull[point] = 1 robot = IntCode(code) direction = 0 point = (0, 0) while True: input = [hull[point]] paint = robot.execute(input) if paint is None: break turn = robot.execute() hull[point] = paint if turn == 0: turn = -1 direction = (direction + turn) % len(DIRECTIONS) move = DIRECTIONS[direction] point = (point[0] + move[0], point[1] + move[1]) return (hull)
def execute(data, noun, verb): input = data.copy() input[1] = noun input[2] = verb i = IntCode(input) i.execute() return i.memory[0]
def part1(code): """ >>> part1(read_input()) 2350741403 """ return IntCode(code).execute([1])
def explore(code): i = IntCode(code) location = (0, 0) seen = {} seen[location] = "S" def search(location): for movement in Movement: next_location = (location[0] + movement.x, location[1] + movement.y) if next_location in seen: continue output = i.execute([movement.forward]) if output == 0: seen[next_location] = '#' elif output == 1: seen[next_location] = ' ' search(next_location) i.execute([movement.back]) elif output == 2: seen[next_location] = '*' search(next_location) i.execute([movement.back]) search(location) return seen
def part2(code): """ >>> part2(read_input()) 53088 """ return IntCode(code).execute([2])
def part2(data): """ >>> part2(read_input()) 3508186 """ return IntCode(data).execute([5])
def part1(data): """ >>> part1(read_input()) 12234644 """ return IntCode(data, [1]).run()[-1]
def part1(code): """ >>> part1(read_input()) 304 """ i = IntCode(code, memory_size=10000) screen = defaultdict(int) while True: x = i.execute() if x is None: break y = i.execute() square = i.execute() screen[(x, y)] = square return sum(1 for key in screen.keys() if screen[key] == 2)
def calc_signal(code, phases): """ >>> calc_signal([3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0], [4,3,2,1,0]) 43210 >>> calc_signal([3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0], [0, 1, 2, 3, 4]) 54321 """ signal = 0 for phase in phases: signal = IntCode(code).execute([phase, signal]) return signal
def calc_feedback(code, phases): """ >>> calc_feedback([3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5], [9,8,7,6,5]) 139629729 >>> calc_feedback([3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10], [9,7,8,5,6]) 18216 """ thrusters = [IntCode(code, [phase]) for phase in phases] signal = 0 for thruster in itertools.cycle(thrusters): output = thruster.execute([signal]) if output is None: return signal signal = output
def part2(code, draw): """ >>> part2(read_input(), False) 14747 """ code[0] == 2 score = 0 i = IntCode(code, memory_size=10000) screen = defaultdict(int) if draw: stdscr = curses.initscr() curses.curs_set(0) stdscr.addstr(24, 0, "Score:") bat_x = None ball_x = None playing = False while True: x = i.execute() if x is None: if playing: break playing = True continue y = i.execute() square = i.execute() if x == -1 and y == 0: score = square if draw: stdscr.addstr(24, 7, str(score)) continue if square == 3: bat_x = x elif square == 4: ball_x = x if not playing: joystick = 0 elif bat_x < ball_x: joystick = 1 elif bat_x > ball_x: joystick = -1 else: joystick = 0 i.add_input(joystick) screen[(x, y)] = square if draw: pixel = [" ", "█", "▓", "▔", "•"][square] stdscr.addch(y, x, pixel) stdscr.refresh() if (square == 3 or square == 4): time.sleep(0.007) if draw: curses.endwin() curses.curs_set(1) return score