1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#!/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)
|