def solve(instruction, input=[]): computer = Computer(instruction) for i in input: computer.put(i) answer = [] running = True while running is True: try: answer.append(computer.eval()) except StopIteration: running = False return answer
def solve2(instruction): screens = [] instruction[0] = 2 computer = Computer(instruction) running = True screenSize = 50 screen = [[0 for i in range(38)] for j in range(20)] next_input = 0 score = 0 while running is True: try: computer.clear() computer.put(screen_input(screen)) x = computer.eval() y = computer.eval() tileId = computer.eval() if x == -1 and y == 0: score = tileId print(f"Score : {score}") else: screen[y][x] = tileId screens.append(screen) except StopIteration: running = False return score
def solve(instruction): computer = Computer(instruction) running = True screenSize = 50 screen = [[0 for i in range(38)] for j in range(20)] while running is True: try: x = computer.eval() y = computer.eval() tileId = computer.eval() screen[y][x] = tileId except StopIteration: running = False return sum([Counter(line)[2] for line in screen])
def manual(input): global tank_location, walls plan_size = 10 plan = [[0 for i in range(plan_size)] for y in range(plan_size)] position = (math.floor(plan_size / 2), math.floor(plan_size / 2)) computer = Computer(input) while True: print_grid(plan, position) newDirection = manual_input(position) computer.put(newDirection) newState = computer.eval() if newState == 0: walls.add(getNextPos(position, newDirection)) elif newState == 2: position = getNextPos(position, newDirection) tank_location = getNextPos(position, newDirection) else: position = getNextPos(position, newDirection)
def doMove(computer, direction, plan, pos): global walls nextPos = getNextPos(pos, direction) #alreadyVisitedOrWall = plan[nextPos[1]][nextPos[0]] != 0 if nextPos in walls: return sys.maxsize newComputer = Computer(computer.memory, computer.pc, computer.rb) newComputer.put(direction) newState = newComputer.eval() if newState == 0: walls.add(nextPos) plan[nextPos[1]][nextPos[0]] = 2 # it's a wall return sys.maxsize elif newState == 2: return 1 else: plan[pos[1]][pos[0]] = 1 return 1 + min([ doMove(newComputer, x, plan, getNextPos(pos, direction)) for x in getRangeIgnoringPreviousPosition(direction) ])
def solve(input): finalResult = 0 for order in permutations(range(0, 5)): last_amp = 0 for i in order: computer = Computer(input) computer.put(i) computer.put(last_amp) last_amp = computer.eval() finalResult = max(finalResult, last_amp) return finalResult
def solve2(initialInstruction): result = 0 for ordre in permutations(range(5, 10)): amps = [] for x in range(len(ordre)): amps.append(Computer(initialInstruction)) amps[x].put(ordre[x]) amp = 0 running = True while running is True: for x in range(5): amps[x].put(amp) try: amp = amps[x].eval() except StopIteration: running = False result = max(result, amp) return result
def solve(input): """ Commands: - 1 : North - 2 : South - 3 : West - 4 : East status codes: - 0 : hit a wall. Position not changed - 1 : The repair droid has moved one step in the requested direction - 2 : The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system part 1 : what is the fewest number of movement required """ plan_size = 1000 # sys.setrecursionlimit(10000) plan = [[0 for i in range(plan_size)] for y in range(plan_size)] position = (math.floor(plan_size / 2), math.floor(plan_size / 2)) computer = Computer(input) return min(doMove(computer, 1, plan, position), doMove(computer, 2, plan, position), doMove(computer, 3, plan, position), doMove(computer, 4, plan, position))
def solve2(instruction): computer = Computer(instruction) """ 0: up, 1: right, 2: down, 3: left """ robot_direction = 0 x = 50 y = 50 panel = [[0 for j in range(100)] for i in range(100)] panel[x][y] = 1 running = True panelPainted = {} while running is True: try: computer.put(panel[y][x]) new_color = computer.eval() # Count panels if (x, y) in panelPainted: panelPainted[(x, y)] += 1 else: panelPainted[(x, y)] = 1 panel[y][x] = new_color new_direction = computer.eval() # orientation if new_direction == 0: robot_direction = (robot_direction - 1) % 4 else: robot_direction = (robot_direction + 1) % 4 # movement if robot_direction == 0: y = y - 1 elif robot_direction == 2: y = y + 1 elif robot_direction == 1: x = x + 1 elif robot_direction == 3: x = x - 1 except StopIteration: running = False plt.imshow(panel, 'gray') plt.show() return panel