Make a 2d menger sponge

2

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.

John Paul Penaloza

Posted 2017-09-24T12:57:34.670

Reputation: 31

Question was closed 2017-09-24T13:17:13.957

1

Also known as a Sierpinski carpet

– H.PWiz – 2017-09-24T13:04:01.527

Answers

1

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

dzaima

Posted 2017-09-24T12:57:34.670

Reputation: 19 048

Wow, impressive. Mind explaining it a little bit for people not as familiar with these types of languages? – John Paul Penaloza – 2017-09-24T13:25:13.713

@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 – dzaima – 2017-09-24T13:29:54.473

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! – John Paul Penaloza – 2017-09-24T13:33:02.720