def get_answer1(df: pd.DataFrame) -> float: Nstep = 100 df = df2grid(df) df = df.astype(int) Nflashes = 0 for itt in range(0, Nstep): #step 1 df += 1 #step 2 df_has_flashed = pd.DataFrame(False, index=df.index, columns=df.columns) max_val_not_flashed = df.max().max() while max_val_not_flashed > 9: df_flash = df > 9 for x in itertools.product(list(df.index), list(df.columns)): if df_flash.iloc[x] and not df_has_flashed.iloc[x]: #add to all neighbors xn_list = get_neighbor_idx(x, df) for xn in xn_list: df.iloc[xn] += 1 # update df_has_flashed = df_has_flashed | df_flash max_val_not_flashed = df[-df_has_flashed].max().max() #step 3 df[df_has_flashed] = 0 Nflashes += df_has_flashed.sum().sum() return Nflashes
def get_answer1(df: pd.DataFrame) -> float: df = df2grid(df) #handle that df is not square for final input if df.shape[0] != df.shape[1]: df = df.append( pd.DataFrame('.', columns=df.columns, index=[max(df.index) + 1])) Maxitt = 1000 for ii in range(Maxitt): if round(ii / 10) == ii / 10: print(ii) df_next = df.copy() for x in itertools.product(list(df.index), list(df.columns)): if df.iloc[x] == '.': continue else: xn_list = get_neighbor_idx(x, df) nn = '' for xn_i in xn_list: nn += df.iloc[xn_i] if df.iloc[x] == 'L' and nn.count('#') == 0: df_next.iloc[x] = '#' elif df.iloc[x] == '#' and nn.count('#') >= 4: df_next.iloc[x] = 'L' #check for convergence if df.equals(df_next): break # update df = df_next.copy() print(ii) return (df == '#').sum().sum()
def get_answer1(df: pd.DataFrame)->float: # split in 1 column per value df = df2grid(df) df = df.astype(int) # find lowpoints lowpoint_idx = find_lowpoints(df) # calculate score score = 0 for idx in lowpoint_idx: val = df.loc[idx] score += (val+1) return score
def get_answer2(df: pd.DataFrame) -> float: df = df2grid(df) df = df.astype(int) # make new grid Nrep = 5 df_all_list = [] for rowrep in range(Nrep): dfcol0 = (rowrep) + df.copy() dfcol0[dfcol0 > 9] += -9 df_rowx_list = [dfcol0] for colrep in range(Nrep - 1): df_newtile = (colrep + 1) + dfcol0.copy() df_newtile[df_newtile > 9] += -9 df_rowx_list.append(df_newtile) df_rowx = pd.concat(df_rowx_list, axis=1) df_rowx.columns = range(df_rowx.shape[1]) df_all_list.append(df_rowx) df_all = pd.concat(df_all_list, axis=0).reset_index(drop=True) risk = find_lowrisk_path(df_all) return risk
def get_answer1(df: pd.DataFrame) -> float: df = df2grid(df) df = df.astype(int) risk = find_lowrisk_path(df) return risk
def get_answer2(df: pd.DataFrame) -> float: df = df2grid(df) #handle that df is not square for final input if df.shape[0] != df.shape[1]: df = df.append( pd.DataFrame('.', columns=df.columns, index=[max(df.index) + 1])) # add border df = df.append( pd.DataFrame('B', columns=df.columns, index=[max(df.index) + 1])) df = df.append( pd.DataFrame('B', columns=df.columns, index=[min(df.index) - 1])) df = df.sort_index().reset_index(drop=True) df.insert(0, -1, 'B') df.insert(max(df.columns) + 2, max(df.columns) + 1, 'B') df.columns = df.columns + 1 true_board_idx = list(df.index)[1:-1] Maxitt = 1000 for ii in range(Maxitt): if round(ii / 10) == ii / 10: print(ii) df_next = df.copy() for x in itertools.product(true_board_idx, true_board_idx): if df.iloc[x] == '.': continue else: step = 1 xn_list = get_neighbor_idx(x, df, step) found = len(xn_list) * [False] nn = '' while sum(found) < len(found): idx_at_boarder = [] for idx, xn_i in enumerate(xn_list): #find seats in this distance if not found[idx] and df.iloc[xn_i] in ['L', '#']: nn += df.iloc[xn_i] found[idx] = True #update found if we are at edge so we dont keep looking in that direction elif df.iloc[xn_i] == 'B': #found[idx] = True idx_at_boarder.append(idx) #remove boarders from search for ix in sorted(idx_at_boarder, reverse=True): found.pop(ix) #update step += 1 xn_list = get_neighbor_idx(x, df, step) #update seat x if df.iloc[x] == 'L' and nn.count('#') == 0: df_next.iloc[x] = '#' elif df.iloc[x] == '#' and nn.count('#') >= 5: df_next.iloc[x] = 'L' #check for convergence if df.equals(df_next): break # update df = df_next.copy() print(ii) return (df == '#').sum().sum()