21
\$\begingroup\$

Write a function (or a whole program) that outputs (or prints) the following ASCII art:

Output:

                    1
            1       2       1
      1      2      3      2      1
  1     2     3     4     3     2     1
1    2    3    4    5    4    3    2    1
1   2   3   4   5   6   5   4   3   2   1
  1  2  3  4  5  6  7  6  5  4  3  2  1
      1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
            12345678987654321
      1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1  2  3  4  5  6  7  6  5  4  3  2  1
1   2   3   4   5   6   5   4   3   2   1
1    2    3    4    5    4    3    2    1
  1     2     3     4     3     2     1
      1      2      3      2      1
            1       2       1
                    1

This is , so the shortest solution in bytes wins!

Allowed:

  • Any number of trailing spaces on each line
  • An equal number of additional leading spaces on each line (meaning of additional: without counting the already mandatory leading spaces shown in the output above)
  • Additional newlines at the beginning and/or at the end. These additional lines can contain any number of spaces

I'm late to the party, but as some of you may have recognized, this challenge is inspired by the bugged output in Carcigenicate's answer in Clojure for the Print this diamond challenge.

This answer also led to a chain of 3 challenges from Kevin Cruijssen : It was just a bug, It was just an input-bug, and I done did made a spaceship maw!

But i wanted to see something more "pulsar-y", as this is what i find most beautiful in the original answer!

I hope you will generate beautifully bugged ASCII arts while doing this challenge :)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can we output as a list of lines? \$\endgroup\$ Jul 21 at 18:53
  • \$\begingroup\$ @cairdcoinheringaahing I'm not fond of the idea, but apparently multiple solutions already took the initiative of doing it, so i'll allow it. \$\endgroup\$
    – Fhuvi
    Jul 21 at 23:55
  • 1
    \$\begingroup\$ Rather than 'pulsar-y', this challenge looks more like a real diamond! Or, well, a brilliant-cut diamond seen from the top; point stands :p. \$\endgroup\$ Jul 23 at 22:47

23 Answers 23

8
\$\begingroup\$

Python 3, 80 bytes

i=8
while i+9:k=abs(i);print(f"{(' '*k).join(str((10**(9-k)//9)**2)):^41}");i-=1

Try it online!

Uses the classic arithmetic trick where, for example,

(10**9//9)**2 == 111111111**2 == 12345678987654321

Python 3, 79 bytes

f=lambda k=8,c=1:(s:=[f"{(' '*k).join(str(c*c)):^41}"])[k:]or s+f(k-1,c*10+1)+s

Try it online!

Recursive function that generates a list of lines when called with no inputs. The idea is to start with the center line, then repeatedly sandwich what we have between two copies of a new line.

Python 3, 79 bytes

f=lambda p=8*' ',c=1:(s:=f"{p.join(str(c*c)):^41}\n")+(p and f(p[1:],c*10+1)+s)

Try it online!

Recursive function that generates the whole multiline string, with a trailing newline, when called with no input.

\$\endgroup\$
7
\$\begingroup\$

Thunno 2, 14 bytes

9Rƒıṅ8_ṣjⱮ;ⱮØC

Try it online!

Seems like the compressed list part (¿qJŒḣ“ɲþẇɦɠ¿i) could be golfed a bit with a formula, but I couldn't find it.

I found the formula \$(n-6)(n-5)\$ for the leading spaces.

I don't need that any more. Port of Kevin Cruijssen's 05AB1E answer.

-5 by porting @KevinCruijssen's 05AB1E answer

Explanation

9Rƒıṅ8_ṣjⱮ;ⱮØC  # Full program
9Rƒ             # Prefixes of [1..9]
   ı      ;     # Map over:
    ṅ8_         #  8 - iteration index
       ṣj       #  Join by that many spaces
         Ɱ      #  Palindromise
           Ɱ    # Palindromise
            ØC  # Center
                # Implicit output

Old:

9RⱮı65d-pṣnRⱮn9_ṣj+  # Full program
9RⱮı                 # Map over [1,2,...,8,9,8,...,2,1]:
    65d-pṣ           #  Push (n-6) * (n-5) spaces
          nRⱮ        #  Push [1,2,...,n,...2,1]
             n9_ṣj   #  Join by (9-n) spaces
                  +  #  Prepend the leading spaces
                     # Implicit output, joined on newlines
\$\endgroup\$
6
\$\begingroup\$

Vyxal, 12 bytes

9ƛɾ9n-Ij;∞øṗ

Try it Online!

Uses the strangely specific øṗ (Flip Brackets Vertical Palindromise, Center, Join on Newlines) builtin.

9ƛ      ;         for n in [1...9]:
  ɾ9n-Ij            join [1..n] by 9-n spaces
         ∞        palindromize the list
          øṗ      palindromize and center all the strings and join by \n
\$\endgroup\$
1
6
\$\begingroup\$

Ruby, 71 69 bytes

Function outputting an array of lines. A synthesis of my old answer with Level River St's answer as per their suggestions.

-2 bytes by switching to Ruby 2.7+ (TIO is on 2.5.5)

->{(-8..8).map{([*1...x=9-k=_1.abs,*x.downto(1)]*(' '*k)).center 41}}

Attempt This Online!

Ruby, 77 bytes

Recursion, ahoy! Function outputting an array of lines.

f=->x=1{s=([*1...x,*x.downto(1)]*(' '*(9-x))).center 41;x<9?[s,*f[x+1],s]: s}

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Thanks for the tips on my answer. Here's 72 bytes, combining the best of both of our ideas: (-8..8).map{|i|puts ([*1...x=9-k=i.abs,*x.downto(1)]*(' '*k)).center 41} \$\endgroup\$ Jul 22 at 4:07
  • \$\begingroup\$ @LevelRiverSt Good ideas! It also turns out keeping it as a function saves an extra byte as well. \$\endgroup\$
    – Value Ink
    Jul 24 at 22:09
5
\$\begingroup\$

Charcoal, 17 bytes

E⁹⪫⮌…·¹⊕ι× ⁻⁸ι‖O¬

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

E⁹⪫⮌…·¹⊕ι× ⁻⁸ι

For n from 1 to 9, output the numbers from n down to 1 joined with 9-n spaces, but doing everything 0-indexed.

‖O¬

Reflect both left and down to complete the diamond gone beautifully wrong.

\$\endgroup\$
5
\$\begingroup\$

Pip -l, 23 bytes

QPZJ RZJ:R\,_JsX9-_M\,9

Attempt This Online!

Explanation

QPZJ RZJ:R\,_JsX9-_M\,9
                    \,9  ; Range from 1 to 9, inclusive
                   M     ; Map this function to each:
          \,_            ;   Range from 1 to fn arg, inclusive
         R               ;   Reversed
             J           ;   Join on this string:
              sX         ;   A number of spaces equal to
                9-_      ;   9 minus fn arg
      ZJ:                ; Transpose list of strings, filling gaps with spaces
     R                   ; Reverse list of lines
  ZJ                     ; Transpose again
QP                       ; Quad-palindromize
                         ; Autoprint, joining on newlines (-l flag)

Here's the step-by-step evolution, using size 5 instead of 9 and showing spaces using periods:

\,5

1
2
3
4
5

R\,_JsX5-_M

1
2...1
3..2..1
4.3.2.1
54321

ZJ:

12345
....4
...33
..2.2
.1.21
.....
..11.

R

..11.
.....
.1.21
..2.2
...33
....4
12345

ZJ

......1
..1...2
1..2..3
1.2.3.4
..12345

QP

......1......
..1...2...1..
1..2..3..2..1
1.2.3.4.3.2.1
..123454321..
1.2.3.4.3.2.1
1..2..3..2..1
..1...2...1..
......1......
\$\endgroup\$
4
\$\begingroup\$

Jelly, 20 bytes

9Rjạ9⁶xƊƊ€Uz⁶ZUŒBŒḄY

A full program that prints the diamond.

Try it online!

How?

9Rjạ9⁶xƊƊ€Uz⁶ZUŒBŒḄY - Main Link: no arguments
9                    - 9
         €           - for each {i in [1..9]}:
        Ɗ            -   last three links as a monad f(i):
 R                   -     range -> [1..i]
       Ɗ             -     last three links as a monad f(i):
   ạ9                -       absolute difference with 9 -> 9-i
     ⁶               -       space character
      x              -       repeat -> ' '*(9-i)
  j                  -     join -> e.g. "1      2      3"
          U          - reverse each
           z⁶        - transpose with space character as filler
             Z       - transpose
              U      - reverse each
               ŒB    - bounce each
                 ŒḄ  - bounce
                   Y - join with newline characters
                     - implicit, smashing print
\$\endgroup\$
4
\$\begingroup\$

Ruby, 83 bytes

Updated per suggestion from @ValueInk

(-8..8).map{|i|puts (((-8+c=i.abs)..8-c).map{|j|"#{9-c-j.abs}"}*(" "*c)).center 41}

Try it online!

Ruby, 86 bytes

(-8..8).map{|i|puts (((-8+c=i.abs)..8-c).map{|j|(9-c-j.abs).to_s}*(" "*c)).center(41)}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ "#{9-c-j.abs}" saves 4 bytes over .to_s and you can lose the parens on center(41) for 1 more. \$\endgroup\$
    – Value Ink
    Jul 21 at 20:25
  • \$\begingroup\$ @ValueInk The saving for "#{9-c-j.abs}" is only 2 bytes, but thanks anyway! \$\endgroup\$ Jul 22 at 4:14
3
\$\begingroup\$

Swift, 156 bytes

String(repeating:count) is nice for practical code, but so long for code golf...

let f={(1...$0)+(1..<$0).reversed()}
f(9).map{n in print(.init(repeating:" ",count:(n-6)*(n-5))+f(n).map{"\($0)"+String(repeating:" ",count:9-n)}.joined())}

Try it on SwiftFiddle!

Port of @TheThonnu's various answers. f is a shorthand for counting from 1 to n and back. I put the joining spaces inside the map callback rather than passing them to joined because the latter would involve an extra argument label (joined(separator:)).

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

Scala, 214 bytes

Port of @Level River St's Ruby answer in Scala.


Golfed version. Try it online!

object M extends App{(-8 to 8).map{i=>val c=i.abs
println((" "*(20-((-8+c)to(8-c)).map{j=>(9-c-j.abs).toString}.mkString(" "*c).length/2)+((-8+c)to(8-c)).map{j=>(9-c-j.abs).toString}.mkString(" "*c)).slice(0,41))}}

Ungolfed version. Try it online!

object Main {
  def main(args: Array[String]): Unit = {
    (-8 to 8).map { i =>
      val c = i.abs
      val line = ((-8 + c) to (8 - c)).map { j =>
        (9 - c - j.abs).toString
      }.mkString(" " * c)
      println(center(line, 41))
    }
  }
  
  def center(s: String, width: Int): String = {
    val padSize = width - s.length
    if(padSize <= 0) s
    else (" " * (padSize/2)) + s + (" " * (padSize - padSize/2))
  }
}

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

05AB1E, 16 15 bytes

9Lηε8N-ð×ýû}û.c

Try it online.

Explanation:

9L           # Push a list in the range [1,9]
  η          # Pop and push its prefixes: [[1],[1,2],[1,2,3],...]
   ε         # Map over each prefix:
    8N-      #  Push 8 minus the 0-based map index
       ð×    #  Pop and push a string of that many spaces
         ý   #  Join the list with those spaces (or empty string for N=8) as delimiter
          û  #  Palindromize this string
   }û        # After the map: palindromize the list of lines as well
     .c      # Join the list by newlines,
             # and add leading spaces where necessary to centralize the lines
             # (after which the result is output implicitly)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ To whoever downvoted: any reason why? The output is correct as far as I can tell: verify here? \$\endgroup\$ Jul 24 at 11:27
  • 1
    \$\begingroup\$ @KevinCruissjen I think the downvote is from a troll or a bot: the challenge and 4 of the answers got a -1 for no reason in an interval of only a few seconds/minutes \$\endgroup\$
    – Fhuvi
    Jul 24 at 12:07
  • 2
    \$\begingroup\$ @Fhuvi Ah, is it that time of year again. ;) Yeah, I've seen it a few times before in the past, where all answers had a downvote on a single challenge. Ah well. Nice challenge btw! I think I'm gonna wait a while before posting a challenge based on this output now, haha. :D \$\endgroup\$ Jul 24 at 12:22
3
\$\begingroup\$

Vyxal, 16 bytes

9ɾKėƛ÷$8εIj∞;∞øĊ

Try it Online!

Port of my Thunno 2 answer Kevin Cruijssen's 05AB1E answer.

-1 thanks to @lyxal
-3 by porting @KevinCruijssen's 05AB1E answer

Explanation

9ɾKėƛ÷$8εIj∞;∞øĊ  # Full program
9ɾK               # Prefixes of [1..9]
   ėƛ       ;     # Enumerate and map over:
     ÷$8ε         #  Absolute difference between 8 and the iteration index
         Ij       #  Join by that many spaces
           ∞      #  Palindromise
             ∞    # Palindromise
              øĊ  # Center
                  # Implicit output

Old:

9ɾ∞ƛ65f-ΠInɾ∞n9εIj+  # Full program
9ɾ∞ƛ                 # Map over [1,2,...,8,9,8,...,2,1]:
    65f-ΠI           #  Push (n-6) * (n-5) spaces
          nɾ∞        #  Push [1,2,...,n,...2,1]
             n9εIj   #  Join by (9-n) spaces
                  +  #  Prepend the leading spaces
                     # Implicit output, joined on newlines
\$\endgroup\$
1
3
\$\begingroup\$

Perl 5, 65 bytes

$a=-1-abs,print map/\D|0/?$":$_,map$a+10+abs()/$a,-20..20for-8..8

Try it online!

I'd count it 63+1 (instead of 65), the +1 for -E and say instead of print, can't figure out how to get it in TIO which also doesn't add 1 for -l switch. But it beats direct competitors anyway :), it's strange nobody used this strategy.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$
    – The Thonnu
    Jul 29 at 8:13
2
\$\begingroup\$

JavaScript (V8), 102 bytes

A full program.

for(n=17;n--;print(s))for(q=n<9?n:16-n,x=9-q,i=s=''.padEnd(20-q*x);~q;)s+=`${++i>q?1+q--:i}`.padEnd(x)

Try it online!


C (gcc), 116 bytes

-1 thanks to @c--

q;x;i;main(n){for(n=17;n--;i=!puts(""))for(q=n<9?n:16-n,x=9-q;~q;q-=i>q)printf("%*d",i>1?x:x+12-q*x+q,++i>q?1+q:i);}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ What exactly does [...Array(k)] achieve? \$\endgroup\$
    – Bbrk24
    Jul 21 at 17:25
  • \$\begingroup\$ @Bbrk24 It generates an array of k undefined entries. \$\endgroup\$
    – Arnauld
    Jul 21 at 17:30
  • \$\begingroup\$ ...right, because JS distinguishes "array of missing items" from "array with undefined items". I forgot about that. \$\endgroup\$
    – Bbrk24
    Jul 21 at 17:31
  • 1
    \$\begingroup\$ -1 byte for the C program. It relies on puts(3) not returning 0 though, so here's an alternative after some rearranging \$\endgroup\$
    – c--
    Jul 21 at 21:56
2
\$\begingroup\$

Python 3, 107 bytes

R=range
for n in[*R(1,9),*R(9,0,-1)]:print((n-6)*(n-5)*' '+((9-n)*' ').join(map(str,[*R(1,n),*R(n,0,-1)])))

Try it online!

Port of my Thunno 2 answer.

-5 thanks to @Neil

\$\endgroup\$
2
  • \$\begingroup\$ [*R(1,9),*R(9,0,-1)] saves 1 byte and [*R(1,n),*R(n,0,-1)] saves 4! \$\endgroup\$
    – Neil
    Jul 22 at 9:14
  • \$\begingroup\$ @Neil good spot, thanks \$\endgroup\$
    – The Thonnu
    Jul 22 at 9:21
2
\$\begingroup\$

Retina, 60 bytes


9* 
 
$`;$'¶
v`( *;)( *)
$.1$2
P`.+
%`^.
$^$<'
L$`9
$>`$^$`

Try it online! Explanation:


9* 

Insert 9 spaces.

 
$`;$'¶

Generate a 9×9 square of spaces, but with ;s on the main diagonal.

v`( *;)( *)
$.1$2

Use the spaces before the ; to generate the numbers from n down to 1 on the nth row, spaced using the spaces after the ;.

P`.+
%`^.
$^$<'

Reflect horizontally.

L$`9
$>`$^$`

Reflect vertically (actually around the central 9 but it comes to the same thing).

92 bytes in Retina 0.8.2:


9$* 
 
$`;$'¶
(?=( *;)( *)).
$.1$2
.+
$&12$* 
(.(.{20})).*
$2$1
%20O^$`.

¶.+¶$
$&$`
9>O^$`

Try it online! Explanation:


9$* 

Insert 9 spaces.

 
$`;$'¶

Generate a 9×9 square of spaces, but with ;s on the main diagonal.

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

Use the spaces before the ; to generate the numbers from n down to 1 on the nth row, spaced using the spaces after the ;.

.+
$&12$* 
(.(.{20})).*
$2$1
%20O^$`.

Reflect horizontally by prefixing each line with a copy of the 20 characters after the first and then reversing those 20 characters.

¶.+¶$
$&$`
9>O^$`

Reflect vertically by duplicating all the lines except the last and then reversing only the duplicates.

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

05AB1E, 21 bytes

9Lûε65S-Pð×yLûy9αð×ý«

Try it online!

Port of my Thunno 2 answer. Unfortunately 05AB1E doesn't have the "push n spaces" built-in that Thunno 2 and Vyxal have.

Output as a list of lines. TIO footer joins by newlines.

-2 thanks to @KevinCruijssen

Explanation

9Lûε65S-Pð×yLûy9αð×ý«  # Implicit input
9Lûε                   # Map over [1,2,...,8,9,8,...,2,1]:
    65S-P×J            #  Push (n-6) * (n-5) spaces
           yLû         #  Push [1,2,...,n,...2,1]
              y9αð×ý   #  Join by (9-n) spaces
                    «  #  Prepend the leading spaces
                       # Implicit output
\$\endgroup\$
2
  • \$\begingroup\$ Both иJ can be × for -2 bytes. Although with the centralize and mirror builtins in addition to the palindromize, 16 bytes is possible. :) \$\endgroup\$ Jul 24 at 10:32
  • \$\begingroup\$ @KevinCruijssen wow... time to port to Thunno 2 and Vyxal :) \$\endgroup\$
    – The Thonnu
    Jul 24 at 15:38
2
\$\begingroup\$

Zsh, 108 bytes

for c ({1..9} {8..1}){w=$[(10**c/9)**2];printf \\n%$[(5-c)*(6-c)]s
for x (${(s::)w})printf %-$[9-$#w/2]s $x}

Try it online!

Adapted from @Donat's "bash diamond" code and my "spaceship maw" solution.

\$\endgroup\$
1
  • \$\begingroup\$ TODO: Try iterating c over {-8..8} per some other solutions here... \$\endgroup\$
    – roblogic
    Oct 1 at 14:41
2
\$\begingroup\$

J, 45 bytes

8({.~#-@+41-:@-#)@dlb@(9&-":1+]-|@i:)@-|@i:@8

Try it online!

Meh.

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

C (gcc), 98 bytes

j;main(z,x){for(;z<10;putchar(x>20?10:x%z|(x=58-z-abs(x/z))<49?32:x))x=j%43-21,z=abs(j++/43-8)+1;}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Suggest z<10 -> 9/z \$\endgroup\$
    – c--
    Jul 24 at 3:14
1
\$\begingroup\$

C (gcc), 98 bytes

i,c=8;main(j){for(;--i/j?i=j=9-abs(c--):1;)printf("%*c",i-j?j-10:11*i-i*i-31,i-j?48+j-abs(i):10);}

Try it online!

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

JavaScript (V8), 98 bytes

I don't know if it's very polite to answer my own challenge, but here it is!

for(z of(z=9,s=(n=1,p="".padEnd(9-z))=>n<z?n+p+s(n+1,p)+p+n:z)())print("".padEnd((6-z)*(5-z))+s())

Try it online!


This is the combined works of Donat's recent Javascript answer to the challenge "Print this diamond"
and The Thonnu's formula: (n−6)(n−5) for the leading spaces.

@Donat, don't hesitate if you want to post this as your own answer, this is mostly your work after all :)

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

Vyxal, 278 Bytes

»×ẏ∴⁺¢ɽƒ3∴ĿṘ€ȯLdd2)"lY∇λµ≠Ḟw⅛!ḋ₌₁ẊṘ?
‡₄¥D‹Ḣ€ɾȯḟǑLdẇm₁≤ɖ↵$hḭ«€X⇧0A2&/•p[₀‟₃5Ǔ≈Π>ḋṠßĠ-⋏↳ĖOUḃ`∑ß∨(V⋏⋎ṁ7Ż≥×h'D℅p×>¥D↲‛℅ddlʀɾ⌐D%₆₄⁋ŻṘġȧ₅ḣ'ɾp0Ḋ¢lǐ⟨6K⌊∵¶⟨Ḣ,g#;¬‛^4⟨β•qrṠg₅Þf⋎→__↑¢[ẏNcḟ≠H꘍C¼↔Π꘍∧≬ǏḭḞǔṄꜝ⌐ø₇µ`ʁȯ⁋√ÞVe↲ <†∧↔⊍n÷PḂė?ġ¦$₌$e⋎\]↵⇩ṁn€⋏+₀ǒ&Ċ⌐∩ṁṗ½VḃḞβ#WżL∵ṖðǓmẊOur,8‹»`1 
23456789`τ

Compressed number converted to custom string base.

Try it online!

\$\endgroup\$
4
  • 6
  • 2
    \$\begingroup\$ @TheThonnu It doesn't even look that compressed; Charcoal can compress the entire output in 125 bytes. \$\endgroup\$
    – Neil
    Jul 21 at 18:40
  • 1
    \$\begingroup\$ Yeah for Vyxal this is uncommonly long, even PHP can do better with GZip <?=gzinflate('•‘KÀ D÷Mz¯à€hï±J Ú5QæÅ–òõ<¦Ò#V»N:M•A5)Í)ÇÖ¨(˜…ë(\dÇÄ#»Žt<Lb”ë¶6SÈÜô¨ÄjŠbC§hΪáÓ7éã]ö÷ؼÑWþèÝölþnjH™ÜÕì');` for 143 bytes (it won't work here, many special chars) \$\endgroup\$
    – Kaddath
    Jul 24 at 17:50
  • \$\begingroup\$ The try it online link ends up as blank code \$\endgroup\$
    – Jo King
    Sep 3 at 23:57

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.