Skip to content

Instantly share code, notes, and snippets.

@aarondewindt
Last active November 20, 2021 21:20
Show Gist options
  • Save aarondewindt/d8e5a25d460a3b68b12da667f22a1599 to your computer and use it in GitHub Desktop.
Save aarondewindt/d8e5a25d460a3b68b12da667f22a1599 to your computer and use it in GitHub Desktop.
import numpy as np
import numpy.testing as npt
from itertools import product
b = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 0, 0, 0, 1, 0, 1, 0,],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0,],
[0, 0, 0, 1, 0, 0, 0, 0, 1, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 1, 0, 0, 1, 0, 1, 0,],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 1, 0, 0, 0, 0, 1, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 1, 0, 0, 1, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 1, 0, 0, 0, 0, 1, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
])
b_correct = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0,],
[0, 0, 1, 0, 0, 0, 0, 1, 1, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0,],
[0, 1, 0, 1, 0, 0, 0, 1, 0, 0,],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
])
b=(3==(n:=sum(np.roll(b,s,(0,1))for s in product(*2*[(-1,0,1)]))))|(n==4)&b
print(b)
npt.assert_equal(b, b_correct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment