``````

In [1]:

import sys
import numpy as np

``````
``````

In [2]:

GRID_SERIAL_NUMBER = 5093

``````
``````

In [3]:

def power_level(x, y):
rack_id = x + 10
power = rack_id * y
power += GRID_SERIAL_NUMBER
power *= rack_id
power = (power % 1000) // 100
power -= 5
return power

``````
``````

In [4]:

power_level(8,1)

``````
``````

Out[4]:

4

``````
``````

In [5]:

def racks(grid, cx, cy):
xm, ym = grid.shape
for row in range(0, ym - cx + 1):
for col in range(0, xm - cx + 1):
yield grid[row:row + cy, col:col + cx], col, row

``````
``````

In [6]:

grid = np.fromfunction(np.vectorize(power_level), (10, 10))

``````
``````

In [7]:

grid

``````
``````

Out[7]:

array([[ 4., -5., -4., -3., -2., -1.,  0.,  1.,  2.,  3.],
[-5., -4., -3., -2.,  0.,  1.,  2.,  3.,  4., -4.],
[-4., -3., -1.,  0.,  1.,  3.,  4., -4., -3., -1.],
[-3., -2.,  0.,  2.,  3., -5., -3., -2.,  0.,  2.],
[-2., -1.,  1.,  3., -5., -3., -1.,  1.,  3., -5.],
[-2.,  1.,  3., -5., -3.,  0.,  2.,  4., -4., -1.],
[-1.,  2., -5., -3.,  0.,  2., -5., -3.,  0.,  2.],
[ 0.,  3., -4., -1.,  2., -5., -2.,  1.,  3., -4.],
[ 1.,  4., -2.,  1.,  4., -3.,  1.,  4., -3.,  0.],
[ 2., -4., -1.,  3., -3.,  0.,  4., -3.,  1., -5.]])

``````
``````

In [8]:

racks(grid, 3, 3)

``````
``````

Out[8]:

<generator object racks at 0x7f6e51fe4138>

``````
``````

In [11]:

for rack, col, row in racks(grid, 3, 3):
print(f"{col}x{row} {rack}")

``````
``````

0x0 [[ 4. -5. -4.]
[-5. -4. -3.]
[-4. -3. -1.]]
1x0 [[-5. -4. -3.]
[-4. -3. -2.]
[-3. -1.  0.]]
2x0 [[-4. -3. -2.]
[-3. -2.  0.]
[-1.  0.  1.]]
3x0 [[-3. -2. -1.]
[-2.  0.  1.]
[ 0.  1.  3.]]
4x0 [[-2. -1.  0.]
[ 0.  1.  2.]
[ 1.  3.  4.]]
5x0 [[-1.  0.  1.]
[ 1.  2.  3.]
[ 3.  4. -4.]]
6x0 [[ 0.  1.  2.]
[ 2.  3.  4.]
[ 4. -4. -3.]]
7x0 [[ 1.  2.  3.]
[ 3.  4. -4.]
[-4. -3. -1.]]
0x1 [[-5. -4. -3.]
[-4. -3. -1.]
[-3. -2.  0.]]
1x1 [[-4. -3. -2.]
[-3. -1.  0.]
[-2.  0.  2.]]
2x1 [[-3. -2.  0.]
[-1.  0.  1.]
[ 0.  2.  3.]]
3x1 [[-2.  0.  1.]
[ 0.  1.  3.]
[ 2.  3. -5.]]
4x1 [[ 0.  1.  2.]
[ 1.  3.  4.]
[ 3. -5. -3.]]
5x1 [[ 1.  2.  3.]
[ 3.  4. -4.]
[-5. -3. -2.]]
6x1 [[ 2.  3.  4.]
[ 4. -4. -3.]
[-3. -2.  0.]]
7x1 [[ 3.  4. -4.]
[-4. -3. -1.]
[-2.  0.  2.]]
0x2 [[-4. -3. -1.]
[-3. -2.  0.]
[-2. -1.  1.]]
1x2 [[-3. -1.  0.]
[-2.  0.  2.]
[-1.  1.  3.]]
2x2 [[-1.  0.  1.]
[ 0.  2.  3.]
[ 1.  3. -5.]]
3x2 [[ 0.  1.  3.]
[ 2.  3. -5.]
[ 3. -5. -3.]]
4x2 [[ 1.  3.  4.]
[ 3. -5. -3.]
[-5. -3. -1.]]
5x2 [[ 3.  4. -4.]
[-5. -3. -2.]
[-3. -1.  1.]]
6x2 [[ 4. -4. -3.]
[-3. -2.  0.]
[-1.  1.  3.]]
7x2 [[-4. -3. -1.]
[-2.  0.  2.]
[ 1.  3. -5.]]
0x3 [[-3. -2.  0.]
[-2. -1.  1.]
[-2.  1.  3.]]
1x3 [[-2.  0.  2.]
[-1.  1.  3.]
[ 1.  3. -5.]]
2x3 [[ 0.  2.  3.]
[ 1.  3. -5.]
[ 3. -5. -3.]]
3x3 [[ 2.  3. -5.]
[ 3. -5. -3.]
[-5. -3.  0.]]
4x3 [[ 3. -5. -3.]
[-5. -3. -1.]
[-3.  0.  2.]]
5x3 [[-5. -3. -2.]
[-3. -1.  1.]
[ 0.  2.  4.]]
6x3 [[-3. -2.  0.]
[-1.  1.  3.]
[ 2.  4. -4.]]
7x3 [[-2.  0.  2.]
[ 1.  3. -5.]
[ 4. -4. -1.]]
0x4 [[-2. -1.  1.]
[-2.  1.  3.]
[-1.  2. -5.]]
1x4 [[-1.  1.  3.]
[ 1.  3. -5.]
[ 2. -5. -3.]]
2x4 [[ 1.  3. -5.]
[ 3. -5. -3.]
[-5. -3.  0.]]
3x4 [[ 3. -5. -3.]
[-5. -3.  0.]
[-3.  0.  2.]]
4x4 [[-5. -3. -1.]
[-3.  0.  2.]
[ 0.  2. -5.]]
5x4 [[-3. -1.  1.]
[ 0.  2.  4.]
[ 2. -5. -3.]]
6x4 [[-1.  1.  3.]
[ 2.  4. -4.]
[-5. -3.  0.]]
7x4 [[ 1.  3. -5.]
[ 4. -4. -1.]
[-3.  0.  2.]]
0x5 [[-2.  1.  3.]
[-1.  2. -5.]
[ 0.  3. -4.]]
1x5 [[ 1.  3. -5.]
[ 2. -5. -3.]
[ 3. -4. -1.]]
2x5 [[ 3. -5. -3.]
[-5. -3.  0.]
[-4. -1.  2.]]
3x5 [[-5. -3.  0.]
[-3.  0.  2.]
[-1.  2. -5.]]
4x5 [[-3.  0.  2.]
[ 0.  2. -5.]
[ 2. -5. -2.]]
5x5 [[ 0.  2.  4.]
[ 2. -5. -3.]
[-5. -2.  1.]]
6x5 [[ 2.  4. -4.]
[-5. -3.  0.]
[-2.  1.  3.]]
7x5 [[ 4. -4. -1.]
[-3.  0.  2.]
[ 1.  3. -4.]]
0x6 [[-1.  2. -5.]
[ 0.  3. -4.]
[ 1.  4. -2.]]
1x6 [[ 2. -5. -3.]
[ 3. -4. -1.]
[ 4. -2.  1.]]
2x6 [[-5. -3.  0.]
[-4. -1.  2.]
[-2.  1.  4.]]
3x6 [[-3.  0.  2.]
[-1.  2. -5.]
[ 1.  4. -3.]]
4x6 [[ 0.  2. -5.]
[ 2. -5. -2.]
[ 4. -3.  1.]]
5x6 [[ 2. -5. -3.]
[-5. -2.  1.]
[-3.  1.  4.]]
6x6 [[-5. -3.  0.]
[-2.  1.  3.]
[ 1.  4. -3.]]
7x6 [[-3.  0.  2.]
[ 1.  3. -4.]
[ 4. -3.  0.]]
0x7 [[ 0.  3. -4.]
[ 1.  4. -2.]
[ 2. -4. -1.]]
1x7 [[ 3. -4. -1.]
[ 4. -2.  1.]
[-4. -1.  3.]]
2x7 [[-4. -1.  2.]
[-2.  1.  4.]
[-1.  3. -3.]]
3x7 [[-1.  2. -5.]
[ 1.  4. -3.]
[ 3. -3.  0.]]
4x7 [[ 2. -5. -2.]
[ 4. -3.  1.]
[-3.  0.  4.]]
5x7 [[-5. -2.  1.]
[-3.  1.  4.]
[ 0.  4. -3.]]
6x7 [[-2.  1.  3.]
[ 1.  4. -3.]
[ 4. -3.  1.]]
7x7 [[ 1.  3. -4.]
[ 4. -3.  0.]
[-3.  1. -5.]]

``````
``````

In [12]:

def part_one():
grid = np.fromfunction(np.vectorize(power_level), (300, 300))
max_power = -sys.maxsize
max_rack = None
for rack, col, row in racks(grid, 3, 3):
power = np.sum(rack)
if power > max_power:
max_power = power
max_rack = (col, row)
return (max_power, max_rack)

``````
``````

In [13]:

(max_power, max_rack) = part_one()

``````
``````

In [14]:

max_power

``````
``````

Out[14]:

31.0

``````
``````

In [15]:

max_rack

``````
``````

Out[15]:

(49, 243)

``````
``````

In [ ]:

``````