Skip to content

Commit 54ac574

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent cd90f94 commit 54ac574

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

cellular_automata/von_neumann.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def create_random_grid(
2626
2727
Args:
2828
rows: Number of grid rows
29-
columns: Number of grid columns
29+
columns: Number of grid columns
3030
alive_probability: Probability (0.0-1.0) of each cell being initially alive
3131
seed: Random seed for reproducibility
3232
@@ -115,23 +115,25 @@ def count_von_neumann_neighbors(
115115
down_neighbors = np.roll(alive_mask, 1, axis=0)
116116
left_neighbors = np.roll(alive_mask, -1, axis=1)
117117
right_neighbors = np.roll(alive_mask, 1, axis=1)
118-
neighbor_counts = up_neighbors + down_neighbors + left_neighbors + right_neighbors
118+
neighbor_counts = (
119+
up_neighbors + down_neighbors + left_neighbors + right_neighbors
120+
)
119121
else:
120122
# Manually count neighbors without wraparound
121123
for r in range(rows):
122124
for c in range(cols):
123125
count = 0
124126
# Check up
125-
if r > 0 and alive_mask[r-1, c]:
127+
if r > 0 and alive_mask[r - 1, c]:
126128
count += 1
127129
# Check down
128-
if r < rows-1 and alive_mask[r+1, c]:
130+
if r < rows - 1 and alive_mask[r + 1, c]:
129131
count += 1
130132
# Check left
131-
if c > 0 and alive_mask[r, c-1]:
133+
if c > 0 and alive_mask[r, c - 1]:
132134
count += 1
133135
# Check right
134-
if c < cols-1 and alive_mask[r, c+1]:
136+
if c < cols - 1 and alive_mask[r, c + 1]:
135137
count += 1
136138
neighbor_counts[r, c] = count
137139

@@ -168,7 +170,7 @@ def apply_cellular_automaton_rules(
168170
Examples:
169171
>>> ages = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=np.uint8)
170172
>>> new_ages = apply_cellular_automaton_rules(
171-
... ages, birth_neighbor_counts={2},
173+
... ages, birth_neighbor_counts={2},
172174
... survival_neighbor_counts={2, 3}, use_wraparound=False
173175
... )
174176
>>> bool(new_ages[0, 0] > 0) # corner should be born (2 neighbors: right and down)
@@ -177,7 +179,7 @@ def apply_cellular_automaton_rules(
177179
>>> # Test aging of dead cells
178180
>>> dead_aging = np.array([[2, 0, 0]], dtype=np.uint8) # age 2, no survival
179181
>>> result = apply_cellular_automaton_rules(
180-
... dead_aging, birth_neighbor_counts=set(),
182+
... dead_aging, birth_neighbor_counts=set(),
181183
... survival_neighbor_counts=set(), maximum_age=3
182184
... )
183185
>>> bool(result[0, 0] == 3) # should age from 2 to 3
@@ -198,8 +200,12 @@ def apply_cellular_automaton_rules(
198200
)
199201

200202
# Determine which cells are born or survive
201-
birth_mask = (~alive_cells_mask) & np.isin(neighbor_counts, list(birth_neighbor_counts))
202-
survival_mask = alive_cells_mask & np.isin(neighbor_counts, list(survival_neighbor_counts))
203+
birth_mask = (~alive_cells_mask) & np.isin(
204+
neighbor_counts, list(birth_neighbor_counts)
205+
)
206+
survival_mask = alive_cells_mask & np.isin(
207+
neighbor_counts, list(survival_neighbor_counts)
208+
)
203209

204210
new_ages = current_ages.copy()
205211

@@ -299,4 +305,5 @@ def simulate_von_neumann_cellular_automaton(
299305

300306
if __name__ == "__main__":
301307
import doctest
308+
302309
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)