14
\$\begingroup\$

Your task is to fill the bucket with numbers upto a given input.

Rules

Numbers occupy the leftmost position then rightmost, then leftmost and so on.

After overflow, the numbers start to gather around the bucket in a similar manner. They occupy position diagonally.

The examples should make it clear what the expected output is (Some rules are mentioned in the examples).

For more then 10, use the rightmost digit

Examples:

The bucket: 
|      |  or |      | 
|      |     |      | 
|      |     |      |
|      |     |      |
|------|     |______|

input:1  (You can start from either 0 or 1)
output:
|      |  (There can be whitespace to the left even if there is no overflow
|      |  but the bucket must not be distorted.)
|      |
|1     |
|------|

input:6
output:
|      |
|      |
|      |
|135642|
|------|

input:8
output:
|      |
|      |
|7    8|
|135642|
|------|

input:23
output:
|913 20|
|357864|
|791208|
|135642|
|------|

input:27
output:
  |913420|
  |357864|
  |791208|
  |135642|
75|------|6

input:30
output:
  |913420|
  |357864|
  |791208|
 9|135642|0
75|------|68

input:40
output:
    |913420|
    |357864|
   5|791208|6
 939|135642|040
7175|------|6828

input:54   (Maximum input for start=1)
    3|913420|4
   13|357864|42
  915|791208|620
 7939|135642|0408
57175|------|68286

This is code-golf so shortest code wins.

\$\endgroup\$
  • \$\begingroup\$ What do you exactly mean by "you can start from either 0 or 1"? May the sequence of digits itself be 0-indexed (i.e. |024531| for \$n=6\$) or only the input? \$\endgroup\$ – Arnauld Dec 11 '18 at 18:37
  • \$\begingroup\$ @Arnauld, yes, the sequence may start from 0 \$\endgroup\$ – Vedant Kandoi Dec 11 '18 at 19:33
  • \$\begingroup\$ This is one of the better coding challenges for code golf that I have seen here! \$\endgroup\$ – Michael Karas Dec 12 '18 at 11:01
9
\$\begingroup\$

JavaScript (Node.js),  145  143 bytes

A hard-coded pattern (see here for more maths).

1-indexed.

n=>`    g|EGIJHF|h
   e]|?ACDB@|^f
  c[U|9;=><:|V\\d
 aYSO|357864|PTZb
_WQMK|------|LNRX\``.replace(/[3-h]/g,c=>(x=Buffer(c)[0])<n+51?x%10:' ')

Try it online!

Saved 2 bytes thanks to @tsh

\$\endgroup\$
  • 1
    \$\begingroup\$ /[^\s|-]/ -> /[0-z]/ \$\endgroup\$ – tsh Dec 12 '18 at 2:40
  • \$\begingroup\$ @tsh D'oh! I totally missed this simplification. Thank you! \$\endgroup\$ – Arnauld Dec 12 '18 at 10:15
7
\$\begingroup\$

JavaScript (ES6),  144 ... 139  137 bytes

A mathematical approach (see here for less maths).

0-indexed.

n=>(y=4,g=x=>~y?(X=x>8?17-x:x,k=X<y?g:X<5?24-(z=4+y-X)*~z+y*2:y*6+X*2-18,~X?X^5?k<0?'-':(k+=x>8)<n?k%10:' ':'|':`
`)+g(~X?-~x:!y--):'')()

Try it online!

How?

The output is built character by character, with \$y\$ decreasing from \$4\$ to \$0\$ and \$x\$ increasing from \$0\$ to \$18\$ on each row.

We define:

$$X=\begin{cases}x&\text{if }x\le 8\\17-x&\text{if }x>8\end{cases}$$

By writing the full values rather than just the unit digits, we get the following table:

 x |  0  1  2  3  4  5  6  7  8 |  9 10 11 12 13 14 15 16 17 18
 X |  0  1  2  3  4  5  6  7  8 |  8  7  6  5  4  3  2  1  0 -1
---+----------------------------+-------------------------------
 4 | .. .. .. .. 52 || 18 20 22 | 23 21 19 || 53 .. .. .. .. \n
 3 | .. .. .. 50 42 || 12 14 16 | 17 15 13 || 43 51 .. .. .. \n
 2 | .. .. 48 40 34 || 6  8  10 | 11 9  7  || 35 41 49 .. .. \n
 1 | .. 46 38 32 28 || 0  2  4  | 5  3  1  || 29 33 39 47 .. \n
 0 | 44 36 30 26 24 || -- -- -- | -- -- -- || 25 27 31 37 45 \n

This table is essentially symmetric across the y-axis, except that the values on the left side are even and the values on the right side are their odd counterparts.

We define:

$$k=\begin{cases}24+(4+y-X)(5+y-X)+2y&\text{if }X<5\\6y+2X-18&\text{if }X>5\end{cases}$$

$$k'=\begin{cases}k&\text{if }x\le 8\\k+1&\text{if }x>8\end{cases}$$

And for each cell, we append:

  • a linefeed if \$X=-1\$
  • a pipe if \$X=5\$
  • a hyphen if \$k<0\$
  • a space if \$X<y\$ or \$k'>n\$ (where \$n\$ is the input)
  • \$k' \bmod 10\$ otherwise

Commented

n => (                                // main function taking n
  y = 4,                              // start with y = 4
  g = x =>                            // g = recursive function taking x
    ~y ?                              // if y is not equal to -1:
      ( X = x > 8 ? 17 - x : x,       //   compute X
        k = X < y ?                   //   if X is less than y:
          g                           //     set k to a non-numeric value
        :                             //   else:
          X < 5 ?                     //     if X is less than 5:
            24 - (z = 4 + y - X) * ~z //       apply the 'side numbers' formula
             + y * 2                  //
          :                           //     else:
            y * 6 + X * 2 - 18,       //       apply the 'middle numbers' formula
        ~X ?                          //   if X is not equal to -1:
          X ^ 5 ?                     //     if X is not equal to 5:
            k < 0 ?                   //       if k is less than 0:
              '-'                     //         append a hyphen
            :                         //       else:
              (k += x > 8) < n ?      //         update k to k'; if it's less than n:
                k % 10                //           append the unit digit of k'
              :                       //         else:
                ' '                   //           append a space
          :                           //     else (X = 5):
            '|'                       //       append a pipe
        :                             //   else (X = -1):
          `\n`                        //     append a linefeed
      )                               //
      + g(~X ? -~x : !y--)            //   update x and y, and do a recursive call
    :                                 // else (y = -1):
      ''                              //   stop recursion
)()                                   // initial call to g with x undefined
\$\endgroup\$
3
\$\begingroup\$

Python 2, 170 bytes

I=input()
s="    u|SUWXVT|v\n   sk|MOQRPN|lt\n  qic|GIKLJH|djr\n oga]|ACEFDB|^bhp\nme_[Y|------|Z\`fn"
i=1
exec"s=s.replace(chr(64+i),[`i%10`,' '][i>I]);i+=1;"*55
print s

Try it online!

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

Java 10, 168 bytes

n->"    g|EGIJHF|h\n   e]|?ACDB@|^f\n  c[U|9;=><:|V\\d\n aYSO|357864|PTZb\n_WQMK|------|LNRX`".chars().forEach(c->System.out.print(c<46|c==124?(char)c:c<n+51?c%10:" "))

Port of @Arnauld's JavaScript answer (so also 1-indexed, and outputting - as bottom). If you like this answer, make sure to upvote him as well!

Try it online.

Explanation:

n->                      // Method with integer parameter and no return-type
  "    g|EGIJHF|h\n   e]|?ACDB@|^f\n  c[U|9;=><:|V\\d\n aYSO|357864|PTZb\n_WQMK|------|LNRX`"
                         //  String containing the bucket and magic string
   .chars().forEach(c->  //  Loop over the characters (as integers)
     System.out.print(   //   Print:
       c<46|c==124?      //    If the character is "\n", " ", "-", or "|":
        (char)c          //     Output the character as is
       :c<n+51?          //    Else-if the character value is smaller than the input + 51:
        c%10             //     Output a digit: the character value modulo-9
       :                 //    Else:
        " "))            //     Output a space
\$\endgroup\$
1
\$\begingroup\$

6502 machine code (C64), 130 bytes

00 C0 20 9B B7 A9 C0 65 65 85 FB A2 00 BD 2B C0 F0 1A 10 12 C5 FB 90 04 A9 20
D0 0A 69 70 C9 3A 90 04 E9 0A B0 F8 20 D2 FF E8 D0 E1 60 20 20 20 20 F5 7D D3
D5 D7 D8 D6 D4 7D F6 0D 20 20 20 F3 EB 7D CD CF D1 D2 D0 CE 7D EC F4 0D 20 20
F1 E9 E3 7D C7 C9 CB CC CA C8 7D E4 EA F2 0D 20 EF E7 E1 DD 7D C1 C3 C5 C6 C4
C2 7D DE E2 E8 F0 0D ED E5 DF DB D9 7D 2D 2D 2D 2D 2D 2D 7D DA DC E0 E6 EE 00

This uses a modified version of the "preformatted" approach of some other answers. It contains a full string of the bucket, but the digits are replaced by values starting from 0xC1, while any characters for direct printing are in the range 0x01-0x7f.

The C64 charset doesn't include a pipe (|) character, it's therefore replaced with the similar-looking PETSCII character 0x7d.

Online demo

Usage: SYS49152,[n] (1-indexed, e.g. SYS49152,54 for the full output)

Commented disassembly:

         00 C0       .WORD $C000        ; load address
.C:c000  20 9B B7    JSR $B79B          ; get unsigned byte from commandline
.C:c003  A9 C0       LDA #$C0           ; add #$C0 (+ carry = #$C1) ...
.C:c005  65 65       ADC $65            ; ... to parameter
.C:c007  85 FB       STA $FB            ; and store in $FB
.C:c009  A2 00       LDX #$00           ; loop index
.C:c00b   .loop:
.C:c00b  BD 2B C0    LDA .bucket,X      ; loop over encoded string
.C:c00e  F0 1A       BEQ .done          ; null-terminator -> done
.C:c010  10 12       BPL .out           ; positive (bit 7 clear) -> output
.C:c012  C5 FB       CMP $FB            ; compare with parameter+#$C1
.C:c014  90 04       BCC .digit         ; smaller -> convert to digit
.C:c016  A9 20       LDA #$20           ; otherwise load space character
.C:c018  D0 0A       BNE .out           ; and output
.C:c01a   .digit:
.C:c01a  69 70       ADC #$70           ; add offset to '0' (#$30)
.C:c01c   .check:
.C:c01c  C9 3A       CMP #$3A           ; greater than '9' (#$39) ?
.C:c01e  90 04       BCC .out           ; no -> to output
.C:c020  E9 0A       SBC #$0A           ; otherwise subtract 10 (#$a)
.C:c022  B0 F8       BCS .check         ; and check again
.C:c024   .out:
.C:c024  20 D2 FF    JSR $FFD2          ; output character
.C:c027  E8          INX                ; next index
.C:c028  D0 E1       BNE .loop          ; and repeat loop
.C:c02a   .done:
.C:c02a  60          RTS                ; exit ....
.C:c02b   .bucket:
.C:c02b  20 20 20    [...]              ; "encoded" string for bucket
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 64 bytes

Nθ³↑⁵‖M←F²«‖J⁻³ι±¹F⊘⁺θ¬ι«↖I﹪⁺⊗κ⊕ιχM§”)⊟E≡≦⌈▷⊖ü∕”꧔)⊟&hXτtD(λM”κ

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

Nθ

Input the number.

³↑⁵‖M←

Draw half of the bucket and then mirror it to complete the bucket.

F²«

Loop for each side of the bucket.

‖J⁻³ι±¹

Reflect the bucket so that we can draw in a consistent direction on both loops, and jump to the position of the first digit on that side of the bucket.

F⊘⁺θ¬ι«

Loop over the number of digits on that side of the bucket.

↖I﹪⁺⊗κ⊕ιχ

Print the next digit and move the cursor up and left.

M§”)⊟E≡≦⌈▷⊖ü∕”꧔)⊟&hXτtD(λM”κ

Adjust the cursor position by reading the offsets from two compressed strings, 003003003005203004000500 (horizontal offsets) and 11011011011510200300040000 (vertical offsets). These offsets take the above cursor movement into account which conveniently means that they never have to be negative.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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