# Draw a battery indicator

Given the battery level L in the range 0-5, your code should draw the following ASCII art.

When L=0:

############
#          ###
#          ###
#          ###
############


When L=1:

############
###        ###
###        ###
###        ###
############


When L=2:

############
#####      ###
#####      ###
#####      ###
############


And etc. You can change the character # to any other (except space), it's up to you. Trailing spaces are allowed.

This is a code-golf challenge so the shortest solution wins.

• I presume I can't change the character # to a space. Feb 24 at 18:33
• @pxeger Yes, you can't now :) Feb 24 at 18:37
• you allow trailing whitespace, but what about preceding whitespace? (specifically a preceding newline?)
– Dave
Feb 26 at 3:10
• @Dave I think it would be fine Feb 26 at 7:30

=substitute(query({rept("#",12);sort(if({1;1;1},"#"&rept("#",A1*2)&rept("-",10-A1*2)&"###"));rept("#",12)},,9)," ",char(10))


## 89 bytes if it's okay for the output to be displayed in separate cells

={rept("#",12);sort(if({1;1;1},"#"&rept("#",A1*2)&rept("-",10-A1*2)&"###"));rept("#",12)}


Try it here

• Welcome to Code Golf, and nice first answer! Feb 24 at 20:19
• @pxeger, thanks! :) Feb 24 at 20:33

# brainfuck, 266 245 242 bytes

-21 bytes thanks to @emanresu A's suggestion to use ÿ instead of ! (since ÿ's charcode is the same as the glider reference value 255 on an 8 bit bf interpreter like TIO's).

-3 bytes by removing unnecessary code (at the expense of a pretty memory layout).

-[-[-<]>>+<]>->>+++++[<++>-]->++++[<...>-]<<.>+>-[>+<-----]>---<,>[<->-]<[<+>-]<[>->+>->+>->+<<<<<<-]+++++[>+>>+>>+<<<<<-]->>>>>>>-<+[[[-<+]-.>+[->+]-<[+[-<+]-..>+[->+]-<-]>+<-<[+[-<+]-<<..>>>+[->+]-<-]+[-<+]-...<.>>+[->+]<->]<<+]++++[>...<-]


Try it online!

Uses a glider and fancy loop tricks to print the middle 3 rows. Uses ÿ instead of # since that is allowed by the challenge.

Ungolfed:

memory layout
(pni = 5 minus inp)
3   4   5   6   7   8   9   10  11  12
' ' \n  ÿ   pni inp pni inp pni inp ÿ

' ' = 32
-[-[-<]>>+<]>-
\n = 10
>>+++++[<++>-]
ÿ = 255
-

print top row of ÿs
>++++[<...>-]<<.>+

get input
>-[>+<-----]>---<,>[<->-]<[<+>-]<

copy to loop cells
[>->+>->+>->+<<<<<<-]
+++++[>+>>+>>+<<<<<-]
create glider refs
->>>>>>>-<+

main loop
[
glider collision guard
exit the loop when all loop variables are exhausted
[-
print first ÿ
+[-<+]-.>
+[->+]-
print ÿs in battery
<[
+[-<+]-..>
+[->+]-
<-
]
move glider back one cell
>+<-
print spaces in battery
<[
+[-<+]-<<..>>>
+[->+]-
<-
]
print last 3 ÿs and \n
+[-<+]-...<.>>
+[->+]
move glider back one cell
<->
]<<+
]

print last row of ÿs
++++[>...<-]

• Would it be cheaper to use ÿ instead? Feb 25 at 3:01
• It could be, actually. Let me try doing that and see. Feb 25 at 13:27
• Finally got around to implementing your golf @emanresu A. Thanks! Feb 27 at 20:04

# Python, 46 bytes

lambda n:[s:="#"*12,*3*[f'#{"##"*n:10}###'],s]

Attempt This Online!

Outputs a list of lines, as allowed by standard I/O rules.

$$\ -9 -1 = -10 \$$ thanks to @xnor

## Python, 76 bytes

lambda n,r=f"#{' '*10}#".replace:[a:=r(" ","#")]+[r(" ","#",2*n)+"##"]*3+[a]

Attempt This Online!

Did you know Python's str.replace method has a third parameter to limit the maximum number of replacements that will be performed? You do now!

• 47 bytes using f-strings: lambda n:[s:="#"*12]+[f"#{'##'*n:10}###"]*3+[s]
– xnor
Feb 24 at 19:00
• List unpacking saves a byte: lambda n:[s:="#"*12,*[f'#{"##"*n:10}###']*3,s]
– xnor
Feb 24 at 19:40

# C (POSIX), 83 bytes

#define s"\n############"
#define r"\n%1$-11s###" f(i){printf(s r r r s,s+12-i*2);}  I was originally tweaking ErikF's answer but ended up making more substantial changes, so it felt like a new separate answer. Note that this saves some space by printing a newline before the output, which has been approved. Uses compile-time string concatenation to compress the output template, then uses printf to fill in the correct amount of the bar. ## Breakdown: ### r template pattern \n - print a literal newline % s - followed by a string 1$          - from the first argument (POSIX extension)
-         - padded with trailing spaces
11       - to at least 11 characters
###   - followed by 3 literal hash symbols


### printf call

printf(
s r r r s,     // concatenate define'd strings into full pattern
s + 12 - i * 2 // first argument: fragment of s (last i * 2 + 1 characters)
);


### Expanded

f(i) {
printf(
"\n############\n%1$-11s###\n%1$-11s###\n%1$-11s###\n############", "\n############" + 12 - i * 2 ); }  Try it online As an interesting alternative, this version uses hex values to draw the shape, so it's limited to f characters, but avoids the initial newline. It is the same size (83 bytes): #define r"%2$-11lxfff\n"
f(i){printf("%lx\n"r r r"%1$lx",(1l<<48)-1,(16l<<i*8)-1);}  • Somehow, the "Expanded" version might be less readable than the golfed version. :-) Feb 26 at 12:24 • yes the golfed version is quite nice for its clarity, but I expanded it so that it is obvious what's happening for people who may not be familiar with C's macro expansion / compile-time string concatenation – Dave Feb 26 at 15:36 • Very elegant. I was going to take a stab at this one but I don’t think I can outdo this. Mar 18 at 23:46 # Charcoal, 18 bytes ＵＯ⊕⊗Ｎ³-¹²↘‖Ｏ↓ＵＯ±³-  Try it online! Link is to verbose version of code. Explanation: ＵＯ⊕⊗Ｎ³  Draw the fullness of the top half of the battery. -¹²↘  Draw the line at the top of the battery and leave the cursor at the top right of the 3×3 square end. ‖Ｏ↓  Reflect to complete the fullness and sides of the battery. (This also moves the cursor to the bottom right of the 3×3 square end.) ＵＯ±³-  Draw the 3×3 square end. # Acc!!, 152 bytes N-46 Count i while i-2 { Count l while (l-3)*i { Count c while c-14 { Write 32+1/((c+3)/2%7/_+1) } Write 10 } Count h while h-12 { Write 33 } Write 10 }  Uses exclamation points. Try it online! ### Explanation This was fun to golf. The verbose syntax of Acc!! drove me to combine as many Count while and Write statements as possible, which required some interesting expressions to encapsulate the logic. N-46  Read a character from stdin and store its codepoint minus 46 in the accumulator. If the input number is L, the accumulator now holds the value L+2. Count i while i-2 { ... }  Do the following twice: Count l while (l-3)*i {  Do the following (output the middle part of the battery) three times, but only on the second iteration of the i loop: Count c while c-14 {  Do the following (output a character in the middle part of the battery) fourteen times: Write 32+1/((c+3)/2%7/_+1)  Write either a space or an exclamation point, based on the loop index c and the accumulator value _. Observe that we want to output some number of exclamation points, then some number of spaces, then three more exclamation points. This looks like a slice into a periodic function. Since we want to add exclamation points in pairs, let's aim for something like this: 0 1 01234567890123456789 !!!!..........!!!!..  where ! means "definitely maps to !" and . means "might map to either ! or   depending on the accumulator value." We want to run our loop from 3 to 16 in this function; since loop variables in Acc!! always start at zero, we'll just add 3 to the loop variable c. Next, we combine the pairs of characters that will always be the same by int-dividing by 2: 0123456789 !!.....!!.  We can get the periodic behavior if we take this input mod 7: 0123456 !!.....  Now it's just a matter of adjusting the cutoff depending on the input value. Conceptually, we want exclamation points if this number is less than L+2, and spaces if it is greater than or equal to L+2. Conveniently, L+2 is the value in the accumulator. Inconveniently, Acc!! doesn't have comparison operators, so we'll have to abuse some arithmetic: With integer division, and assuming b is always positive, a/b is 0 if a<b and some positive number otherwise. To turn 0 vs positive into 1 vs 0, we can add 1 (making it 1 vs greater than 1) and then int-divide 1 by that quantity. End result: 1/(a/b+1) is 1 if a<b and 0 otherwise. In our case, we have a = (c+3)/2%7 and b = _ (the accumulator). Substituting those into the above expression and adding to 32 gives exclamation point (ASCII 33) or space (ASCII 32) exactly where we want them. } Write 10 }  Close the c loop, write a newline, and close the l loop. Count h while h-12 { Write 33 } Write 10  Output the top/bottom of the battery: Loop 12 times and write exclamation point; then write a newline. # Pari/GP, 82 bytes n->[print(l)|l<-[s="############",e=Strchr([35-3*(j>n+n&j<11)|j<-[0..13]]),e,e,s]]  Try it online! This code is most probably not the shortest possible, as this is my first time golfing in Pari/GP. Thanks @alephalpha for -14 bytes. • You can use list comprehension instead of a for loop. 87 bytes. Mar 1 at 9:18 • Even shorter using Strchr to convert integers to strings: 82 bytes. Mar 1 at 9:31 • @alephalpha Thanks for the tips! I'm sure I'll be using these in the future. I wanted to ask, is there a way to do string multiplication in Pari/GP? Like "#"*12 in python. Mar 1 at 9:51 • There is no string multiplication. The shortest way I can think of is concat(["#"|i<-[1..12]]), but that is already longer than "############". Mar 1 at 10:04 • -2 bytes by using exclamation marks instead: Try it online! Mar 1 at 18:59 # Vyxal, 18 bytes d×*₀↲×p14e:"×12*p∞  Try it Online! d # Input * 2 ×* # That many asterisks ₀↲ # Pad the left to length 10 with spaces ×p # Prepend an asterisk 14e # Extend to length 14 by appending the first char :" # Pair with itself p # Prepend to this... ×12* # 12 asterisks ∞ # Palindromise the result  # Retina 0.8.2, 55 bytes .+ #$&$*_10$* ###¶
+(_*)_
##$1 .+¶ 12$*#¶$&$&$&12$*#


Try it online! Explanation:

.+
#$&$*_10$* ###¶  Produce a body row of the empty battery but include L _s. +(_*)_ ##$1


For each _, delete two spaces and add two #s.

.+¶
12$*#¶$&$&$&12$*#  Triplicate the row and wrap it in lines of 12 #s. # C (gcc) with -lm, 93 91 bytes • -2 thanks to ceilingcat Uses 9 as the fill character. I make use of 10n-1 producing n nines when n>0. *s="999999999999";f(i,j){for(puts(s),j=3;j--;printf("%-11.f999\n",exp10(i-~i)-1));puts(s);}  Try it online! • you can get down to 90 if you use hex instead of decimal (I expected more but oh well): *s="ffffffffffff";f(i,j){for(puts(s),j=3;j--;printf("%-11lxfff\n",(16l<<i*8)-1));puts(s);} – Dave Feb 26 at 2:06 • or down to 88 if you use strings (and it lets you use any output character): short*s="############";f(i,j){for(puts(s),j=3;j--;printf("#%-10s###\n",s+6-i));puts(s);} – Dave Feb 26 at 2:06 # Python 3.8, 85726849 46 bytes lambda l,a="##":[a*6,*3*[f'#{l*a:10}###'],a*6]  Outputs a list of strings, as allowed by standard I/O rules Try it online! -13 thx to @Aiden chow -4 thx to @ovs -3 thx to @Aiden chow • 72 bytes by converting c into an f-string and some other optimizations. Feb 25 at 7:04 • Additional -4 by using the sep argument of print. – ovs Feb 25 at 7:07 • is this looking too much like pxeger's answer? Feb 25 at 7:26 • @DialFrost yeah it's looking really similar to pxeger's answer, though it's still a little different so i think it's fine. Feb 25 at 7:46 • Also 46 bytes (different from pxeger's answer) Feb 25 at 7:47 # Julia 1.0, 57 51 48 bytes c='#';!n=[c^12,(h=rpad(c*c^2n,11)*c^3;),h,h,c^12]  Try it online! -3 MarcMush • 48 bytes Feb 25 at 19:47 # Excel, 82 74 bytes It Taylor Rained 8 bytes. =LET(a,REPT("#",12),b,MID(a&REPT(" ",10),12-2*A1,11)&"### ",a&" "&b&b&b&a)  Input of L is in the cell A1. Output is wherever the formula is. For best results, make sure the column is wide enough, text wrapping is on, and the font is monospace. The output will be correct regardless but this makes that fact visually apparent. • LET(a,REPT("#",12) defines a to be 12 number signs in a row. Combining LET() and REPT() only saves us 1-3 bytes but savings are savings. • b,MID(a&REPT(" ",10),12-2*A1,11)&"###\n" has a lot going one but the summary is this: • Create a string of 12 # and 10 spaces. • Find the right spot (based on the input) in the middle of that string to start pulling characters. • Pull 11 characters (1 # + 2*input # + however many spaces). • Add ### and a line break. • Set the resulting string to the variable b. • a&"\n"&b&b&b&a concatenates the pieces into an output. • you can get to 74 bytes with =LET(a,REPT("#",12),b,MID(a&REPT(" ",10),12-2*A1,11)&"###\n",a&"\n"&b&b&b&a) or =LET(a,REPT("#",12),a&"\n"&REPT(MID(a&REPT(" ",10),12-2*A1,11)&"###\n",3)&a) Feb 26 at 0:46 # C++, 267 bytes #include<iostream> #define c std::cin #define o std::cout #define h()o<<H<<'\n' #define L 12 #define H 111111111111 #define M 1 typedef int I; I main(){I l;c>>l;h();for(I i=0;i<3;i++){o<<M;for(I j=0;j<l*2;j++){o<<1;}for(I k=0;k<L-l*2-2;k++){o<<' ';}o<<111<<'\n';}h();}  Try it Online! First time golfing in C++, not sure if it's good or not. • 162 bytes Mar 14 at 23:48 # JavaScript, 76 70 bytes -6 thanks to Hannesh _=>(a=############ )+#${'##'.repeat(_).padEnd(10)}###
.repeat(3)+a


Try it Online!

• Could save 6 bytes by using .padEnd(10) instead of +' '.repeat(5-_) Mar 6 at 3:22

# brainfuck, 202 bytes

+++++[>++>++++++<<-]>>++>-............<<.>>>>++++[->+++<]>>>>>+++++++[<+++++++>-],<--[>-<-]>[<+<+<+>>>-]<<<[<[-<+<+>>]<[>+<-]>>[<<++<-->>>-]<<-[-<<.>>]<[-<<.>>]<...<<.>>>>>>>[[<+>-]>]<<[<]>>]<[<<<.>>>-]


Try it online!

first, I try to comment this shorter bytes brainfuck to first brainfuck(245 bytes) comment, but i don't have enough reputation. so i decide to comment here and give the link to that first comment. also if I can, I will try to expaining this code later

# Python 3, 86 bytes

a,b="#"*12,int(input())
c="#"+"#"*2*b+" "*(10-2*b)+"###"
print("\n".join([a,c,c,c,a]))


Try it online!

Quick and dirty, but it works.

# Charcoal, 49 bytes

Ｐ×-¹²Ｐ↓×-⁵≔Ｎα↓→Ｆ³«Ｐ×-×α²↓»Ｐ×-¹¹Ｆχ«→»Ｐ↑×-⁴Ｆ³«↑Ｐ×-³


Try It Online! Link is to verbose version of code.

Uses "-" instead of "#" because if I change, The Output uses both "-" and "#".

## Explanation:

TODO: Understand the code and explain

• Trivial golfs: ↓→ can be ↘; Ｆχ«→» can be Ｆχ→ or Ｍχ→; Ｐ×-¹² can be Ｐ¹² (but not when using ↓); ≔Ｎα can be Ｎα (but I normally use θ instead of α).
– Neil
Feb 24 at 20:22
• @Neil Thanks for 8 bytes. Feb 24 at 21:44
• Actually the Ｐ×- was just an example; you can do the same simplification to Ｐ in a couple of other places too.
– Neil
Feb 24 at 23:21

# 05AB1E, 19 18 bytes

X12×$·×Tj7bìĆRD».∊  Outputs 1 instead of #. Explanation: X # Push 1 12 # Push 12 × # Repeat the "1" 12 times: 111111111111$               # Push 1 and the input
·              # Double the input
×             # Repeat the "1" that many times
7bì        # Prepend the binary of 7: 111
Ć       # Enclose; append its own head
R      # Reverse it
D     # Duplicate it
»    # Join the stack with newline delimiter
.∊  # Mirror it vertically with overlap
# (after which the result is output implicitly)


# Noether, 76 bytes

I~l12("#"P!i)?3(0~rl2*1+("#"P!r)0~r11l2*-(r0{>}{" "P}!r)"###"P?!k)12("#"P!y)


Try it online!

# Java (JDK), 98, 95 bytes

L->{System.out.format("%s"+"%2$-11s###\n".repeat(3)+"%1$s","############\n","#".repeat(L-~L));}


Try it online!

# Perl 5, 63 bytes

$_=($s='#'x12 .$/).('#'.'#'x($x=2*$_).$"x(10-$x)."###\n")x3 .$s


Try it online!

• 57 bytes
– Neil
Feb 27 at 0:57
• Nice, no need for $x here. Feb 28 at 9:19 • Nice, I came up with pretty much the same approach for 44 bytes, but it does necessitate resetting $\ in your test suite (works as a single run without a problem though!) Mar 1 at 9:36
• Gosh, nice too. I missed the fine print about allowing other chars than #. Mar 1 at 17:13

# J, 5046 44 bytes

(a,3 1#(a=:12#'#'),:~'# #'#~(>:,10&-,3:)@+:)


Found that building row by row can be shorter.

Try it online!

# Lua, 87 bytes

x,b=...,"\n"d=b.rep a=d("#",12)print(a..b..d(b.format("%-11s###\n",d("#",1+2*x)),3)..a)


Attempt This Online!

My alternate approach before the format string was 6 bytes longer at 93 bytes:

x,b=...,"\n"d,f=b.rep,''a=d("#",12)print(a..b..d(d("#",1+2*x)..d(" ",10-2*x).."###"..b,3)..a)


# Pip, 29 bytes

i:00YPiX6L3P[iJiXa.sXt-2*ai]y


Attempt This Online!

# APL+WIN, 51 bytes

Prompts for n

t←14↑12⍴'#'⋄v←3 14⍴' '⋄v[;(⍳1+2×⎕),11+⍳3]←'#'⋄t⍪v⍪t


Try it online! Thanks to Dyalog Classic

# PowerShell, 45 bytes

%{'#'*12;"#{0,-10}###n"-f('##'*$_)*3+'#'*12}  Try it online! Try it in a PS console (Windows or Core):  0..5|%{'#'*12;"#{0,-10}###n"-f('##'*$_)*3+'#'*12}


## Explanation

% is an Alias for the Cmdlet "ForEach-Object", which accepts input from the pipeline and processes each incoming object inside the ScriptBlock {...}
'#'*12; First line, PowerShell can "multiply" strings; the ; terminates the command, output is implicit.
"#{0,-10}###n" The level row; {0,-10} is a placeholder where the first argument of the following -f format operator will be inserted. -10 will print the string inserted left-aligned, and padded to a length of 10. The  n is a newline.
-f('##'*$_) Inserts the actual level into the row using the -f format operator; $_ is the current input value.
*3 Multiplies the level row created by "#{0,-10}###n"-f('##'*\$_) three times
+'#'*12 Adds the last line to the level rows created above.
Output is implicit.

• The code %{...} is a code sinppet, not a program or function. Feb 25 at 10:58
• We had that discussion before at Return all letter counts as an integer, and I stand by what I said there. ForEach-Object (aka %) is a built-in function/cmdlet which takes input from stdin, which is a perfectly allowed method. What exactly is your issue with that? Feb 25 at 11:22
• 1. I remember. I have written for other readers. 2. You are not using a stdin in your code, you are using a piece of pipe - code snippet. 3. please use TIO field input if you want use stdin. Thanks. Feb 25 at 18:06
• This is most definitely not a snippet, this is a fully functional PowerShell command. You can paste the code as shown into a PS shell, and it will run as it is. And using stdin as input means input through the pipeline, ever since the concept of input and output streams was invented; you're splitting hairs here, and I have no idea why. Feb 26 at 16:10
• The pipeline is not stdin. The pipeline is a PowerShell expression and should be included in the counted code. Feb 26 at 17:07

# Canvas, 23 bytes

２×╵#×α⁵∔ ×#３×）∑３＊#⁶×ｑ；Ｏ


Try it here!

### Explanation:

２×╵                         push input*2+1 (called n)
#×                       repeat '#' n times
α⁵∔ ×                  repeat ' ' 11-n times
#３×               repeat '#' 3 times
）∑            join
３＊         repeat 3 times vertically
#⁶×       repeat '#' 12 times
ｑ；Ｏ  print without popping, reverse stack, print stack


# CJam, 38 bytes

li2*:I;'#:OC*N+_O_I*+AI-S*+O3*+N+3*\++


Try it online!