# Make a 2d menger sponge [duplicate]

A menger sponge is a fractal made out of cubes within cubes within cubes...

If you start with a cube, on each face there are 9 squares. The middle square becomes empty (or 0). The other 8 squares iterate this process. They each get split up into 9 squares and the middle square face is taken out.

The goal today is to generate a menger sponge face, not the cube.

Here are some example given an input for the number of iterations.

0:
1
(No holes or anything, solid face)

1:
1 1 1
1 0 1
1 1 1

2:
111111111
101101101
111111111
111000111
101000101
111000111
111111111
101101101
111111111


To complete the challenge, based on an input, return a set of 1 (filled in blocks) and 0 (empty blocks) with spaces between or not.

This is code golf, lowest number of bytes wins.

• Also known as a Sierpinski carpet Sep 24 '17 at 13:04

# SOGL V0.12, 19 bytes

1¹.{³³┼┼≥:10ŗ;┼┼⁴++


Try it Here!

1¹                   push [1]
.{                 input times do
³³                 duplicate top of stack 4 times, resulting in 5 total copies in the stack
┼┼               join the last 3 copies horizontally together
≥              put the joined at the bottom of the stack
:             duplicate top of stack again
10ŗ          replace 1 with 0 only in the top item
;         swap the top 2 items
┼┼       join the top 3 items horizontally together
⁴      copy the item one below top of stack
++    add together vertically

• Wow, impressive. Mind explaining it a little bit for people not as familiar with these types of languages? Sep 24 '17 at 13:25
• @JohnPaulPenaloza while I'm writing an explanation, you could turn on the debug mode, click run, and step trough the program to see it live in action Sep 24 '17 at 13:29
• Thanks for the explanation. I'm new to code-golf style challenges and have never come close to solving one, I didn't know about the debug thing. Seeing these awesome solutions is really cool! Sep 24 '17 at 13:33