Challenge:
Input: Three integers: bottom-edge length; starting amount of grains of sand; index
Output: Output the state of the bottom part of an hourglass at the given index, based on the given bottom-edge length and amount of grains of sand.
Challenge rules:
- We simulate the grains of sand with digits 1-9
- We put the currently remaining amount of grains of sand at the top in the middle, following by a single character of your own choice (excluding digits, whitespaces and new-lines; i.e.
-
) on the line below it - When the hourglass is being filled, we fill it per row, one digit at a time
- When the sand can go either left or right, we ALWAYS go right (the same applies to balancing the remaining amount of grains of sand above the hour glass)
- When we've reached 9 it's filled, and we can't fit any more sand at that specific place in the hourglass
- The amount of grains of sand left are also always rightly aligned
- Once the bottom halve of the hourglass is completely filled, or the amounts of grains of sand left reaches 0, we can't go any further, and this would be the output for all indexes beyond this point
- Both 0-indexed or 1-indexed is allowed, and please specify what you've used in your answer.
- Trailing and leading spaces and a single trailing or leading new-line are optional
- You are allowed to use any other character instead of a zero to fill up empty spaces of the bottom part of the hourglass (excluding digits, new-lines, or the character you've used as neck), if you choose to display them.
- The bottom-edge length will always be odd
- The bottom-edge length will be
>= 3
; and the amounts of grains of sand>= 0
- If you want you are also allowed to print all states up to and including the given index
- You can assume the (0-indexed) index will never be larger than the total amount of grains of sand (so when there are 100 grains of sand, index 100 is the maximum valid index-input).
- The first index (0 for 0-indexed; 1 for 1-indexed) will output an empty hourglass with the amount of grains of sand above it.
Example: Pictures (or ascii-art) say more than a thousand words, so here is an example:
Input bottom-edge length: 5
Input amount of grains of sand: 100
Instead of a current index, I display all steps here:
Output for all possible indexes with bottom-edge length 5
and amount of grains of sand 100
:
100
-
0
000
00000
99
-
0
000
00100
98
-
0
000
00110
97
-
0
000
01110
96
-
0
000
01111
95
-
0
000
11111
94
-
0
000
11211
93
-
0
000
11221
92
-
0
000
12221
91
-
0
000
12222
90
-
0
000
22222
89
-
0
000
22322
88
-
0
000
22332
87
-
0
000
23332
86
-
0
000
23333
85
-
0
000
33333
84
-
0
000
33433
83
-
0
000
33443
82
-
0
000
34443
81
-
0
000
34444
80
-
0
000
44444
79
-
0
000
44544
78
-
0
000
44554
77
-
0
000
45554
76
-
0
000
45555
75
-
0
000
55555
74
-
0
000
55655
73
-
0
000
55665
72
-
0
000
56665
71
-
0
000
56666
70
-
0
000
66666
69
-
0
000
66766
68
-
0
000
66776
67
-
0
000
67776
66
-
0
000
67777
65
-
0
000
77777
64
-
0
000
77877
63
-
0
000
77887
62
-
0
000
78887
61
-
0
000
78888
60
-
0
000
88888
59
-
0
000
88988
58
-
0
000
88998
57
-
0
000
89998
56
-
0
000
89999
55
-
0
000
99999
54
-
0
010
99999
53
-
0
011
99999
52
-
0
111
99999
51
-
0
121
99999
50
-
0
122
99999
49
0
222
99999
48
-
0
232
99999
47
-
0
233
99999
46
-
0
333
99999
45
-
0
343
99999
44
-
0
344
99999
43
-
0
444
99999
42
-
0
454
99999
41
-
0
455
99999
40
-
0
555
99999
39
-
0
565
99999
38
-
0
566
99999
37
-
0
666
99999
36
-
0
676
99999
35
-
0
677
99999
34
-
0
777
99999
33
-
0
787
99999
32
-
0
788
99999
31
-
0
888
99999
30
-
0
898
99999
29
-
0
899
99999
28
-
0
999
99999
27
-
1
999
99999
26
-
2
999
99999
25
-
3
999
99999
24
-
4
999
99999
23
-
5
999
99999
22
-
6
999
99999
21
-
7
999
99999
20
-
8
999
99999
19
-
9
999
99999
So as example:
inputs: 5,100,1
output:
99
-
0
000
00100
Same example with another valid output format:
99
~
.
...
..1..
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
INPUTS: bottom-edge length, amount of grains of sand, index (0-indexed)
inputs: 5,100,1
output:
99
-
0
000
00100
inputs: 5,100,24
output:
76
-
0
000
45555
inputs: 5,100,100
output:
19
-
9
999
99999
inputs: 5,10,15
output:
0
-
0
000
22222
inputs: 3,30,20
output:
10
-
0
677
inputs: 3,3,0
3
-
0
000
inputs: 9,250,100
150
-
0
000
00000
2333332
999999999
inputs: 9,225,220
5
-
4
999
99999
9999999
999999999
inputs: 13,1234567890,250
1234567640
-
0
000
00000
0000000
344444443
99999999999
9999999999999
inputs: 25,25,25
0
-
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
0000000000000000000
000000000000000000000
00000000000000000000000
1111111111111111111111111
5,100,10000
why does20+9+9+9+9+9+9+9+9+9 = 101
? \$\endgroup\$