δ‚·<QPIƒD»,¶?0δ.ø¬0*šĆ2Fø€ü3}εε˜DÅsD>I>%Š_iOĀ*ëĀO5‹*
Formats by joining each row with a space-delimiter and then all lines with a newline-delimiter. The output can therefore be a bit wonky if it contains any multi-digit integers (for inputs \$n\geq17\$).
The D»,¶?
could have been =
(-4 bytes) without the strict output format, by just outputting the matrices as is (without popping).
Try it online or verify all test cases below 10.
Explanation:
Step 1: Using the input \$n\$, create an \$n\times n\$ matrix of \$0\$s with a \$1\$ in the center:
δ # Apply double-vectorized:
# (which implicitly converts the implicit input-integer to
# the range [1,input] first, used twice)
‚ # Pair them together
· # Double each inner integer
< # Decrease each by 1
Q # Check for each value if it's equal to the (implicit) input
P # Get the product of each inner pair
Try just step 1 online.
Step 2: Loop \$n+1\$ amount of times, and pretty-print the matrix at the start of each iteration:
Iƒ # Loop the input+1 amount of times:
D # Duplicate the current matrix
» # Join each inner list by spaces,
# and then each string by newlines
, # Pop and print it with trailing newline
¶? # Print an additional empty line
Try steps 1 & 2 online.
Step 3a: Surround the entire matrix with a ring of 0s:
δ # Map over each row:
0 .ø # Surround it with a leading/trailing 0
¬ # Push the first row (without popping the matrix)
0* # Convert all its values to 0
š # Prepend this list to the matrix
Ć # Enclose; append its first row
Step 3b: And then get all overlapping 3x3 blocks. Each of these 3x3 blocks will have the current value at the center, and its 8 neighbors around it.
2F # Loop 2 times:
ø # Zip/transpose; swapping rows/columns
€ # Map over each row:
ü3 # Create overlapping triplets of this row
} # Close the loop
Try steps 1 & 3 online.
Step 4: Based on these 3x3 blocks, determine the next value for the cell:
εε # Map over each inner 3x3 block:
˜ # Flatten it to a single list of 9 values
D # Duplicate this list
Ås # Pop and push the middle cell value
D # Duplicate this middle value
> # Increase it by 1
I>% # Modulo the (input+1)
Š # Triple-swap list,middle,middle+1 to middle+1,list,middle
_i # If the middle cell is 0:
O # Sum its neighbors together
Ā # Check if this is not 0 (0 if 0; 1 otherwise)
* # Multiply this by the middle+1
ë # Else:
Ā # Check for each value that it's NOT 0
O # Sum to get the amount of filled cells, including itself
5‹ # Check if this is smaller than 5,
# so there are not 4 or more filled neighbors
* # Multiply this by the middle+1