class Robot: def __init__(self, code, initial_panels={}) -> None: self.__robot = IntCode(code) self.panels = initial_panels def run(self): direction = Direction.UP location = [0, 0] while not self.__robot.completed: self.__robot.input = self.panels.get(str(location), 0) self.__robot.run(until_output = True) self.panels[str(location)] = self.__robot.output self.__robot.run(until_output = True) direction = direction + (-1 if self.__robot.output == 0 else 1) while direction < 0: direction += 4 while direction > 3: direction -= 4 if direction == Direction.UP: location[1] -= 1 elif direction == Direction.DOWN: location[1] += 1 elif direction == Direction.LEFT: location[0] -= 1 elif direction == Direction.RIGHT: location[0] += 1
def find_noun_verb(input_code, target_output): for noun_candidate in range(100): for verb_candidate in range(100): new_int_code = list(input_code) new_int_code[1] = noun_candidate new_int_code[2] = verb_candidate int_code_machine = IntCode(new_int_code) int_code_machine.run() if int_code_machine.code is not None and \ int_code_machine.code[0] == target_output: return noun_candidate, verb_candidate
def solve(): f = open('input/day02.txt', 'r') cs_int_code = f.read() f.close() orig_int_code = list(map((lambda x: int(x)), cs_int_code.split(','))) int_code_input = list(orig_int_code) int_code_input[1] = 12 int_code_input[2] = 2 int_code = IntCode(int_code_input) int_code.run() print('Part 1') print(' After running, position 0 contains: {}'.format(int_code.code[0])) (noun, verb) = find_noun_verb(list(orig_int_code), 19690720) print() print('Part 2') print(' Input noun is {} and verb is {}'.format(noun, verb))
def solve(): f = open("input/day09.txt", "r") raw_code = f.read().strip('\n') f.close() orig_int_code = list(map((lambda x: int(x)), raw_code.split(','))) int_code = IntCode(list(orig_int_code)) int_code.input = 1 int_code.run() print("Part 1") print(f" Outputs after run: {int_code.outputs}") int_code.reset() int_code.input = 2 int_code.run() print() print("Part 2") print(f" Outputs after run: {int_code.outputs}")
class ArcadeGame: BLOCK_TYPES = {0: "", 1: "|", 2: "#", 3: "=", 4: "O"} def __init__(self, code): self.__game = IntCode(code) self.score = 0 self.__tiles = {} def run(self): while not self.__game.completed: self.__tiles = {} self.__game.run() for triplet in partition(self.__game.outputs, 3): x = triplet[0] y = triplet[1] code = triplet[2] location = (x, y) if location == (-1, 0): self.score = code else: self.__tiles[location] = self.BLOCK_TYPES[code] ball = self.locations_of_tile("O")[0] paddle = self.locations_of_tile("=")[0] if ball[0] < paddle[0]: self.__game.input = -1 elif ball[0] > paddle[0]: self.__game.input = 1 else: self.__game.input = 0 if self.count_tiles("#") == 0: return def locations_of_tile(self, tile_char): return [k for k, v in self.__tiles.items() if v == tile_char] def count_tiles(self, tile_char): return list(self.__tiles.values()).count(tile_char)
def solve(): f = open('input/day05.txt', 'r') cs_int_code = f.read() f.close() orig_int_code = list(map((lambda x: int(x)), cs_int_code.split(','))) int_code_input = list(orig_int_code) int_code = IntCode(int_code_input) int_code.input = 1 int_code.run() print('Part 1') print(' After running, the output is: {}'.format(int_code.output)) int_code_input = list(orig_int_code) int_code = IntCode(int_code_input) int_code.input = 5 int_code.run() print() print('Part 2') print(' After running, the output is: {}'.format(int_code.output))
def make_amplifier(orig_code, phase_code): int_code = IntCode(orig_code) int_code.input = phase_code int_code.run() return int_code
def __init__(self, code, initial_panels={}) -> None: self.__robot = IntCode(code) self.panels = initial_panels
def __init__(self, code): self.__game = IntCode(code) self.score = 0 self.__tiles = {}