def explore(self, stdscr=None, delay=0.1): if stdscr: stdscr.addstr(0, 0, self.get_map_string()) stdscr.refresh() if self.map[self.location] == OXYGEN: stdscr.addstr(f'\nTarget Found in {self.counter} steps!') stdscr.refresh() time.sleep(5) else: time.sleep(delay) to_search = [] # search around self for d in DIRECTIONS: if tadd(self.location, DIRECTIONS[d]) not in self.map: res = self.move(d) if res == 1 or res == 2: to_search.append(d) self.move(REVERSE_DIRECTIONS[d]) self.counter -= 2 # search each unexplored path for d in to_search: self.move(d) self.explore(stdscr, delay) self.move(REVERSE_DIRECTIONS[d]) self.counter -= 2
def main(stdscr): stdscr.clear() drone = Drone(DRONE_PROGRAM) drone.explore() grid = drone.get_grid() w, h = len(grid[0]), len(grid) print(get_grid_string(grid)) fronts = [(7, 37)] OLD_OXYGEN = 'o' steps = 0 while len(fronts) > 0: new_fronts = [] for front in fronts: for d in DIRECTIONS.values(): x, y = tadd(front, d) if grid[y][x] == EMPTY: grid[y][x] == OXYGEN new_fronts.append((x, y)) grid[front[1]][front[0]] = OLD_OXYGEN fronts = new_fronts steps += 1 stdscr.addstr(0, 0, get_grid_string(grid) + f'\n Steps: {steps}') stdscr.refresh() time.sleep(0.05) time.sleep(10)
def move(self, direction): assert direction in DIRECTIONS, "Invalid direction" res = self.program.send(direction) new = tadd(self.location, DIRECTIONS[direction]) if res == 0: self.map[new] = WALL elif res == 1: self.map[new] = EMPTY self.location = new self.counter += 1 elif res == 2: self.map[new] = OXYGEN self.location = new self.target_found = True self.counter += 1 next(self.program)
return self._potential() * self._kinetic() positions = [[5, -1, 5], [0, -14, 2], [16, 4, 0], [18, 1, 16]] # positions = [[-1, 0, 2], [2, -10, -7], [4, -8, 8], [3, 5, -1]] moons = [Moon(pos) for pos in positions] steps = 1000 for _ in range(steps): # gravity for m1, m2 in combinations(moons, 2): for i in range(3): if m1.pos[i] < m2.pos[i]: m1.vel[i] += 1 m2.vel[i] -= 1 elif m1.pos[i] > m2.pos[i]: m1.vel[i] -= 1 m2.vel[i] += 1 # velocity for moon in moons: moon.pos = tadd(moon.pos, moon.vel) # output # print(f'After {_ + 1} steps:') # for moon in moons: # print(moon) # for moon in moons: # print(moon.energy()) print(sum(moon.energy() for moon in moons))
rob = intcode(paint) rob.send(None) pos, direction = (0, 0), (0, -1) painted = {(0, 0): 1} try: while True: painted[pos] = rob.send(painted.get(pos, 0)) if next(rob) == 0: direction = DIRECTIONS[(DIRECTIONS.index(direction) + 1) % 4] else: direction = DIRECTIONS[(DIRECTIONS.index(direction) - 1) % 4] pos = tadd(pos, direction) next(rob) # advance to next input except StopIteration: pass max_x, max_y = float('-inf'), float('-inf') min_x, min_y = float('inf'), float('inf') for coord in painted: x, y = coord max_x = max(x, max_x) max_y = max(y, max_y) min_x = min(x, min_x) min_y = min(y, min_y) for j in range(max_y + 1): s = ""
from common import intcode, tadd, grid_to_string with open('day17.txt') as f: aft = [int(c) for c in f.readline().strip().split(",")] DIRECTIONS = [(1, 0), (0, 1), (-1, 0), (0, -1)] if __name__ == "__main__": s = ''.join(chr(i) for i in list(intcode(aft))) s = s.rstrip() # remove trailing new lines grid = [[c for c in row] for row in s.split('\n')] intersections = [] for j, row in enumerate(grid): for i, cell in enumerate(row): if cell == "#": try: if all([ grid[y][x] == "#" for x, y in [tadd(d, (i, j)) for d in DIRECTIONS] ]): intersections.append((i, j)) except IndexError: pass print(grid_to_string(grid)) # print(intersections) ans = sum(x * y for x, y in intersections) print(ans) print(len(grid[0]), len(grid))