DEV Community

Discussion on: AoC Day 11: Chronal Charge

Collapse
 
r0f1 profile image
Florian Rohrer

Python

n = 7689

from itertools import product
from math import ceil
import numpy as np
from scipy import ndimage

a = np.zeros((300,300), dtype=int)
for x, y in product(range(300), range(300)):
    rack_id = x+1+10
    power_level = rack_id*(y+1) + n
    a[y,x] = (int((power_level*rack_id) / 100) % 10)-5

Part 1

k = np.ones((3,3), dtype=int)
m = ndimage.convolve(a, k, mode='constant', cval=0)
y, x = np.unravel_index(m.argmax(), m.shape)
print(x, y)

Part 2

r = []
for i in range(1, 301):
    k = np.ones((i,i), dtype=int)
    m = ndimage.convolve(a, k, mode='constant', cval=0)
    r.append((np.max(m), i))

r.sort(reverse=True)
maximum, i = r[0]
k = np.ones((i,i), dtype=int)
m = ndimage.convolve(a, k, mode='constant', cval=0)
y, x = np.unravel_index(m.argmax(), m.shape)
c = ceil(i / 2 - 2)
print(x-c, y-c, i)
Collapse
 
rpalo profile image
Ryan Palo

Nice use of Numpy! I think you proved @aspittel right. :)