Best Python code snippet using sure_python
day_09.py
Source:day_09.py
1# https://adventofcode.com/2021/day/92import functools3def get_lower_than(heightmap):4 rows, cols = len(heightmap), len(heightmap[0])5 lower_than = [[set() for _ in range(cols)] for _ in range(rows)]6 directions = ((1, 0), (-1, 0), (0, 1), (0, -1))7 for r in range(rows):8 for c in range(cols):9 if heightmap[r][c] == 9:10 continue11 elif heightmap[r][c] == 0:12 lower_than[r][c].update(13 tuple(sum(x) for x in zip((r, c), d))14 for d in directions)15 else:16 for (d_r, d_c) in directions:17 if (18 (r + d_r) not in range(rows) or19 (c + d_c) not in range(cols) or20 heightmap[r][c] < heightmap[r + d_r][c + d_c]21 ):22 lower_than[r][c].add((r + d_r, c + d_c))23 return lower_than24def get_basin(heightmap, low_point, lower_than):25 rows, cols = len(heightmap), len(heightmap[0])26 lower_than = get_lower_than(heightmap)27 (lp_r, lp_c) = low_point28 basin = set((low_point,))29 frontier = list(lower_than[lp_r][lp_c])30 visited = set()31 while frontier:32 (p_r, p_c) = frontier.pop()33 if (p_r, p_c) in visited:34 continue35 elif (36 (p_r, p_c) in basin or37 p_r not in range(rows) or38 p_c not in range(cols) or39 heightmap[p_r][p_c] == 940 ):41 visited.add((p_r, p_c))42 continue43 else:44 basin.add((p_r, p_c))45 visited.add((p_r, p_c))46 frontier += list(lower_than[p_r][p_c])47 return basin48def get_risk_level(heightmap):49 rows, cols = len(heightmap), len(heightmap[0])50 lower_than = get_lower_than(heightmap)51 return sum(heightmap[r][c] + 152 for r in range(rows)53 for c in range(cols)54 if len(lower_than[r][c]) == 455 )56def find_basins(heightmap):57 rows, cols = len(heightmap), len(heightmap[0])58 lower_than = get_lower_than(heightmap)59 low_points = [(r, c)60 for r in range(rows)61 for c in range(cols)62 if len(lower_than[r][c]) == 463 ]64 basin_sizes = [len(get_basin(heightmap, lp, lower_than))65 for lp in low_points]66 return functools.reduce(67 lambda x, y: x * y,68 sorted(basin_sizes, reverse=True)[:3]69 )70if __name__ == '__main__':71 with open('input/day_09.txt', 'r') as f:72 heightmap = tuple(...
__init__.py
Source:__init__.py
1from analyze.deeppoly.transform import Transformer2import torch3class ReluTransformer(Transformer):4 def __init__(self, heuristic):5 self._heuristic = heuristic6 def _transform(self, ad, input, ads=None):7 ad_relu = ad.clone()8 # 1. setting the equations to zero for nodes with upper_bound lower than zero9 set_to_zero = (ad.upper_bounds <= 0).flatten()10 ad_relu.lower_than.A[set_to_zero, :] = 0.011 ad_relu.lower_than.v[set_to_zero, :] = 0.012 ad_relu.greater_than.A[set_to_zero, :] = 0.013 ad_relu.greater_than.v[set_to_zero, :] = 0.014 # 2. for nodes where the lower_bound is bigger than 0 we don't do anything.15 # 3. for nodes with lower_bound < 0 and upper_bound > 016 relu_mask = ((ad.lower_bounds < 0) & (ad.upper_bounds > 0)).flatten()17 # 3.1 we set the upper bounds and relational contraints:18 # x^L_j <= u_j/(u_j-l_j) * (x^(L-1) - l_j) where l_j <= x^(L-1) <= u_j19 delta = (ad.upper_bounds[relu_mask] - ad.lower_bounds[relu_mask])20 lambda_up = ad.upper_bounds[relu_mask] / delta21 c_up = - ad.lower_bounds[relu_mask] * ad.upper_bounds[relu_mask] / delta22 ad_relu.greater_than.A[relu_mask, :] *= lambda_up23 ad_relu.greater_than.v[relu_mask] *= lambda_up24 ad_relu.greater_than.v[relu_mask] += c_up25 ad_relu.greater_than.A_no_sub = torch.eye(len(relu_mask))26 ad_relu.greater_than.A_no_sub[relu_mask, :] = (torch.eye(len(relu_mask))[relu_mask] * lambda_up)27 ad_relu.greater_than.v_no_sub = torch.zeros(ad_relu.greater_than.v_no_sub.shape)28 ad_relu.greater_than.v_no_sub.T[relu_mask] = c_up29 # 3.2 for the lower bound, the heuristic..30 # x^L_j >= lambda * x^(L-1)_j31 lambda_low = self._heuristic.compute_lambda(ad.lower_bounds[relu_mask], ad.upper_bounds[relu_mask])32 ad_relu.lower_than.A[relu_mask, :] *= lambda_low33 ad_relu.lower_than.v[relu_mask] *= lambda_low34 ad_relu.lower_than.A_no_sub = torch.eye(len(relu_mask))35 ad_relu.lower_than.A_no_sub[relu_mask, :] = (torch.eye(len(relu_mask))[relu_mask] * lambda_low)36 ad_relu.lower_than.v_no_sub = torch.zeros(ad_relu.lower_than.v_no_sub.shape)37 ad_relu.lower_than.v_no_sub.T[relu_mask] = lambda_low38 #if backprop:39 # ad_relu.upper_bounds = ad_relu.greater_than.compute_bounds_backprop(ad_relu, ads)40 # ad_relu.lower_bounds = ad_relu.lower_than.compute_bounds_backprop(ad_relu, ads)41 #else:42 ad_relu.lower_bounds = ad_relu.lower_than.compute_bounds(input.lower_bounds, input.upper_bounds)43 ad_relu.upper_bounds = ad_relu.greater_than.compute_bounds(input.lower_bounds, input.upper_bounds)...
day9.py
Source:day9.py
1import numpy as np2inp = []3with open("day9_input.txt") as file:4 inp = [[int(y) for y in x[:-1]] for x in file]5A = np.array(inp)6lower_than = np.full_like(A,0)7lower_than[1:,:] += A[1:,:] < A[:-1,:] #above8lower_than[:-1,:] += A[:-1,:] < A[1:,:] #below9lower_than[:,1:] += A[:,1:] < A[:,:-1] #left10lower_than[:,:-1] += A[:,:-1] < A[:,1:] #right11lower_than[0,:] += 112lower_than[-1,:] += 113lower_than[:,0] += 114lower_than[:,-1] += 115risk_level = 016low_points = []17for i in range(lower_than.shape[0]):18 for j in range(lower_than.shape[1]):19 if lower_than[i,j] == 4:20 risk_level += A[i,j] + 121 low_points.append((i,j))22print("part1", risk_level)23basin_sizes = []24for low_point in low_points:25 flood = np.full_like(A, False, dtype=np.bool_)26 flood[low_point] = True27 previous_sum = 028 while np.sum(flood) > previous_sum:29 previous_sum = np.sum(flood)30 for i in range(100):31 for j in range(100):32 if flood[i,j] == False:33 continue34 if i-1 >= 0 and A[i-1,j] != 9:35 flood[i-1,j] = True36 if i+1 < 100 and A[i+1,j] != 9:37 flood[i+1,j] = True38 if j-1 >= 0 and A[i,j-1] != 9:39 flood[i,j-1] = True40 if j+1 < 100 and A[i,j+1] != 9:41 flood[i,j+1] = True42 basin_sizes.append(previous_sum)43basin_sizes.sort()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!