Task
Produce a graphical or ASCII-art representation of a Sierpinski carpet of order   N.



For example, the Sierpinski carpet of order   3   should look like this:

###########################
# ## ## ## ## ## ## ## ## #
###########################
###   ######   ######   ###
# #   # ## #   # ## #   # #
###   ######   ######   ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
#########         #########
# ## ## #         # ## ## #
#########         #########
###   ###         ###   ###
# #   # #         # #   # #
###   ###         ###   ###
#########         #########
# ## ## #         # ## ## #
#########         #########
###########################
# ## ## ## ## ## ## ## ## #
###########################
###   ######   ######   ###
# #   # ## #   # ## #   # #
###   ######   ######   ###
###########################
# ## ## ## ## ## ## ## ## #
###########################

The use of the   #   character is not rigidly required for ASCII art.

The important requirement is the placement of whitespace and non-whitespace characters.



Related task

   Sierpinski triangle


package require Tcl 8.5
 
proc map {lambda list} {
    foreach elem $list {
        lappend result [apply $lambda $elem]
    }
    return $result
}
 
proc sierpinski_carpet n {
    set carpet [list "#"]
    for {set i 1} {$i <= $n} {incr i} {
        set carpet [concat \
            [map {x {subst {$x$x$x}}} $carpet] \
            [map {x {subst {$x[string map {"#" " "} $x]$x}}} $carpet] \
            [map {x {subst {$x$x$x}}} $carpet] \
        ]
    }
    return [join $carpet \n]
}
 
puts [sierpinski_carpet 3]