# Let's design a digit mosaic

## Challenge

Given a positive integer $N$, repeat each of its digits $d_1, d_2, d_3, \cdots, d_n$ a number of times corresponding to its position in $N$. In other words, each digit $d_k$ should be repeated $k$ times (for each $1\le k\le n$, 1-indexed), thus creating the new number:

$$\overline{d_1d_2d_2d_3d_3d_3\cdots\underbrace{d_nd_nd_n\cdots d_n}_{n\text { times}}}$$

Then, write it down both horizontally and vertically and fill in the blanks with copies of the digit that corresponds to the greater index between the column index and the row index of the blank space. The final output should look like this:

$$\begin{bmatrix} \color{red}{d_1} \color{green}{d_2 d_2} \color{blue}{d_3 d_3 d_3} \cdots \\ \color{green}{d_2 d_2 d_2} \color{blue}{d_3 d_3 d_3} \cdots \\ \color{green}{d_2 d_2 d_2} \color{blue}{d_3 d_3 d_3} \cdots \\ \color{blue}{d_3 d_3 d_3 d_3 d_3 d_3} \cdots \\ \color{blue}{d_3 d_3 d_3 d_3 d_3 d_3} \cdots \\ \color{blue}{d_3 d_3 d_3 d_3 d_3 d_3} \cdots \\ \vdots \end{bmatrix}$$

## Specs

You may take $N$ as an integer, a string, a list of digits or a list of characters representing the digits. The output can be a newline-separated string, a list of strings / integers or a list of lists of characters / digits, but please include a pretty-print version too, if possible. If the output is a newline-separated string, it is also acceptable to:

• have leading / trailing whitespace, as long as the visual appearance of the output doesn't change
• separate the columns using a consistent amount spaces or the rows with a consistent (non-zero) amount of newlines

You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is , so try to complete the task in the least bytes you can manage in your language of choice.

## Test cases

65:

655
555
555

---------------

203:

200333
000333
000333
333333
333333
333333

--------------

233:

233333
333333
333333
333333
333333
333333

---------------

5202:

5220002222
2220002222
2220002222
0000002222
0000002222
0000002222
2222222222
2222222222
2222222222
2222222222

---------------

12345:

122333444455555
222333444455555
222333444455555
333333444455555
333333444455555
333333444455555
444444444455555
444444444455555
444444444455555
444444444455555
555555555555555
555555555555555
555555555555555
555555555555555
555555555555555
• Do we have the handle two of the same digit next to one another? – Dom Hastings Jun 25 '18 at 12:04
• @DomHastings Yes, you have to handle them. Added a test case illustrating this. – Mr. Xcoder Jun 25 '18 at 12:05
• Related – Magic Octopus Urn Jun 25 '18 at 13:57

# JavaScript (ES7), 70 bytes

Takes input as a string. Returns a string with a trailing linefeed.

s=>(g=x=>(c=s[(x>y?x:y)**.5-1>>1])?c+g(x+8):x>y?
+g(1,y+=8):'')(y=1)

Try it online!

## How?

### Method

We build the output character by character by walking through a square matrix and converting each cell into an index $i_{x,y}$ into the input string.

### Coordinates to string index

The upper bound $u_{n}$ of the $n^{th}$ digit area (0-indexed) along each axis is given by A000096:

$$u_{n} = \frac{n(n+3)}{2}$$ $$u_{0}=0,u_{1}=2,u_{2}=5,u_{3}=9,u_{4}=14,u_{5}=20,\dots$$

Given an integer $k$, we can find out in which area $n=\lfloor{x}\rfloor+1$ it is located by solving:

$$x²+3x-2k=0$$

$$x = \frac{\sqrt{1+8k}-3}{2}$$ $$n = \left\lfloor\frac{\sqrt{1+8k}-3}{2}\right\rfloor+1=\left\lfloor\frac{\sqrt{1+8k}-1}{2}\right\rfloor$$

For each cell $(x, y)$, we define:

$$v_{x,y} = \max(1+8x,1+8y)$$

These values $v_{x,y}$ are converted into indices $i_{x,y}$ into the input string by doing:

$$i_{x,y} = \left\lfloor\frac{\sqrt{v_{x,y}}-1}{2}\right\rfloor$$

v(x,y) |  0  1  2  3  4  5  6  7  8  9        i(x,y) |  0  1  2  3  4  5  6  7  8  9
--------+-------------------------------      --------+-------------------------------
0   |  1  9 17 25 33 41 49 57 65 73           0   |  0  1  1  2  2  2  3  3  3  3
1   |  9  9 17 25 33 41 49 57 65 73           1   |  1  1  1  2  2  2  3  3  3  3
2   | 17 17 17 25 33 41 49 57 65 73           2   |  1  1  1  2  2  2  3  3  3  3
3   | 25 25 25 25 33 41 49 57 65 73           3   |  2  2  2  2  2  2  3  3  3  3
4   | 33 33 33 33 33 41 49 57 65 73   -->     4   |  2  2  2  2  2  2  3  3  3  3
5   | 41 41 41 41 41 41 49 57 65 73           5   |  2  2  2  2  2  2  3  3  3  3
6   | 49 49 49 49 49 49 49 57 65 73           6   |  3  3  3  3  3  3  3  3  3  3
7   | 57 57 57 57 57 57 57 57 65 73           7   |  3  3  3  3  3  3  3  3  3  3
8   | 65 65 65 65 65 65 65 65 65 73           8   |  3  3  3  3  3  3  3  3  3  3
9   | 73 73 73 73 73 73 73 73 73 73           9   |  3  3  3  3  3  3  3  3  3  3

### Halting conditions

We know that we've reached:

• the right boundary of the matrix when the character at $i_{x,y}$ does not exist and we have $x > y$

• the bottom boundary of the matrix when the character does not exist and we have $x \le y$

# J, 16 15 bytes

-1 byte thanks to FrownyFrog!

{~#\<:@>./~@##\

Try it online!

Takes N as a string.

## Explanation of th initial solution:

#\   finds the length of the successive prefixes of the input (1 2 3...)
#~     copies each digit as many times (1 2 2 3 3 3...)
>./~@       and creates a table of the max of the row/col numbers
[:<:@            then subtract 1 from each element (for indexing)
{~                 select the corresponding digit from the input

Test session with input 203:

#\ '203'
1 2 3

#~#\ '203'
1 2 2 3 3 3

>./~@#~#\ '203'
1 2 2 3 3 3
2 2 2 3 3 3
2 2 2 3 3 3
3 3 3 3 3 3
3 3 3 3 3 3
3 3 3 3 3 3

<:@>./~@#~#\ '203'
0 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2

({~[:<:@>./~@#~#\) '203'
200333
000333
000333
333333
333333
333333
• Hah, apart from the placement of the ), your APL answer is the same as the would-have-been mine. – Erik the Outgolfer Jun 23 '18 at 9:13
• I really don't know J at all, but [:<:@ seems quite costly. Could you instead prepend something to the list you are indexing into to account for the 1-indexing (e.g. prepend a 0 in order to move each necessary element 1 position to the right)? – Mr. Xcoder Jun 23 '18 at 10:32
• @Mr.Xcoder I was thinking about that. I'll try it to see if it would save some bytes. – Galen Ivanov Jun 23 '18 at 11:24
• @EriktheOutgolfer {⍵[∘.⌈⍨(/⍨)⍳⍴⍵]} ? – Galen Ivanov Jun 23 '18 at 11:31
• @GalenIvanov Yes, that. – Erik the Outgolfer Jun 23 '18 at 11:58

# Jelly, 7 bytes

Jx»þị

Try it online!

Clarified output.

f x|s<-do(n,_)<-zip[0..]x;n<$[0..n]=[(x!!).max a<$>s|a<-s]

Try it online!

(map=<<(.((snd.).max)).flip map).((\t@(n,c)->t<$[1..n])=<<).zip[1..] Try it online! # Python 2, 71 bytes i=j=0;r='' for x in input():i+=1;r+=x*i for c in r:print j*c+r[j:];j+=1 Try it online! First generates the first row r, then iterates over r to print each line. # R, 59 bytes function(a){m=outer(x<-rep(g<-seq(a),g),x,pmax);m[]=a[m];m} Try it online! • I noticed that taking a vector of digits is acceptable, and this allowed me to save 21 bytes :) • -2 bytes thanks to @Giuseppe suggestion to accept only character vector • -2 bytes assigning in arguments definition • You could take a as a vector of characters, allowing you to set g=seq(a) directly. – Giuseppe Jun 23 '18 at 12:25 • @Giuseppe: that's right! – digEmAll Jun 23 '18 at 12:26 # APL (Dyalog Classic), 16 bytes {⍵[∘.⌈⍨(/⍨⍳⍴⍵)]} I'm separating this solution from the post with my J answer, as suggested by Jo King Try it online! # 05AB1E, 1411 10 bytes Saved 1 byte thanks to Magic Octopus Urn / Adnan ƶJDv¬N×?=¦ Try it online! Explanation ƶ # repeat each element its index (1-based) times J # join to string Dv # for N in [0 ... len(string)-1] do ¬N× # push the head repeated N times ? # print without newline = # print the rest of the string without popping ¦ # remove the head # Python 2, 74 bytes i=1;a=[] for c in input():exec"a=zip(*a+[c*-~len(a)]);"*i;i+=2+i%2 print a Try it online! # Excel VBA, 95 bytes An anonymous VBE immediate window funtion that takes input from [A1] and outputs to the console n=[len(A1)]:For y=1To n:For l=1To y:?:For x=1To n:?String(x,Mid([A1],IIf(x>y,x,y)));:Next x,l,y ### Ungolfed and commented n=[len(A1)] '' Get Length For y=1To n '' Iterate down input For l=1To y '' Iterate down repeat lines ? '' Print Newline For x=1To n '' Iterate accross input ?String(x,Mid([A1],IIf(x>y,x,y))); '' Print x of the max(x,y)th digit in input Next x,r,y '' Loop, Loop, Loop # MATL, 15 12 bytes tftY"t!2$X>)

Try it online!

I suspect this can be shortened, but it's not so bad...

% implicit input, '230'
t         % duplicate input. Stack: ['230','230']
f         % indices of nonzero values. Stack: ['230',[1,2,3]]
t         % duplicate. Stack: ['230',[1,2,3],[1,2,3]]
Y"        % run-length decoding. Stack: ['230',[1,2,2,3,3,3]]
t         % duplicate. Stack: ['230',[1,2,2,3,3,3],[1,2,2,3,3,3]]
!         % transpose. Stack: ['230',[1,2,2,3,3,3],[1;2;2;3;3;3]]
2$X> % elementwise maximum of 2 inputs, with broadcast. % Stack: % ['230', % [1, 2, 2, 3, 3, 3; % 2, 2, 2, 3, 3, 3; % 2, 2, 2, 3, 3, 3; % 3, 3, 3, 3, 3, 3; % 3, 3, 3, 3, 3, 3; % 3, 3, 3, 3, 3, 3]] ) % index into G % implicit end, display stack contents # Add++, 35 bytes L,bLRdBcB]£X¦Ω+d‽b>1€Ω_A€Ω:AbLR¦+$T

Try it online!

## How it works

We take input as a list of digits, while prevents us from a) having to cast to digits with BD, and also from having to save the digits, which would take two bytes.

First, we generate a range from [1 ... len(input)] with bLR, then we repeat each element $n$ in the range $n$ times. As automatic vectorisation doesn't exist in Add++, we zip it with itself, dBcB], to create a list of pairs $[[1, 1], [2, 2] ... [n, n]]$. We then apply starmap, coupled with repetition over the pairs: £X before concatenating them into one flat array (¦Ω+).

Next, we duplicate this array and table it by maximum, d‽b>. I.e. each element in the array is paired with each other element from the second array and the dyadic maximum command is run over the pair. For an example input of [6 5], this creates the array [1 2 2 2 2 2 2 2 2], which is a flattened version of the mosaic, as the indexes for the array. Unfortunately, Add++ uses 0-indexed arrays, so we need to decrement each element: 1€Ω_.

Then, we index into the input list, by pushing the input again (A), which again saves bytes by taking input as a list. Index into the list with €Ω: before chopping the array into the appropriately lengthed pieces. If the number of digits in the input is denoted by $x$, then the piece size is

$$\frac{x(x - 1)}{2}$$

or the $x^{th}$ triangular number. We generate that by pushing the input's length, calculating the range from 1 to that value, then taking the sum with AbLR¦+. Now, the stack, for an input of [6 5], looks like [[6 5 5 5 5 5 5 5 5] 3]. T chops the array into pieces of size $n$, but the arguments are currently in the wrong order, so we swap them with $before chopping and returning with T. # Charcoal, 17 bytes Ｆ⮌…ＬθＵＯ⊕⊘×ι⁺³ι§θι Try it online! Explanation: Ｆ⮌…Ｌθ Loop over the indices of the characters in reverse order. ⊕⊘×ι⁺³ι Calculate the size of the square. ＵＯ...§θι Draw the square using the current character. # Canvas, 12 bytes ø╶｛；ｌ└²＋：＊；ｎ Try it here! # Python 2, 76 73 bytes -3 bytes thanks to Lynn. l=0;m=[] for k in input():l+=1;m=[r+l*k for r in m]+l*[l*-~l/2*k] print m Try it online! # Charcoal, 14 bytes Ｅ⭆θ×⊕κι×⊕κι‖Ｏ↗ Try it online! ### How? Ｅ⭆θ×⊕κι×⊕κι‖Ｏ↗ - implicitly print the result of... Ｅ - map: ⭆ - over: string map: θ - over: first input × - using: repeat ι - what: ι (loop value) ⊕κ - by: incremented κ (loop counter) × - using: repeat ι - what: ι (loop value) ⊕κ - by: incremented κ (loop counter) ‖Ｏ - Reflect with overlap: ↗ - direction: up-right ...can this method be golfed? • "...can this method be golfed?" Even Neil's solution is longer, so I don't see any hope here. :P – Erik the Outgolfer Jun 23 '18 at 15:07 • ×⊕κι twice though. – Jonathan Allan Jun 23 '18 at 15:11 • The thing is, it's not easy to assign that to a variable, since the values of ι and κ change at every iteration of the Ｅach loop. – Erik the Outgolfer Jun 23 '18 at 15:36 • It needs to be a function but I don't know if it's even possible. – Jonathan Allan Jun 23 '18 at 15:40 • The question to ask is if it's possible in 3 (or 5, depending on how the function is defined) bytes or less. ;) (The obvious answer is, of course, not.) – Erik the Outgolfer Jun 23 '18 at 16:26 # Stax, 12 bytes ü°√¿«│⌠º₧@\τ Run and debug it Using this algorithm. Explanation: c%R:BXm]i*xit+ Full program, implicit input c% Length of input R 1-based range :B Repeat each element according to the range ("123" -> "122333") X Save to X register m Map: ] Character -> string i* Repeat by iteration index xit Trim first <iteration index> elements from X + Concatenate Implicit output with newline ### Stax, 201918 16 bytes ù↔‼i,ÑΓæ☺=╘‼æ↕4╝ Run and debug it Explanation: c%R:BX%mYx%{y|Mvx@m Full program, implicit input c% Length of input R 1-based range :B Repeat each element according to the range ("123" -> "122333") X Save to X register % Length m Map over 1-based range: Y Save index to Y register x% Push length of X register { m Map over 1-based range: y|M Maximum of both indices v 1-based -> 0-based (decrement) x@ Index into X register Implicit output with newline # Attache, 34 bytes {_[Table[Max,Flat!{_&_}=>1:#_]-1]} Try it online! ## Explanation Works similarly to Galen Ivanov's J answer. {_[Table[Max,Flat!{_&_}=>1:#_]-1]} { } anonymous function: _ is input, array of digits example: _ := [2, 0, 3] 1:#_ the range 1 to Size[_] > e.g.: [1, 2, 3] { }=> over each number N: _&_ map to N repeated N times > e.g.: [[1], [2, 2], [3, 3, 3]] Flat! flatten it > e.g.: [1, 2, 2, 3, 3, 3] Table[Max, ] create a "max" table with it > e.g.: 1 2 2 3 3 3 2 2 2 3 3 3 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 -1 subtract 1 from each > e.g.: 0 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 _[ ] index the original array with this matrix > e.g.: 2 0 0 3 3 3 0 0 0 3 3 3 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 # K (ngn/k), 16 bytes {x@i|\:i:&1+!#x} Try it online! # QBasic 1.1, 127 bytes INPUT S$
FOR X=1TO LEN(S$) K=K+X R$=R$+STRING$(X,MID$(S$,X,1))
NEXT
FOR C=1TO K
?STRING$(C-1,MID$(R$,C,1))RIGHT$(R$,K-C+1) NEXT -4 thanks to DLosc. Uses a modified version of xnor's Python 2 algorithm. Input is an unquoted string. Output is \n-separated without extra spaces or \ns. # QBasic, 111 bytes An anonymous function that prompts for input and outputs to the console. INPUT s$
n=LEN(s$) FOR y=1TO n FOR l=1TO y ? FOR x=1TO n z=x IF y>x THEN z=y ?STRING$(x,MID$(s$,z));
NEXT x,l,y
• Looks good--but don't you mean "full program"? I don't think QBasic has "anonymous functions." – DLosc Jul 5 '18 at 18:50

# C (gcc), 130 126 bytes

-4 bytes thanks to ceilingcat

Who needs fancy maths when you can bruteforce?

n,l;R(n,c){for(;n--;)putchar(c);}f(s){for(char*p=s,*q;*p++;)for(n=l=p-s;l--;R(1,10))for(R(n*-~n/2,p[-1]),q=p;*q;)R(++q-s,*q);}

Try it online!

# Php 7.1, 163 bytes

Via CLI providing the number as an argument:

<?foreach(str_split($argv[1])as$k=>$d)$a[]=array_fill(0,$s+=$k+1,array_fill(0,$s,$d));foreach(array_replace_recursive(...array_reverse($a))as$v)echo join($v)."\n"; Not so golfed:$n = 123;

foreach(str_split($n) as$k => $d) {$s += $k + 1;$a[] = array_fill(0, $s, array_fill(0,$s, $d)); } foreach(array_replace_recursive(...array_reverse($a)) as $v) echo implode('',$v) . "\n";

Output:

122333
222333
222333
333333
333333
333333

Method:

Basically build multi-dimensional array squares consisting of the digit, and then superimpose all of them (array_replace_recursive).

(Yes, I know this is embarrassingly long.)

• If the input is a pre-defined array of digits, and the echo implode/join is removed/replaced with an assignment to a list of list of digits, this can be reduced to about 119 bytes, yep still long. – Progrock Jun 24 '18 at 12:09

# Ruby, 80 bytes

->n{s=(1..n.size).map{|i|n[i-1]*i}*"";(0...s.size).map{|i|s[i]*i+s[i..-1]}*"\n"}

Try it online!

# Japt, 12 bytes

Takes input as a string, outputs an array of strings.

Ë+pE
¬£h°YçX

Try it

## Explanation

:Implicit input of string U
Ë           :Map each character D at 0-based index E
pE        :  Repeat D E times
+          :  Append to D
\n          :Reassign to U
¬           :Split to character array
£          :Map each element X at 0-based index Y
°Y       :  Increment Y
çX     :  Repeat X Y times
h         :  Replace the first Y characters in U with that

# uBASIC, 120 bytes

An anonymous function that takes input foprm STDIN and outputs to STDOUT

0Input"",S$:N=Len(S$):ForY=1ToN:ForL=1ToY:ForX=1ToN:ForC=1ToX:Z=X:IfY>XThenZ=Y
1?Mid$(s$,z,1);:NextC:NextX:?:NextL:NextY

Try it online!

# Visual Basic .NET (VBC), 198 bytes

A Subroutine that takes input from STDIN and outputs to STDOUT.

Couldn't seem to get StrDup to work :/

Module M
Sub Main
Dim c,s,n,l,x,y
n=Len(s)
For y=1To n
For l=1To y
For x=1To n
For c=1To x
Console.Write(Mid(s,IIf(x>y,x,y),1)&IIf(c=n,vbLf,""))
Next c,x,l,y
End Sub
End Module

Try it online!

# Lua, 149 140 bytes

Function which accepts a list of digit strings and prints the result to stdout. This is my first attempt at code golf (and the language choice isn't helping either) so bear with me :)

Try it online!

function(a)F,s=0,""for b=1,#a do s=s..a[b]:rep(b)end;for b=1,#a do io.write((s.."\n"):rep(b))F,z=F+b,a[b+1]or""s=z:rep(F)..s:sub(F+1)end end

Ungolfed:

G = function(p)
F,s = 0,""
for i=1,#p do
s=s..p[i]:rep(i)
end
for i=1, #p do
io.write((s.."\n"):rep(i))
F,z = F+i, p[i+1]or""
s = z:rep(F)..s:sub(F+1)
end
end
-- allows to pass the argument list from stdin
-- example: {"1", "2", "3", "4", "5"}
s/./$&x++$-/ge;eval'eval"say;"x++$i;$x+=$i;s/.{$x}/$F[$i]x$x/e;'x@F Try it online! # Yabasic, 108 bytes An anonymous function that takes input from STDIN and outputs to STDOUT Input""s$
n=len(s$) For y=1To n For r=1To y For x=1To n For c=1To x?Mid$(s\$,max(x,y),1);Next