19
\$\begingroup\$

What I want:

Quite simply, I want a text based display, that asks for an input, n, then shows that value on the display! But there's a catch. Each of the 'true' 'pixels' (the ones filled in) has to be represented by that number n..

Example :

You are given an input n. You can assume n will be a single digit

Input: 0
Output:
000
0 0
0 0
0 0
000

Input: 1
Output:
  1
  1 
  1
  1
  1

Input: 2
Output:
222
  2
222
2
222

Input: 3
Output:
333
  3
333
  3
333

Input: 4
Output:
4 4
4 4
444
  4
  4

Input: 5
Output:
555
5  
555
  5
555

Input: 6
Output:
666
6    
666
6 6
666

Input: 7
Output:
777
  7
  7
  7
  7

Input: 8
Output:
888
8 8
888
8 8
888

Input: 9
Output:
999
9 9
999
  9
999

Challenge:

Do the above it in as few bytes as possible.

I will only accept answers that meet all requirements.

Surrounding whitespace is optional, as long as the digit is displayed properly.

Also, <75 bytes is a vote from me, the lowest accept, but I could always change the accepted answer, so don't be discouraged to answer.

\$\endgroup\$
4
  • \$\begingroup\$ Related \$\endgroup\$
    – Emigna
    Apr 26, 2017 at 16:02
  • \$\begingroup\$ Surely, this is a duplicate. It's definitely very close to this \$\endgroup\$
    – Shaggy
    Apr 26, 2017 at 16:23
  • \$\begingroup\$ Possible duplicate of Golf me an ASCII Alphabet \$\endgroup\$
    – sporkl
    Apr 26, 2017 at 16:24
  • 2
    \$\begingroup\$ I don't think its a dup. While the questions are very similar, I think the reduced set of characters (0-9) will make for some quite different answers. \$\endgroup\$ Apr 26, 2017 at 16:45

24 Answers 24

14
\$\begingroup\$

JavaScript (ES6), 88 bytes

f=
n=>`019
2 3
459
6 7
889`.replace(/\d/g,c=>[2,18,142,96,130,131,698,4,146][c]>>n&1?` `:n)
<input type=number min=0 max=9 oninput=o.textContent=f(this.value)><pre id=o>

The numbers encode which squares contain spaces for a given digit e.g. the bottom left corner has a value of 146 because the 1, 4 and 7 don't use it and 146 = 2¹ + 2⁴ + 2⁷.

\$\endgroup\$
11
\$\begingroup\$

05AB1E, 40 39 38 bytes

•Y¤ŸèK'¦ú’ò™^N•4B5ô¹èvð¹«5714yè8+b¦Sè,

Try it online!

Explanation

•Y¤ŸèK'¦ú’ò™^N•            # the compressed string "318975565561233953387608032537"

4B                         # convert to base-4
  5ô                       # split into strings of size 5
    ¹è                     # get the string at index <input>
      v                    # for each digit y in string
          5714yè           # get the digit in 5714 at index y
                8+         # add 8
                  b        # convert to binary
                   ¦       # remove the leading 1
       𹫠         Sè     # with each digit in the binary number, 
                           # index into the string " <input>"   
                      ,    # print with newline
\$\endgroup\$
11
\$\begingroup\$

Japt, 43 bytes

"Ýûÿ©ÿßY÷ß"®c s4äëAU ¬£2839¤ë4X÷d0S1U

Contains some unprintables. Try it online!

Tallies: 13 bytes of compressed data, 9 bytes to decompress it, and 21 bytes to form the output.

Explanation

Ungolfed code:

"Ýûÿ©ÿßY÷ß"®   c s4à ¤  ëAU ¬ £  2839¤  ë4Xà ·  d0S1U
"Ýûÿ©ÿßY÷ß"mZ{Zc s4} s2 ëAU q mX{2839s2 ë4X} qR d0S1U

There are exactly 4 different row possibilities: (# represents a digit)

#  
  #
# #
###

Thus, each number can be stored as a set of five base-4 digits. Since each number can be then stored in 10 bits, the total is 100 bits, which corresponds to 13 bytes. I'll skip the compression process and instead explain the decompression.

mZ{Zc s4} 

mZ{     }  // Replace each character Z in the compressed string with the following:
   Zc      //   Take the char-code of Z.
      s4   //   Convert to a base-4 string.

After decompression, the 13-byte compressed string looks like this:

3131332333332111200122213333313321011121213133133133

Note that this would fail if any of the 4-digit runs started with 0, as the leading zeroes would be left off when s4 is run. We can fix this by having 0 represent #  , which only appears three times, and none of those fall at the start of a 4-digit run.

s2         // Slice off the first two chars of the result.

Okay, so in order to get our 50-digit string to compress nicely in chunks of 4, we had to add two extra digits. Adding them to the beginning of the string means we can chop them off with the one-byter ¤.

ëAU        // Take every 10th (A) char in this string, starting at index <input> (U).

Embarrassingly, Japt lacks a built-in for splitting a string into slices of length X. It does have a built-in for getting every Xth char, however, so we can store all the data by encoding all of the top rows first, then all of the second rows, etc.

So now we have the 5-digit string encoding the digit we want to create, e.g. 32223 for 0.

q mX{2839s2 ë4X} qR
q                    // Split the resulting string into chars.
  mX{          }     // Replace each char X with the result of this function:
     2839s2          //   Convert the magic number 2839 to a binary string.
            ë4X      //   Take every 4th char of this string, starting at index X.
                 qR  // Join the result with newlines.

To explain the magic number, refer back to the four distinct rows. If you replace # with 1 and   with 0, you get

100
001
101
111

Transposing this and then joining into a single string gives us 101100010111. Convert to decimal and, voilà, you have 2839. Reversing the process maps the digits 0123 into the four binary rows shown above.

Almost done! Now all that's left to do is add in the spaces and digits:

d0S1U      // In the resulting string, replace 0 with " " (S) and 1 with <input> (U).

And presto, implicit output takes care of the rest. I'm sorry this explanation is so long, but I don't see any real way to golf it without making it less understandable (if it is understandable...)

\$\endgroup\$
9
\$\begingroup\$

JavaScript (ES6), 115 111 bytes

Takes input as a string.

n=>'02468'.replace(/./g,c=>(n+n+n+`   ${n} `+n).substr([126,341,36,68,327.5,66,98,340,102,70][n]*4>>c&6,3)+`
`)

How it works

Pattern encoding

The four distinct patterns "XXX", "X..", "..X" and "X.X" can be compressed as "XXX...X.X" and extracted this way:

XXX...X.X
^^^        --> XXX  (id = 0 / pointer = 0)
  ^^^      --> X..  (id = 1 / pointer = 2)
    ^^^    --> ..X  (id = 2 / pointer = 4)
      ^^^  --> X.X  (id = 3 / pointer = 6)

By substituting the input digit n for "X" and using actual spaces, this gives the expression:

n+n+n+`   ${n} `+n

Digit encoding

Using the pattern identifiers defined above, each digit can be represented by a 5 * 2 = 10-bit quantity.

For instance:

XXX  -->  0 *   1 =   0
X.X  -->  3 *   4 =  12
XXX  -->  0 *  16 =   0
..X  -->  2 *  64 = 128
XXX  -->  0 * 256 =   0
                    ---
                    140

The complete list is:

[252, 682, 72, 136, 655, 132, 196, 680, 204, 140]

However, dividing these values by 2 allows to save two bytes. So instead we store:

[126, 341, 36, 68, 327.5, 66, 98, 340, 102, 70]

Demo

let f =

n=>'02468'.replace(/./g,c=>(n+n+n+`   ${n} `+n).substr([126,341,36,68,327.5,66,98,340,102,70][n]*4>>c&6,3)+`
`)

console.log(f('0'))
console.log(f('1'))
console.log(f('2'))
console.log(f('3'))
console.log(f('4'))
console.log(f('5'))
console.log(f('6'))
console.log(f('7'))
console.log(f('8'))
console.log(f('9'))

\$\endgroup\$
8
\$\begingroup\$

SOGL V0.12, 30 bytes

■'τč▲Β►║⁰ΡāQšJ┘tXdnοO¹‘'¹n.w3n

Try it Here! The compressed string ■'τč▲Β►║⁰ΡāQšJ┘tXdnοO¹‘ is

  ŗ  ŗ  ŗ  ŗ  ŗŗŗŗ  ŗŗŗŗŗ  ŗŗŗŗŗŗ  ŗŗŗŗ  ŗŗŗŗŗ ŗŗ ŗŗŗŗ  ŗ  ŗŗŗŗŗ  ŗŗŗ  ŗŗŗŗŗŗŗŗ  ŗŗŗŗ ŗŗŗŗŗŗŗ  ŗ  ŗ  ŗ  ŗŗŗŗŗ ŗŗŗŗŗ ŗŗŗŗŗŗŗŗ ŗŗŗŗ  ŗŗŗŗŗŗŗŗ ŗŗ ŗŗ ŗŗŗŗ

which is (starting with 1, ending with 0) the numbers, line by line, number to number. The rest

...‘             push the compressed string, replacing the ŗs with pop - the input
    '¹n          split into chunks of 15
       .w        push the (1-indexed, wrapping) input-th item of that array
             3n  split into chunks of 3
                 implicitly output the array, line by line
\$\endgroup\$
7
  • \$\begingroup\$ I am impressed about how you managed to do it in 35 bytes in one go! Although I have never used SOGL before, I used an online tester and it works! \$\endgroup\$
    – Xetrov
    Apr 27, 2017 at 15:30
  • \$\begingroup\$ The number shape data is really only 17 bytes (the 6 bytes lost there are length data and that it's ` ` and -), and the rest is easy. I'm curious, what online tester did you use? I haven't made nor seen any. \$\endgroup\$
    – dzaima
    Apr 27, 2017 at 15:40
  • \$\begingroup\$ Oh, just click on the SOGL above! You have to download something like 1MB or something. I deleted it instantly. :/ \$\endgroup\$
    – Xetrov
    Apr 27, 2017 at 15:45
  • \$\begingroup\$ How did you do it? \$\endgroup\$
    – Xetrov
    Apr 27, 2017 at 15:48
  • \$\begingroup\$ Oh, wow! Congrats to you! You seem to have a lot of files in it. Maybe seperate it a little bit \$\endgroup\$
    – Xetrov
    Apr 27, 2017 at 15:51
6
\$\begingroup\$

Bash + GNU utils, 114

  • 2 bytes saved thanks to @SophiaLechner

There are probably some more opportunities for compression here, but here's a start:

dc<<<4o16iEAC00CDF33AC373DEF00EEFB3p|fold -5|sed "$[$1+1]!d
s/0/  x\n/g
s/1/x\n/g
s/2/x x\n/g
s/3/xxx\n/g
y/x/$1/"

Explanation

Each row of of every digit is one of these four patterns:

  x
x
x x
xxx

By labeling these 0-3, each digit can be represented by 5 base-4 digits. E.g. 0 would be 32223, and the full list of digits is encoded in base-4 as 32223000003031330303223003130331323300003232332303. This encoded in hex as EAC00CDF33AC373DEF00EEFB3.

  • dc converts hex EAC00CDF33AC373DEF00EEFB3 to base-4.
  • fold puts 5 base-4 digits on each line
  • sed:
    • deletes all input lines apart from line number n+1 (sed 1-indexes lines)
    • translates the base-4 digits to the pattern (of xes) for each line of the given digit, along with a newline for the end of each row
    • transliterates the xes to digit n.

Try it online.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Good idea! Save a couple bytes by replacing s/1/x \n/g with s/1/x\n/g, since those spaces won't visually affect the output. \$\endgroup\$ May 15, 2018 at 19:07
5
\$\begingroup\$

MATL, 45 43 bytes

'V~4;LR`I-D2q (z%eI)P-nc'F TFZa15eiZ)5eGV*c

Try it online!

Explanation

'V~4;LR`I-D2q (z%eI)P-nc' % Compressed string using printable ASCII except '
F TFZa                    % Decompress (change of base) into alphabet [1 0]
15e                       % Reshape into 15-row matrix. Each column contains the
                          % art for one digit, packed in column-major order
iZ)                       % Take input number and pick the corresponding column.
                          % Indexing is 1-based and modular, so 0 is at the end
5e                        % Reshape into a 5-row matrix. This already looks like
                          % the number, with active pixels as 1 and inactive as 0
GV                        % Push input number again. Convert to string, of one char
*                         % Multiply, element-wise. The previous char is
                          % interpreted as its ASCII code. This changes 1 into the 
                          % ASCII code of the input digit, and leaves 0 as is
c                         % Convert to char. Implicitly display. Char 0 is shown as
                          % a space
\$\endgroup\$
3
\$\begingroup\$

Retina, 166 164 163 bytes

Two spaces on the third line from the bottom

0
0addda
d
0 0
1
1b1b1b1b1b1
2
2ab2a2b222
3
3ab3ab3a
4
44 44 4ab4b4
5
55ab555b5a
6
66ab6a 6a
7
7ab7b7b7b7
8
88a 8a8 8a
9
99a 9ab9a
(\d)a
$1$1$1$1
^.

b
  
.{3} $+¶

Try it Online!

An improved version of @Okx's solution

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

Pyth 82 85 91 100 bytes

Km?dC+48Q\ m.&C@"w$kn<^_d\x7f~"Qdj1906486293414135453684485070928 128V5p.)Kp.)K.)K

A lot of golfing probably possible, my first challenge.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Apr 28, 2017 at 12:00
2
\$\begingroup\$

Batch File, 8 + 184 bytes

Here's my obligatory batch solution. Unfortunately the standard method of accomplishing this is over 300 bytes in batch, so we resort to much cheaper tactics.

@type %1

In the current directory, I have 10 files set up, named from 0 to 9. In each of these is the respective 5x3 grid of pixels. For example:

enter image description here

The byte counts for these are:

19 0
19 1
17 2
19 3
19 4
17 5
17 6
19 7
19 8
19 9
Total - 184

Still beat Java.

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

Ruby, 94 bytes

->n{20.times{|i|print i%4>2?$/:[" ",n]["_Q_@@_]UWUU_GD_WU]_U]AA__U_WU_"[n*3+i%4].ord>>i/4&1]}}

Ungolfed in test program

f=->n{
  20.times{|i|                           #4 characters * 5 rows = 20
    print i%4>2?$/:                      #If i%4=3 print a newline else
    [" ",n][                             #either a space if the expression below is 0 or the digit n if 1.
      "_Q_@@_]UWUU_GD_WU]_U]AA__U_WU_"[  #bitmap data for digits, one character for each column
        n*3+i%4].ord>>i/4&1              #Select the correct character for the column, convert to ascii code,
    ]                                    #rightshift to find the correct bit for the row, 
  }                                      #use & to isolate the bit: 0 or 1
}

f[gets.to_i]
\$\endgroup\$
1
\$\begingroup\$

PHP, 115 Bytes

for(;$i<5;)echo strtr(sprintf("%03b\n","1754"[[425,0,465,273,26,285,413,1,409,281][$argn]/4**$i++%4]),[" ",$argn]);

Try it online!

Expanded

for(;$i<5;)
  echo strtr(
    sprintf("%03b\n",  # make a binary
      "1754"[[425,0,465,273,26,285,413,1,409,281][$argn]/4**$i++%4])
    ,[" ",$argn]); # replace 0 with space and 1 with the input

Encoding Try it online!

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

Retina, 125 bytes

.
A$&¶B$&¶C$&¶D$&¶E$&
([ACE][235689]|A0|E0|C4|A7)(?<=(.))
$2$2$2
(B[0489]|D[068]|A4|C0)(?<=(.))
$2 $2
D2
2
B5
5
B6
6
[A-E]
  

Try it online! (Delete test suite in header to test individual input digits.) Last line contains two spaces.

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

PowerShell, 126 120 113 109 101

$n="$args"
-split'ϽϭϿ·ͱ Ο·ͽͼϿ·Ņ ϻ·ͭͭϿ'|%{-join($_[0..2]|%{"$n·"[!($_-band1-shl$n)]})}

Unicode is fun. Characters are also way shorter than numbers. Above code contains U+0000 twice, thus cannot be copied directly (works fine in a file, though). The following code can be copied:

$n="$args"
-split"ϽϭϿ ͱ`0Ο ͽͼϿ Ņ`0ϻ ͭͭϿ"|%{-join($_[0..2]|%{"$n "[!($_-band1-shl$n)]})}

We lose three bytes because we need the UTF-8 signature at the start. Otherwise the string won't work.

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

Retina, 190 bytes

0
000¶aaa000
a
0 0¶
1
aaaaa
a
b1¶
2
ab2¶a2¶a
a
222¶
3
ab3¶ab3¶a
a
333¶
4
4 4¶4 4¶444¶b4¶b4
5
a5b¶ab5¶a
a
555¶
6
a6¶a6 6¶a
a
666¶
7
777aaaa
a
¶b7
8
a8 8¶a8 8¶a
a
888¶
9
a9 9¶ab9¶a
a
999¶
b
  

There are two spaces on the last line but SE doesn't want to render it :/ Fixed!

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I have a feeling there's a way to combine the repeated replacement of a with three digits and a newline. (Maybe using lookaheads?) \$\endgroup\$ Apr 26, 2017 at 17:24
  • \$\begingroup\$ @mathjunkie Yeah :/ \$\endgroup\$
    – Okx
    Apr 26, 2017 at 17:26
1
\$\begingroup\$

Java 8, 278 214 210 204 bytes

n->("A"+n+"\nB"+n+"\nC"+n+"\nD"+n+"\nE"+n).replaceAll("([ACE][235689]|A0|E0|C4|A7)(?<=(.))","$2$2$2").replaceAll("(B[0489]|D[068]|A4|C0)(?<=(.))","$2 $2").replaceAll("D2|B5|B6",n).replaceAll("[A-E]","  ")

Port of @Neil's Retina answer. Takes the input as a String.

Try it here.

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

Python 2, 174 125 bytes

lambda n:'\n'.join('xxx  x x'[int(i):][:3]for i in oct(0x3028000adba93b6b0000c0ad36ebac30180c0)[:-1][n::10]).replace('x',`n`)

Try it online!

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

PowerShell,159 135 128 118 bytes

param($n);0..4|%{("$n$n$n","$n $n","  $n","$n  ")['01110333330203002020110220302003010022220101001020'[$_+($n*5)]-48]}

Current answer: removed extraneous variable naming

Try it online!

I'll have to see if I can get some tricks from the other answers :P

EDIT Getting smarter with calling the mega string

EDIT2 Switched to using a string of numbers to index into $a to save 7 bytes. Although I liked my previous dynamic variable name calls in the below code (135 bytes)

param($n);$a="$n$n$n";$b="$n $n";$c="  $n";$d="$n  ";$r="abbbadddddacadaacacabbaccadacaadabaaccccababaabaca";0..4|%{gv $r[$_+($n*5)]-v}
\$\endgroup\$
0
\$\begingroup\$

Jelly, 35 32 bytes

Ṿ⁶,“SÐ~ẈoI9Ẇ¦Y./,0&Ƥɓ€ọ’ṃs15ị@s3

Try it online! or see them all.

Same as dzaima's method.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -Mbigint -pa, 101 bytes

$_=sprintf"%16b",0xf79ef7dee492f3def39eb792e79ee7ce4924f6de>>16*$_&65535;eval"y/01/ @F/";s/...\K/\n/g

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 119 bytes

lambda n:(3*f"%s%s{n}\n%s %s\n")[:30]%(*[n if i//(2**n)%2else" "for i in[1021,1005,881,927,893,892,325,1019,877,877]],)

Try it online!

Each 'pixel' is represented as an integer where each power of two indicates whether the pixel is solid. This takes advantage of the fact that the three right-hand corners are always the digit.

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 61 38 33 bytes

E§⪪”{⊞¶↙⬤\→(B⊘K→υ^⎇\`|;ε”⁵N⮌⍘Iι⁺ θ

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

   ”...”            Compressed data string
  ⪪     ⁵           Split into groups of 5 digits
 §                  Indexed by
         N          First input as an integer
E                   Map over digits
             ι      Current digit
            I       Cast to integer
           ⍘        Converted to custom base
              ⁺ θ   Space concatenated with input digit
          ⮌         Reversed
                    Implicitly print

A generic port is a bit boring, so here's a more idiomatic 61-byte answer:

GH✳✳§⟦⟦↑L↓¬⟧⟦↓↓⟧⟦T↓→⟧⟦T→¬⟧⟦↑↑¬↑⟧⟦←↓T⟧⟦T↑L⟧⟦→↓↓⟧⟦+L↓⟧⟦+↓T⟧⟧N³θ

Try it online! No verbose version as the deverbosifier doesn't understand lists of directions or even multidirectionals inside ordinary polygons. Explanation:

     ⟦...⟧      Array of paths, see below
          N     Input as an integer
    §           Index into the array
  ✳✳            Treat the element as direction data
           ³    Length of each segment (including 1 character overlap)
            θ   Draw using the input character
GH              Draw a path

Each path is specified using a list of directions (←↑→↓). Four usable shortcuts save bytes: L represents ↑→, ¬ represents ↓←, T represents →↓← while + represents →↓←↑, although at least one arrow must remain for the list to be recognised as a direction list (so for instance TT cannot be used for 3).

60 bytes using the newer version of Charcoal available on ATO:

GH✳✳§⟦⟦⌈¬↑↑⟧⟦↓↓⟧⟦T↓→⟧⟦T→¬⟧⟦↑↑¬↑⟧⟦←↓T⟧⟦+↑→⟧⟦⌈↓⟧⟦+↑⌈⟧⟦+↓T⟧⟧N³θ

Attempt This Online! Link is to verbose version of code. Explanation: The order in which multidirectionals execute changed which means that 7 can now be drawn using a multidirectional and an arrow instead of three arrows. Some of the other digits have had to have their multidirectionals tweaked but this was possible without changing the length of the code.

\$\endgroup\$
0
\$\begingroup\$

Befunge, 192 174 bytes

3040040403333004010030103304403030010302222204440&:09p5*>:  #v_v
   9g86*+:00p:10p:20p:01p:12p:23p:04p24p 6v             ^-1$\< >
   ,+55,g\2,g\1:,g\0:\_@#:-1p31p30:p41:*84<$

Try it online!

First stab was ~360 bytes doing if/else & pushing characters to stack. Which was devoting 150 characters to encoding numbers. So instead encode characters as 5 numbers based on each line. Then pop 5x input to have top 5 stack elements be desired character. 2nd line sets up line pieces with correct characters in top left 3x5 of memory

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

JavaScript (ES8), 87 bytes

n=>`019
2 3
459
6 7
889`.replace(/\d/g,c=>[2,18,142,96,130,131,698,4,146][c]>>n&1?' ':n)
\$\endgroup\$
1
  • 2
    \$\begingroup\$ This is exactly the same answer as @Neil's JavaScript answer of April 27th.. If you have something he can improve, you should comment to his answer instead of posting a new one that is the same (EDIT: And yes, I realize you don't have enough reputation to comment yet..) \$\endgroup\$ Oct 12, 2017 at 9:32

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.