data = [] with open('data', 'r') as fp: # NOTE(dev): We assume all lines are the same length for line in fp: data_line = [] for ch in line.strip(): data_line.append(int(ch)) data.append(data_line) total = 0 for y in range(0, len(data)): for x in range(0, len(data[y])): sub_total = 0 all_four = 0 # above if(y > 0): if(data[y-1][x] > data[y][x]): all_four += 1 else: all_four += 1 # skip # below if(y < len(data) - 1): if(data[y+1][x] > data[y][x]): all_four += 1 else: all_four += 1 #skip # left if(x > 0): if(data[y][x-1] > data[y][x]): all_four += 1 else: all_four += 1 #skip # right if(x < len(data[y]) - 1): if(data[y][x+1] > data[y][x]): all_four += 1 else: all_four += 1 #skip if(all_four == 4): total += data[y][x] + 1 print(total)