def get_first_x(self, y: int, max_point: int)->int: """ Return the first beam found in line y in between 0 max_point """ for x in range(max_point): explorebot = Computer(read_input(self.path)) code = explorebot.run((x, y)) if code == 1: return x
def __main__(): with open("2019/resources/txt/day9.txt") as txt: instructions = txt.read() instructions = instructions.split(',') instructions = [int(x) for x in instructions] computer = Computer(instructions) # computer.input_1 = 1 computer.run()
def part1(self, height, width): screen = {} for i in range(height): for j in range(width): explorebot = Computer(read_input(self.path)) code = explorebot.run((j, i)) if code == 99: return screen elif code == 1: screen[(i, j)] = 1 else: screen[(i, j)] = 0 setup_screen(screen, height, width)
def __main__(): with open("2019/resources/txt/day2.txt") as txt: instructions = txt.read() instructions = instructions.split(',') for i in range(len(instructions)): instructions[i] = int(instructions[i]) # part one instructions[1] = 12 instructions[2] = 2 comp1 = Computer(instructions) comp1.run() print(comp1.program[0]) # part two for x in range(100): for y in range(100): instructions[1] = x instructions[2] = y comp2 = Computer(instructions) comp2.run() if comp2.program[0] == 19690720: print(x, y) break
def __main__(): with open("2019/resources/txt/day5.txt") as txt: instructions = txt.read() instructions = instructions.split(',') instructions = [int(x) for x in instructions] # part 1 comp1 = Computer(instructions) comp1.input_1 = 1 comp1.run() # part 2 comp2 = Computer(instructions) comp2.input_1 = 5 comp2.run()
def robot_grid(input): """ Take care of robot errance """ robot_computer = Computer(input) screen = {} # haut, droite, bas, gauche robotdir = [(1, 0), (0, 1), (-1, 0), (0, -1)] robot_translate = {(0, 1): 4, (-1, 0): 2, (0, -1): 3, (1, 0): 1} max_y = 0 max_x = 0 robotpos = (0, 0) obstructed = set() next = 0 count = 0 while count < 5001: prev_next = next next = normal_next(robotpos, robotdir, obstructed, next) code = robot_computer.run(robot_translate[robotdir[next]]) y = robotpos[0] + robotdir[next][0] x = robotpos[1] + robotdir[next][1] if y > max_y: max_y = y if x > max_x: max_x = x if code == 0: obstructed.add((y, x)) screen[(y, x)] = "###" next = prev_next else: if robotpos in screen: if screen[robotpos] != " X": screen[robotpos] = " " else: screen[robotpos] = " " robotpos = (y, x) if code == 2: print(robotpos) screen[robotpos] = " X" else: screen[robotpos] = " D" count += 1 return screen
def robot_grid(input, first): """ Take care of robot errance """ robot_computer = Computer(input) robotdir = [(1, 0), (0, 1), (-1, 0), (0, -1)] pointer_position = 0 max_y = 0 max_x = 0 robotpos = (0, 0) ship = {(0, 0): Paint_square(first, (0, 0))} while True: if robotpos in ship: see = ship[robotpos].color else: see = 0 color_to_paint = robot_computer.run(see) facing = robot_computer.run() if color_to_paint == 99: break if robotpos not in ship: ship[robotpos] = Paint_square(color_to_paint, robotpos) else: ship[robotpos].color = color_to_paint if facing == 0: pointer_position += -1 else: pointer_position += 1 direction = pointer_position % 4 y = robotpos[0] + robotdir[direction][0] x = robotpos[1] + robotdir[direction][1] if y > max_y: max_y = y if x > max_x: max_x = x robotpos = (y, x) return ship, max_y, max_x
def explore_binary(self, start, end, max_point, goal): """ Approximative binary search to find where you have a certain amount of beams """ if start == end: return start point = start + (end - start) // 2 count_x = 0 for j in range(max_point): explorebot = Computer(read_input(self.path)) code = explorebot.run((j, point)) if code == 1: count_x += 1 if count_x < goal: return self.explore_binary(point +1, end, max_point, goal) elif count_x > goal: return self.explore_binary(start, point, max_point, goal) else: return point
def insert_credit(self): visited = set() route = [] robot_pos = (0, 0) next_input = "" game = Computer(read_input(self.intcode)) while True: state = game.run(next_input) next_input = "Iddle" print(state) if game == "Game over": break direction = [] for line in state.split('\n'): if "==" in line: name = line for key in self.dir_dict.keys(): if key in line: direction.append(key) if robot_pos not in self.ship_map: self.ship_map[robot_pos] = Room(name, robot_pos, set(direction)) visited.add(robot_pos) if not route: route = self.dfs_unvisited(visited, set(), robot_pos) if route is None: print(visited) print("all visited") print("Game over") break next_input = route.pop() robot_pos = (robot_pos[0] + self.dir_dict[next_input][0], robot_pos[1] + self.dir_dict[next_input][1])
def part2(self): ship_size = 100 min_y = self.explore_binary(500, 1000, 1200, ship_size * 2) while True: loop = False first_x = self.get_first_x(min_y + ship_size - 1, 1200) for y in range(ship_size): itera = min_y + ship_size - 1 - y explorebot = Computer(read_input(self.path)) code = explorebot.run((first_x, itera)) check = self.check_y(itera, first_x, ship_size) if not check or code != 1: print(min_y, "not good", y) min_y += 1 loop = True break if not loop: print(first_x, min_y) print((first_x) * 10000 + (min_y)) break
def part_a(instructions): phase_settings = get_phase_settings() best_score = 0 for i in range(len(phase_settings)): phase_setting = phase_settings[i] amp_a, amp_b, amp_c, amp_d, amp_e = Computer(instructions), Computer( instructions), Computer(instructions), Computer( instructions), Computer(instructions) amp_a.input_1 = phase_setting[0] amp_b.input_1 = phase_setting[1] amp_c.input_1 = phase_setting[2] amp_d.input_1 = phase_setting[3] amp_e.input_1 = phase_setting[4] amp_a.input_2 = 0 amp_a.run() amp_b.input_2 = amp_a.output_signal amp_b.run() amp_c.input_2 = amp_b.output_signal amp_c.run() amp_d.input_2 = amp_c.output_signal amp_d.run() amp_e.input_2 = amp_d.output_signal amp_e.run() if amp_e.output_signal > best_score: best_score = amp_e.output_signal print("Best score: ", best_score)
def __init__(self, intcodeList): self.brain = Computer(intcodeList, automaticMode=True) self.screen = Map() screenDictionary = { 0: ' ', 1: 'W', 2: 'B', 3: '=', 4: 'O', 16: 'X', 34: 'X' } self.screenRepresentation = Representation('screen', screenDictionary) self.score = 0 self.ballX = 0 self.paddleX = 0
def check_y(self, y: int, start: int, ship_size: int)->bool: """ Check if a line y, is in the beam at start position and start position + size of the ship """ explorebot = Computer(read_input(self.path)) code = explorebot.run((start, y)) if code != 1: return False explorebot = Computer(read_input(self.path)) code = explorebot.run((start+ship_size-1, y)) if code != 1: return False return True
intcodes.append(int(ints)) return intcodes def setup_screen(screen): for y in range(20): line = [] for x in range(40): if (x, y) not in screen: screen[(x, y)] = " " else: line.append(screen[x, y]) print("".join(line)) arcade = Computer(read_input()) code = 0 paddle_position = (0, 0) ball_position = (0, 0) x = 0 y = 0 block = 0 screen = {} max_x = float('-inf') max_y = float('-inf') while code != 99: x = arcade.run() y = arcade.run() code = arcade.run() if code == 0:
def part_b(instructions): phase_settings = get_phase_settings(True) best_score = 0 for i in range(len(phase_settings)): phase_setting = phase_settings[i] amp_a, amp_b, amp_c, amp_d, amp_e = Computer(instructions), Computer( instructions), Computer(instructions), Computer( instructions), Computer(instructions) amp_a.input_1 = phase_setting[0] amp_a.do_feedback = True amp_b.input_1 = phase_setting[1] amp_b.do_feedback = True amp_c.input_1 = phase_setting[2] amp_c.do_feedback = True amp_d.input_1 = phase_setting[3] amp_d.do_feedback = True amp_e.input_1 = phase_setting[4] amp_e.do_feedback = True initialised = False while True: amp_a.input_2 = 0 if not initialised else amp_e.output_signal amp_a.run() initialised = True amp_b.input_2 = amp_a.output_signal amp_b.run() amp_c.input_2 = amp_b.output_signal amp_c.run() amp_d.input_2 = amp_c.output_signal amp_d.run() amp_e.input_2 = amp_d.output_signal is_complete = amp_e.run() if is_complete: break if amp_e.output_signal > best_score: best_score = amp_e.output_signal print("Best score: ", best_score)
""" intcodes = [] with open(path, "r") as file: for x in file: s = x.split(",") for ints in s: intcodes.append(int(ints)) return intcodes network = {} first = {} for computer in range(50): intcode = read_input("input_Day_23.txt") network[computer] = Computer(intcode) first[computer] = True def run_network(network): queue = defaultdict(deque) while True: for adress, computer in network.items(): paquet = None if first[adress]: first[adress] = False paquet = computer.run([adress])