#!/usr/bin/env python3 # import numpy as np from functools import reduce from re import findall from copy import deepcopy import sys # filename = "in/day08.ref" filename = "in/day08.pzl" data = open(filename).read().rstrip('\n') lines = [line for line in data.split('\n')] res1 = 0 res2 = 0 y_len = len(lines) x_len = len(lines[0]) arr = [[int(lines[y][x]) for x in range(x_len)] for y in range(y_len)] edge_count = 2*(y_len-1) + 2*(x_len-1) res1 += edge_count def get_len(arr): if len(arr) == 0: return 0 curr_max = -1 for i,val in enumerate(arr): if val < curr_max: return i curr_max = max(val, curr_max) return i+1 def get_len2(compare, arr): for i,val in enumerate(arr): if val >= compare: return i+1 return len(arr) def slice_x(arr, y, xs, xe, s=1): return [arr[y][i] for i in range(xs, xe, s)] def slice_y(arr, x, ys, ye, s=1): return [arr[i][x] for i in range(ys, ye, s)] scores = list() for y,_ in enumerate(arr): for x,_ in enumerate(arr[y]): v = arr[y][x] if 0 < y < y_len-1 and 0 < x < x_len-1: if all([v > arr[y][i] for i in range(0,x)]) or\ all([v > arr[y][i] for i in range(x+1,x_len)]) or\ all([v > arr[i][x] for i in range(0,y)]) or\ all([v > arr[i][x] for i in range(y+1,y_len)]): res1 += 1 mul = [ get_len2(v, slice_x(arr, y, x+1, x_len)), get_len2(v, slice_x(arr, y, x-1, -1, -1)), get_len2(v, slice_y(arr, x, y+1, y_len)), get_len2(v, slice_y(arr, x, y-1, -1, -1)) ] score = reduce(lambda x,y:x*y, mul) scores.append(score) res2 = max(scores) print('res1:', res1) print('res2:', res2)