Day 09 (glamorized)

This commit is contained in:
Sebastian Seedorf
2021-12-14 11:15:36 +01:00
parent 507987855f
commit de0a250ed9
2 changed files with 2 additions and 4 deletions

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env python3
import numpy as p;import scipy.ndimage as i;a=p.array([[int(n)for n in line.strip()]for line in open("input.txt")]);n=i.generate_binary_structure(len(a.shape),1);print(sum(a[p.where((i.minimum_filter(a,footprint=n)==a)*~(i.maximum_filter(a,footprint=n)==a))]+1))
import numpy as p;import scipy.ndimage as i;a=p.array([[int(n)for n in line.strip()]for line in open("input.txt")]);n=p.array([[0,1,0],[1,1,1],[0,1,0]]);print(sum(a[p.where((i.minimum_filter(a,footprint=n)==a)*~(i.maximum_filter(a,footprint=n)==a))]+1))

View File

@@ -2,13 +2,11 @@
import numpy as np
import scipy.ndimage.filters as filters
import scipy.ndimage.morphology as morphology
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")])
# https://stackoverflow.com/questions/3986345/how-to-find-the-local-minima-of-a-smooth-multidimensional-array-in-numpy-efficie
neighborhood = morphology.generate_binary_structure(len(arr.shape), 1)
neighborhood = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
local_min = filters.minimum_filter(arr, footprint=neighborhood) == arr
local_max = filters.maximum_filter(arr, footprint=neighborhood) == arr
local_min_without_plateau = np.logical_and(local_min, np.logical_not(local_max))