summaryrefslogtreecommitdiff
path: root/2022/day8/asd.py
diff options
context:
space:
mode:
Diffstat (limited to '2022/day8/asd.py')
-rwxr-xr-x2022/day8/asd.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/2022/day8/asd.py b/2022/day8/asd.py
new file mode 100755
index 0000000..c9a7209
--- /dev/null
+++ b/2022/day8/asd.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+
+# import numpy as np
+from functools import reduce
+from re import findall
+from copy import deepcopy
+import sys
+
+# filename = "ref.txt"
+filename = "pzl.txt"
+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)
+