Same output (based on input) on each run:
Å1āÉI<.Da)
Port of @Somebody's Python answer.
Try it online or verify the first ten sizes.
Random valid output on each run:
1ÝIãIãʒ€ü2ü2εøOO4Ö≠}˜P}ʒD_‚ε©˜ƶ®gäΔ2Fø0δ.ø}2Fø€ü3}®*εεÅsyøÅs«à}}}˜Ùg<}P}Ω
Try it online. (Slightly modified ʒ...}Ω to .r.Δ... to speed it up, but unfortunately it'll still time out for \$n\geq4\$.)
Explanation:
Å1 # Push a list of the (implicit) input amount of 1s
ā # Push a list in the range [1,length] (without popping)
É # Check for each whether it's odd: [1,0,1,0,1,0,...]
I< # Push the input-1
.D # Duplicate the [1,0,1,0,1,0,...]-list that many times
a # Change the top list to a list of 0s instead with an is_letter check
) # Wrap all lists on the stack into a list
# (after which this matrix is output implicitly as result)
Step 1: Create a list of all possible input by input matrices with 0s and 1s:
1Ý # Push pair [0,1]
Iã # Take the cartesian power of the input to get all input-sized lists using
# 0s and 1s
Iã # And again, to get all possible matrices
Step 2: Filter this list of matrices to keep all where each overlapping 2x2 block contains at least one 1 and at least one 0:
ʒ # Filter this list of matrices by:
€ü2ü2εø # Get all overlapping 2x2 blocks:
€ # Map over each row:
ü2 # Get its overlapping pairs
ü2 # Get the overlapping lists of lists of pairs
ε # Map over each:
ø # Zip/transpose; swapping rows/columns
OO # Sum each inner 2x2 block
4Ö≠ # Check for each sum whether it's NOT divisible by 4
# (aka it's 1, 2, or 3, and NOT 0 or 4)
} # Close the map
˜ # Flatten this matrix of 2x2 block sum checks
P # Check that all are truthy by taking the product
} # Close the filter
Step 3: Filter it again to keep those where all matrices where both the 1s and 0s form a single island:
ʒ # Filter this list of matrices again:
D # Duplicate this matrix
_ # Invert each 0s to 1 and vice-versa in the copy
‚ # Pair the two inverted matrices together
ε # Map over this pair:
© # Store the current matrix in variable `®` (without popping)
˜ # Flatten it to a single list
ƶ # Multiply each value by its 1-based index
®gä # Convert it back to a matrix of size `®`
Δ # Loop until the result no longer changes to flood-fill:
2Fø0δ.ø} # Add a border of 0s around the matrix:
2F } # Loop 2 times:
ø # Zip/transpose; swapping rows/columns
δ # Map over each row:
0 .ø # Add a leading/trailing 0
2Fø€ü3} # Convert it into overlapping 3x3 blocks:
2F } # Loop 2 times again:
ø # Zip/transpose; swapping rows/columns
€ # Map over each inner list:
ü3 # Convert it to a list of overlapping triplets
®* # Multiply each 3x3 block by the value in matrix `®`
# (so the 0s remain 0s)
εεÅsyøÅs«à # Get the largest value from the horizontal/vertical cross of each
# 3x3 block:
εε # Nested map over each 3x3 block:
Ås # Pop and push its middle row
y # Push the 3x3 block again
ø # Zip/transpose; swapping rows/columns
Ås # Pop and push its middle rows as well (the middle column)
« # Merge the middle row and column together to a single list
à # Pop and push its maximum
}} # Close the nested maps
} # Close the changes-loop
˜ # Flatten the flood-filled matrix
Ù # Uniquify this list
g< # Pop and push its length-1
# (only 1 is truthy in 05AB1E, so this checks if two islands remain)
} # Close the map
P # Check that both are truthy, for the regular and inverted matrix
} # Close the filter
Ω # Pop and push a random matrix from this filtered list
# (which is output implicitly as result)