12
\$\begingroup\$

Task

Given a positive integer n, output the joined compound of circles

The circles are of square of size n+2 but removing the corner edges and the middle, before finally overlapping n times of itself

E.g.

2

->

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

->

 ##
#  #
#  #
 ##

->

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

Testcases

1
->
 #
# #
 #

2
->
 ## ##
#  #  #
#  #  #
 ## ##


3
->
 ### ### ###
#   #   #   #
#   #   #   #
#   #   #   #
 ### ### ###


4
->
 #### #### #### ####
#    #    #    #    #
#    #    #    #    #
#    #    #    #    #
#    #    #    #    #
 #### #### #### ####

Trailing spaces are allowed

You have to use the character #

You have to join the strings by newlines (no lists)

This is , so shortest code wins!

\$\endgroup\$

26 Answers 26

11
\$\begingroup\$

J, 37 32 31 30 28 bytes

'# '{~(>:+*:)($=/~0,~])0,#&1

Try it online!

-2 thanks to ovs for realizing XOR could be replaced with XNOR

key idea

The 2d pattern we seek is just the XOR function table of these two 1d patterns:

   0 1 1 1 0 1 1 1 0 1 1 1 0
  --------------------------
0 |0 1 1 1 0 1 1 1 0 1 1 1 0
1 |1 0 0 0 1 0 0 0 1 0 0 0 1
1 |1 0 0 0 1 0 0 0 1 0 0 0 1 <-- XOR TABLE                 
1 |1 0 0 0 1 0 0 0 1 0 0 0 1
0 |0 1 1 1 0 1 1 1 0 1 1 1 0
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I had that idea too! Those two patterns are the divisibilities of range(n*n+1) and range(n+1) by n+1. \$\endgroup\$
    – emanresu A
    Mar 14, 2022 at 3:24
  • \$\begingroup\$ @emanresuA Yeah, I actually tried to approach building them that way first, but the approach I took ended up being golfier in J. \$\endgroup\$
    – Jonah
    Mar 14, 2022 at 3:25
  • 2
    \$\begingroup\$ XNOR is 2 bytes short in J \$\endgroup\$
    – ovs
    Mar 14, 2022 at 8:55
6
\$\begingroup\$

Lua, 86 bytes

x,a,c=...," ","\n"b=a.rep;d=b(a..b("#",x),x)print(d..c..b(b("#"..b(a,x),x+1)..c,x)..d)

Attempt This Online! Tried moving some stuff around but couldn't get an improvement.

ATO with x=4. trailing newline is part of ATO

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to CGCC! Nice first answer. Hope you enjoy your time here! \$\endgroup\$
    – Giuseppe
    Mar 14, 2022 at 18:01
6
\$\begingroup\$

K (ngn/k), 37 35 34 30 26 bytes

{"# "(1,s)=\:1,/x#,s:&x,1}

Try it online!

Yay my second k answer!

Uses @Jonah's approach.

¯2 bytes thanks to @doug in the k tree

¯1 bytes by using @

¯4 bytes thanks to @ovs and @doug

¯4 bytes thanks to @Traws

\$\endgroup\$
2
  • 2
    \$\begingroup\$ I believe function application and indexing are the same thing, so the @ is not needed. And to make a table one of the each's is enough, as = already vectorizes. \$\endgroup\$
    – ovs
    Mar 15, 2022 at 8:57
  • \$\begingroup\$ you can rearrage the expression and use 1,&x instead of ~!1+x to save a few bytes \$\endgroup\$
    – Traws
    May 10, 2022 at 18:07
5
\$\begingroup\$

Vyxal C, 16 bytes

v\#₌≬₅*Ṅ꘍R₅*ǏȮJJ

Try it Online!

(IMO) this is some pretty elegant code. It uses some quite neat tricks to achieve the score it is. The output format's still stupid though.

v\#              # Fill 1..n with spaces
   ₌-----        # Apply both to the stack...
    ≬---         # The next three elements as one
     ₅*Ṅ         # Repeat each by its length and join with spaces
        ꘍        # And prepend n spaces to each #
         R       # Reverse each of those so a # is at the start
          ₅*     # Repeat each of those by the required length
            Ǐ    # Append the first character (#)
             Ȯ   # Push the item under (the #  #  #) to the stack
              JJ # Append that and prepend it
                 # (C flag) join by newlines and centre

An approach similar to Jonas' answer, but independently derived:

Vyxal j, 18 bytes

›D?*"ʀ$Ḋƒv꘍ƛ‛ #$İ∑

Try it Online!

The last 7 bytes exist because of the stupid output format, and some Vyxal bugs.

Vyxal joe, 18 bytes

v\#*Ṅðp…£v\#?꘍*Ǐ¥J

Try it Online!

The initial version of the first approach. Who's Joe?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Not one, not two, but three Vyxal bugs were found in the making of this answer. \$\endgroup\$
    – emanresu A
    Mar 14, 2022 at 4:17
4
\$\begingroup\$

Ruby, 46 bytes

->n{[z=(" "+?#*n)*n,[?#+(" "*n+?#)*n]*n,z]*$/}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

BQN, 25 bytesSBCS

Port of Jonah's excellent J answer.

" #"⊏˜·≠⌜´+⟜1(+⥊↑⟜1)¨1∾ט

Run online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 69 bytes

f n=unlines[[" # "!!(0^mod x(n+1)+0^y)|x<-[0..n*n+n]]|y<-[0..n]++[0]]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 15 bytes

'#úR×Ćи¬DÙ‡.ø»

Try it online or verify the first 5 results.

Explanation:

'#             '# Push "#"
  ú             # Pad it with the (implicit) input amount of leading spaces
   R            # Reverse it so the spaces are trailing
    ×           # Repeat this the (implicit) input amount of times as string
     Ć          # Enclose; append its own head (the "#")
      и         # Repeat it the (implicit) input amount of times as list
       ¬        # Push its first item (without popping the list)
        D       # Duplicate it
         Ù      # Uniquify its characters: "# "
          Â     # Bifurcate; short for Duplicate & Reverse copy: " #"
           ‡    # Transliterate all "# " to " #" in the duplicated string
            .ø  # Surround the list with this string as leading/trailing item
              » # Join by newlines
                # (after which the result is output implicitly)

I initially tried to use the Canvas builtin, but it ended up at twice the amount of bytes..

\$\endgroup\$
3
\$\begingroup\$

Python, 63 bytes

lambda n:"\n".join(n*(a+n*b)+a for a,b in[" #",*n*["# "]," #"])

Attempt This Online!

\$\endgroup\$
3
  • \$\begingroup\$ 60 bytes by printing at each iteration instead of returning a newline-separated string. \$\endgroup\$
    – ophact
    Mar 14, 2022 at 14:27
  • \$\begingroup\$ @ophact does this count as "joined by newlines" which OP specifically asks for? \$\endgroup\$
    – loopy walt
    Mar 14, 2022 at 14:50
  • \$\begingroup\$ Hmm, not sure about that. Better not use my modification in that case... \$\endgroup\$
    – ophact
    Mar 14, 2022 at 14:53
3
\$\begingroup\$

x86-64 machine code, 33 bytes

56 B8 20 0A B0 23 5A 52 AA 34 03 59 51 F3 AA 34 03 FF CA 75 F3 66 AB FF CE 79 E9 7A E4 88 17 59 C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the result, as a null-terminated byte string, and takes the number n in RSI.

In assembly:

f:  push rsi            # Push n onto the stack.
sl: .byte 0xB8, 0x20, 0x0A  # These bytes combine with the next instruction to form
            #  mov eax, 0x23B00A20. In particular, the lowest byte (AL) is 0x20 (' ')
            #                           and the second-lowest byte (AH) is 0x0A ('\n'). 
pl: mov al, 0x23    # (Executed for the middle lines) Set AL to 0x23 ('#').
    pop rdx; push rdx   # Set RDX to n. (This is 2 bytes; mov edx, [rsp] would be 3.)
l:  stosb               # Write AL to the string, advancing the pointer.
    xor al, 3           # Change AL from ' ' to '#' or vice versa.
    pop rcx; push rcx   # Set RCX to n.
    rep stosb           # Write AL to the string RCX times, advancing the pointer.
    xor al, 3           # Change AL from ' ' to '#' or vice versa.
    dec edx             # Subtract 1 from EDX.
    jnz l               # If it's not zero, jump back. (Repeats n times.)
    stosw               # Write AL and AH ('\n') to the string, advancing the pointer.
    dec esi             # Subtract 1 from ESI.
    jns pl              # If it's not negative (which occurs n times),
                        #  jump back to make a line starting with '#'.
    jpe sl              # Jump back to make a line starting with ' '
                        #  if the sum of the eight low bits is even.
                        #  This is true for -1 (…11111111) but not for -2 (…11111110).
    mov [rdi], dl       # Add DL (the low byte of RDX), which is 0, to the string.
    pop rcx             # Take n off the stack.
    ret                 # Return.
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 58 bytes

param($n)($t=(' '+'#'*$n)*$n);,(('#'+' '*$n)*$n+'#')*$n;$t

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 102 bytes

shorter version thanks to @ceilingcat!

i=-1,j;main(n){for(n+=scanf("%d",&n);j=i++<n;puts(""))for(;j-->n-n*n;putchar("# "[!(j%n)^(i^n&&i)]));}

Try it online!

C (GCC), 107 bytes

main(i,j,n){scanf("%d",&n);++n;for(i=-1;i++<n;puts(""))for(j=-1;j++<n*n-n;putchar("# "[!(j%n)^(i^n&&i)]));}

Attempt This Online!

Indented version:

main(i,j,n){
    scanf("%d",&n);
    ++n;
    for(i=-1;i++<n;puts(""))
        for(j=-1;j++<n*n-n;
            putchar("# "[!(j%n)^(i^n&&i)]));    
}
\$\endgroup\$
0
2
\$\begingroup\$

Python, 71 67 bytes

lambda n:"\n".join([t:=" "+f'{"#"*n} '*n,*["#"+f'{" "*n}#'*n]*n,t])

Attempt This Online!

-4 bytes from @Unrelated String

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Since this isn't recursive, you can drop the f= (leaving it as an anonymous function). You can also save a few parens with f-strings: Attempt This Online! \$\endgroup\$ Mar 14, 2022 at 1:02
  • 2
    \$\begingroup\$ Ah cheers for that, kinda forgot you can just use lambdas as anonymous functions... \$\endgroup\$
    – jezza_99
    Mar 14, 2022 at 1:06
2
\$\begingroup\$

APL+WIN, 37 bytes

Prompts for n. Index origin = 0

' #'[(v,0)∘.≠((n×n+1)⍴v←0,(n←⎕)⍴1),0]

Try it online! Thanks to Dyalog APL Classic

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 16 bytes

NθFθGH+→⁺²θ⁺ ×θ#

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input n.

Fθ

Repeat n times...

GH+→⁺²θ⁺ ×θ#

... draw a hollow polygon using the directions Right, Down, Left, Up, Right, of size n+2, using a string consisting of a space and n #s. Each polygon therefore starts at the top left of the "circle" and finishes at the top right, ready to start the next one.

\$\endgroup\$
2
\$\begingroup\$

MATL, 18 17 bytes

WBtTh!-GtQ*Q:Z)Zc

Try it online! Or verify all test cases.

Explanation

Consider input n = 3 as an example.

WB     % Implicit input: n. 2^n, convert to binary.
       % STACK: [1 0 0 0] 
tTh    % Duplicate, push 1, concatenate horizontally
       % STACK: [1 0 0 0], [1 0 0 0 1]
!-     % Transpose, subtract element-wise with broadcast
       % STACK:  [0 -1 -1 -1
                  1  0  0  0
                  1  0  0  0
                  1  0  0  0
                  0 -1 -1 -1]
GtQ*Q: % Push n, duplicate, add 1, multiply, add 1: gives n^2+n+1. Range (1-based)
       % STACK:  [0 -1 -1 -1
                  1  0  0  0
                  1  0  0  0
                  1  0  0  0
                  0 -1 -1 -1], [1 2 3 4 5 6 7 8 9 10 11 12 13]
Z)     % Horizontal indexing (1-based, modular)
       % STACK: [0 -1 -1 -1  0 -1 -1 -1  0 -1 -1 -1  0
                 1  0  0  0  1  0  0  0  1  0  0  0  1
                 1  0  0  0  1  0  0  0  1  0  0  0  1
                 1  0  0  0  1  0  0  0  1  0  0  0  1                    
                 0 -1 -1 -1  0 -1 -1 -1  0 -1 -1 -1  0]
Zc     % Convert non-zeros to '#'. Implicit display (char(0) is displayed as space)
       % STACK: [' ### ### ### '
                 '#   #   #   #'
                 '#   #   #   #'
                 '#   #   #   #'
                 ' ### ### ### ']
\$\endgroup\$
2
\$\begingroup\$

Jelly, 16 bytes

‘ð;ׯ€⁸ḍn€/ị⁾# Y

Try it online!

‘ð;ׯ€⁸ḍn€/ị⁾# Y - Main Link: integer, N
‘                - increment -> N+1
 ð               - start a new dyadic chain - f(N+1, N)
   ×             - multiply -> (N+1)×N
  ;              - concatenate -> [N+1, (N+1)×N]
    Ż€           - zero range each -> [[0..N+1], [0..(N+1)×N]
      ⁸          - chain's left argument -> N+1
       ḍ         - divides? (vectorises) -> [[1,0,...,0,1], [1,0,...,0,1,0,...,0,1,0,...,1]]
          /      - reduce by:
         €       -   for each:
        n        -     not equal (vectorises)
           ị⁾#.  - index into "# " (vectorises)
               Y - join with newline characters
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -pa, 43 bytes

say$_=$".('#'x$_.$")x$_,($/.y/ #/# /r)x"@F"

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 93 82 bytes

Jonah's observation that this pattern can be formed easily with an XOR table intrigued me, and I wanted to see if this would produce something short in Python. Unfortunately, I couldn't come up with a good way to build the required patterns golfily, and the result clocks in 30 bytes longer than the other python answer here:

lambda x:"\n".join("".join(" #"[i^j]for i in[0,*[1]*x]*x+[0])for j in[0,*[1]*x,0])

Try it online!

Edit -11 bytes: from @DLosc's idea of using lists of ints instead of strings.

Explanation

lambda x:
                                                                   for j in [0,*[1]*x,0]  ## Builds the list [0,1,1,...0], with x 1s, and loops over it
                                         for i in [0,*[1]*x]*x+[0]                        ## Builds the list [0,1,1,...0,1,1,...0,....], i.e. [0,1...] with x 1s, repeated x times, with an extra 0 at the end
                           (" ","#")[i^j]                                                 ## '#' if i xor j, else ' '
                   "".join(                                       )                       ## .join the characters of this line
         "\n".join(                                                                     ) ## Encapsulate it in a list for output              
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Nice approach--too bad it's not shorter. Here's 82 bytes using lists of ints instead of strings. \$\endgroup\$
    – DLosc
    Mar 22, 2022 at 22:09
  • \$\begingroup\$ oh nice one @DLosc! for a while I had been experimenting with a truly bizarre idea using bin() and the fact that 1 and b have the same codepoint mod 49, but it, rather unsurprisingly, didnt really pan out \$\endgroup\$
    – des54321
    Mar 22, 2022 at 22:13
2
\$\begingroup\$

Mathematica 12, 83 bytes

StringRiffle[StringJoin/@Table[If[#∣i⊻#∣j,"#"," "],{j,0,#},{i,0,#2#}]&[#+1,#],"\n"]&

Uses the obvious divisibility trick, and Mathematica's good unicode parsing

Try it online!

\$\endgroup\$
2
\$\begingroup\$

flax C, 21 19 18 bytes

₊ #iZŻ≠⌜ŻF₎⍴⊂µ¬W1⍪

Not happy with the byte count...

Slightly better.

-1 byte by implicit joining of newlines.

\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 79 bytes

.+
$*
1
$_¶
^
$%' #¶
¶$
¶$%` #
\b¶
# ¶
%(`1
$_¶
1(?=.*(.))
$1
.¶

(.+)(.).
$2$1

Try it online! Explanation:

.+
$*

Convert to unary.

1
$_¶

Replicate the line of n 1s n times.

^
$%' #¶

Prefix a line of n 1s with a suffix of #. This indicates the pattern for the first line.

¶$
¶$%` #

Append another such line.

\b¶
# ¶

Append # to the original n lines. These indicate the pattern for the intermediate lines.

%(`

Apply the rest of the script separately to each line.

1
$_¶

Repeat each line n times. This also leaves an extra copy of the two pattern characters on a line on its own.

1(?=.*(.))
$1

Replace each 1 with the last character on the line.

Delete last character on each line as it is joined with the next line.

(.+)(.).
$2$1

Move the final copy of the first pattern character to the start of the entire line and delete the final copy of the second pattern character.

\$\endgroup\$
1
\$\begingroup\$

Retina, 40 bytes

.+
*$( $&*#)¶$&*$(*$(#$&* )#¶)$&*$( $&*#

Try it online! Link includes test cases. Explanation: Just some boring string repetition. The final line parses as $&*$( $&*#)¶$&*$($&*$(#$&* )#¶)$&*$( $&*#) where the $() is a grouping construct and $&* repeats the character or group the number of times given by the input.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 66 65 bytes

n=>(t=(r=s=>s.repeat(n))(' '+r('#'))+`
`)+r(r('#'+r(' '))+`#
`)+t

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 86 bytes

t=(.cycle).take
f n=unlines$t(n+2)[t(n^2+n+1)[" # "!!(x+y)|x<-l]|y<-l]where l=1:t n[0]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Headascii, 121 bytes

+OUO{^)+E:ROD-}.UOD++++[]]]][]]O[UO{N-)!@++E:]PUO{^):]+++PD-};}.UO)++++E:U[{N-)+++E:]+++PUO{^):]PD-};}.OUUO{^)@+E:RO!D-}.

Whew!

Try it here! Code will have to be copied and executed as shown:

erun("+OUO{^)+E:ROD-}.UOD++++[]]]][]]O[UO{N-)!@++E:]PUO{^):]+++PD-};}.UO)++++E:U[{N-)+++E:]+++PUO{^):]PD-};}.OUUO{^)@+E:RO!D-}.","your input here")

But also, it takes input by charcode, so for example if you wanted to input 4, you'd type

"\u0004"

Which I think is perfectly valid :D

Code breakdown:

+OUO{^)+E:ROD-}. block 0
+O               push 1 to global as a flag
  UO             get n/push n to global
  U {^)  :  D-}  loop n times
          RO       push n to global
       +E      . goto block 1
     
UOD++++[]]]][]]O[UO{N-)!@++E:]PUO{^):]+++PD-};}. block 1
UOD                                              push flag to global
   ++++[]]]][]]O                                 push 32 to global
                [                                and store it
                 UO                              push n to global
                   {N-)     :  U              }  loop n times
                             ]P                    append 32 to string
                               UO                  push n to global
                               U {^):     D-};     loop n times
                                     ]+++P           append 35 to string
                       !@                        print string w/trailing
                                                 newline and clear string
                         ++E                   . goto block 2
            
UO)++++E:U[{N-)+++E:]+++PUO{^):]PD-};}. block 2
UO)                                      if flag is 0
   ++++E                                  goto block 5
        :                               else
         U[                               store 32
           {N-)    :     U           }    loop n plus 1 times
                    ]+++P                   append 35 to string
                         UO                 push n to global
                           {^):  D-};       loop n times
                               ]P             append 32 to string
               +++E                   .   goto block 3

OUUO{^)@+E:RO!D-}. block 3
O                  push 0 to global as flag
 U                 discard previous flag state
  UO               push n to global
  U {^)   :   D-}  loop n times
           RO        push n to global
             !       print string w/trailing newline
       @           clear string
        +E       . goto block 1
        
block 4 is empty

Using the global array length as an extra register is a fun trick i just learned doing this :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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