2
\$\begingroup\$

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.

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ Wow, impressive. Mind explaining it a little bit for people not as familiar with these types of languages? \$\endgroup\$
    – johnpyp
    Commented Sep 24, 2017 at 13:25
  • \$\begingroup\$ @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 \$\endgroup\$
    – dzaima
    Commented Sep 24, 2017 at 13:29
  • \$\begingroup\$ 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! \$\endgroup\$
    – johnpyp
    Commented Sep 24, 2017 at 13:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.