def Part2(input): outputs = [] phaseSets = permutations(5) for phase in phaseSets: comp = [ intcodeComputer(input[:]), intcodeComputer(input[:]), intcodeComputer(input[:]), intcodeComputer(input[:]), intcodeComputer(input[:]) ] progInputs = [0] for i in range(len(phase)): progInputs.insert(0, phase[i] + 5) comp[i].input = progInputs comp[i].compute() progInputs = comp[i].output while comp[-1].paused: for i in range(len(phase)): comp[i].input = progInputs comp[i].compute() progInputs = comp[i].output outputs.append(progInputs[-1]) print("Part 2:") print("\t" + str(max(outputs)) + "\n")
def Part1(input: List[int]) -> None: comp = intcodeComputer(input) comp.compute() grid = [[]] for v in comp.output: if v == 10: grid.append([]) else: grid[-1].append(chr(v)) grid = [g for g in grid if g != []] intersections = list() for y in range(len(grid)): for x in range(len(grid[y])): if grid[y][x] == '.': continue sides = list() if y - 1 >= 0: sides.append(grid[y - 1][x]) if x - 1 >= 0: sides.append(grid[y][x - 1]) if y + 1 < len(grid): sides.append(grid[y + 1][x]) if x + 1 < len(grid[y]): sides.append(grid[y][x + 1]) if sides.count('#') >= 3: intersections.append((x, y)) print("Part 1:") print("\t" + str(sum([v[0] * v[1] for v in intersections])) + "\n")
def makeTree(comp, pos: Tuple = (0, 0), direction: str = None, parent=None): copyComp = intcodeComputer(comp.comp) copyComp.i = comp.i copyComp.output = [] copyComp.paused = comp.paused copyComp.base = comp.base if direction: copyComp.input = [dir[direction]] copyComp.compute() if copyComp.output.pop(0) == 2: makeTree.O2 = pos makeTree.comp = copyComp newDirs = [ dirs[a] for a in [i for i, v in enumerate(getSurroundings(copyComp)) if v != 0] ] try: newDirs.remove(dirs[(dirs.index(direction) + 2) % len(dirs)]) except ValueError: pass top = Node(pos) top.parent = parent for d in newDirs: top.children.append(makeTree(copyComp, addTuple(pos, move[d]), d, top)) return top
def Part2(input: List[int]) -> None: input[0] = 2 comp = intcodeComputer(input) comp.compute() x = comp.output[0::3] y = comp.output[1::3] tile = comp.output[2::3] grid = dict(zip([(a, b) for a, b in zip(x, y)], tile)) while comp.paused: # Update Grid x = comp.output[0::3] y = comp.output[1::3] tile = comp.output[2::3] newGrid = dict(zip([(a, b) for a, b in zip(x, y)], tile)) for pos in newGrid.keys(): grid[pos] = newGrid[pos] comp.output = [] ballPos = [coord for coord, val in grid.items() if val == 4][0][0] paddlePos = [coord for coord, val in grid.items() if val == 3][0][0] comp.input = [sign(ballPos - paddlePos)] comp.compute() # Update Grid x = comp.output[0::3] y = comp.output[1::3] tile = comp.output[2::3] finalGrid = dict(zip([(a, b) for a, b in zip(x, y)], tile)) print("Part 2:") print("\t" + str(finalGrid[(-1, 0)]) + "\n")
def Part1(input: List[int]) -> None: robot = {"color": [0], "pos": (0, 0), "dir": 0} grid = dict() comp = intcodeComputer(input, robot["color"]) comp.compute() while comp.paused: # Paint Tile grid[robot["pos"]] = comp.output.pop(0) # Get Direction robot["dir"] += 1 if comp.output.pop(0) == 0 else -1 robot["dir"] %= len(dir) # Move robot robot["pos"] = tuple(map(add, robot["pos"], dir[robot["dir"]][1])) # Get robot's current color robot["color"] = grid[robot["pos"]] if robot["pos"] in grid else 0 # Compute next cycle comp.input = [robot["color"]] comp.compute() if comp.output: grid[robot["pos"]] = comp.output.pop(0) print("Part 1:") print("\t" + str(len(grid.keys())) + "\n")
def Part1(input: List[int]) -> None: comp = intcodeComputer(input) comp.compute() x = comp.output[0::3] y = comp.output[1::3] tile = comp.output[2::3] grid = dict(zip([(a, b) for a, b in zip(x, y)], tile)) print("Part 1:") print("\t" + str(list(grid.values()).count(2)) + "\n")
def getSurroundings(comp) -> List[int]: output = list() for d in dirs: copyComp = intcodeComputer(comp.comp) copyComp.i = comp.i copyComp.output = comp.output copyComp.paused = comp.paused copyComp.base = comp.base copyComp.input = [dir[d]] copyComp.compute() output.append(copyComp.output[-1]) return output
def Part1(input: List[int]): comp = intcodeComputer(input) tree = makeTree(comp) O2Node = findTree(tree, makeTree.O2) # Find O2 node depth depth = 0 top = O2Node while top.parent: top = top.parent depth += 1 print("Part 1:") print("\t" + str(depth) + "\n") return makeTree.comp
def Part2(input: List[int]) -> None: comp = intcodeComputer(input) comp.compute() grid = [[]] for v in comp.output: if v == 10: grid.append([]) else: grid[-1].append(chr(v)) grid = [g for g in grid if g != []] start = [] for y in range(len(grid)): for x in range(len(grid[y])): if (v := grid[y][x]) in '^<v>': start = (x, y) startDir = '^<v>'.index(v) if start: break if start: break
def Part2(input: List[int]) -> None: robot = {"color": [1], "pos": (0, 0), "dir": 0} grid = dict() comp = intcodeComputer(input, robot["color"]) comp.compute() while comp.paused: # Paint Tile grid[robot["pos"]] = comp.output.pop(0) # Get Direction robot["dir"] += 1 if comp.output.pop(0) == 0 else -1 robot["dir"] %= len(dir) # Move robot robot["pos"] = tuple(map(add, robot["pos"], dir[robot["dir"]][1])) # Get robot's current color robot["color"] = grid[robot["pos"]] if robot["pos"] in grid else 0 # Compute next cycle comp.input = [robot["color"]] comp.compute() if comp.output: grid[robot["pos"]] = comp.output.pop(0) # Draw the image picture = [coord for coord, color in grid.items() if color == 1] offset = (min(grid.keys(), key=lambda x: x[0])[0], min(grid.keys(), key=lambda x: x[1])[1]) picture = [tuple(map(sub, v, offset)) for v in picture] corner = (max(picture, key=lambda x: x[0])[0], max(picture, key=lambda x: x[1])[1]) print("Part 2:") for j in reversed(range(corner[1] + 1)): print("\t", end='') for i in range(corner[0] + 1): print('██' if (i, j) in picture else ' ', end='') print() print()
for y in range(len(grid)): for x in range(len(grid[y])): if (v := grid[y][x]) in '^<v>': start = (x, y) startDir = '^<v>'.index(v) if start: break if start: break path = getPath(grid, start, dir[startDir]) path = ','.join(map(str, path)) compIn = zipStr(path) compIn = '\n'.join(compIn) + '\nn\n' compIn = [ord(c) for c in compIn] comp = intcodeComputer(input) comp.comp[0] = 2 comp.input = compIn comp.compute() print("Part 2:") print("\t" + str(comp.output[-1]) + "\n") with open('input.txt') as fp: lines = fp.read() lines = lines.replace('\n', '').split(',') input = list(map(int, lines)) Part1(input) Part2(input)
def Part1(input: List[int]) -> None: comp = intcodeComputer(input, [1]) comp.compute() print("Part 1:") print("\t" + str(comp.output[-1]) + "\n")