def get_neighbours_v2(pos, grid: dict): neighbours = [] for direction in directions: current_pos = ut.pos_add(pos, direction) while in_bounds(current_pos) and grid[current_pos] == '.': current_pos = ut.pos_add(current_pos, direction) neighbours.append(grid.get(current_pos, '.')) return neighbours
def ride(slope): pos = (0, 0) trees = 0 while pos[1] < len(forest) - 1: pos = ut.pos_add(pos, slope) if is_tree(pos): trees += 1 return trees
def update_pos(self, pos): neighbour_positions = [ ut.pos_add(pos, delta_pos) for delta_pos in delta_positions.values() ] blacks = 0 for neighbour_pos in neighbour_positions: if self.tiles.get(neighbour_pos, "white") == "black": blacks += 1 else: self.dead_to_update.append(neighbour_pos) if self.tiles.get(pos, "white") == "white": if blacks == 2: self.new_tiles[pos] = "black" elif self.tiles.get(pos, "white") == "black": if not (blacks == 0 or blacks > 2): self.new_tiles[pos] = "black"
def get_neighbours(pos): return [ut.pos_add(pos, direction) for direction in directions]
def get_pos(instruction): pos = (0, 0) for direction in instruction: pos = ut.pos_add(pos, delta_positions[direction]) return pos
def N(self, units): delta_pos = ut.pos_mul(self.delta_directions['N'], units) self.way_point_pos = ut.pos_add(self.way_point_pos, delta_pos)
def F(self, units): delta_pos = ut.pos_mul(self.delta_directions[self.facing[0]], units) self.current_pos = ut.pos_add(self.current_pos, delta_pos)
def W(self, units): delta_pos = ut.pos_mul(self.delta_directions['W'], units) self.current_pos = ut.pos_add(self.current_pos, delta_pos)
def F(self, units): x_delta = self.way_point_pos[0] * units y_delta = self.way_point_pos[1] * units delta_pos = (x_delta, y_delta) self.current_pos = ut.pos_add(self.current_pos, delta_pos)
def get_neighbours(self, pos): return [ ut.pos_add(pos, delta_pos) for delta_pos in self.delta_positions ]