| Bytes | Lang | Time | Link |
|---|---|---|---|
| 075 | AWK | 250822T193002Z | xrs |
| 102 | Scala 3 | 240503T010316Z | 138 Aspe |
| 147 | C# | 240417T150450Z | Ezlandin |
| 092 | JavaScript Node.js | 240412T023431Z | 2pichar |
| 099 | Acc!! | 220224T212133Z | DLosc |
| 027 | Pip | 220224T193029Z | DLosc |
| 058 | YASEPL | 240411T135633Z | madeforl |
| 082 | R | 240410T111937Z | Patric |
| 043 | Ruby | 240411T033717Z | manatwor |
| 087 | Lua | 220316T173911Z | twentysi |
| 242 | brainfuck | 220224T202115Z | nununois |
| 202 | brainfuck | 220301T150852Z | patchara |
| 044 | J | 220304T194844Z | M Misaki |
| 070 | JavaScript | 220303T103423Z | CreaZyp1 |
| 038 | CJam | 220305T204701Z | sinvec |
| 267 | C++ | 220303T132446Z | oeuf |
| 082 | Pari/GP | 220301T084653Z | ophact |
| 074 | Excel | 220225T194729Z | Engineer |
| 023 | Canvas | 220226T185205Z | whqwert |
| 083 | C POSIX | 220226T115726Z | Dave |
| 048 | Julia 1.0 | 220225T163758Z | amelies |
| 063 | Perl 5 | 220225T175824Z | Kjetil S |
| 095 | Java JDK | 220225T023515Z | swpalmer |
| 076 | Noether | 220225T124423Z | sinvec |
| 045 | PowerShell | 220225T105217Z | user3141 |
| 018 | 05AB1E | 220225T093204Z | Kevin Cr |
| 046 | Python 3.8 | 220225T062859Z | DialFros |
| 091 | C gcc with lm | 220225T002702Z | ErikF |
| 049 | Charcoal | 220224T191404Z | Fmbalbue |
| 018 | Charcoal | 220224T204625Z | Neil |
| 055 | Retina 0.8.2 | 220224T201417Z | Neil |
| 124 | Google Sheets | 220224T200830Z | user1111 |
| 046 | Python | 220224T184251Z | pxeger |
| 051 | APL+WIN | 220224T193705Z | Graham |
| 018 | Vyxal | 220224T185538Z | emanresu |
| 086 | Python 3 | 220224T183922Z | user1008 |
AWK, 75 bytes
{for(a=(b="###")b b b RS;i++<11;)s=s (i<$1*2+2?"#":FS)}$0=a (s=s b RS)s s a
Scala 3, 102 bytes
102 bytes, it can be golfed more.
Golfed version. Attempt This Online!
n=>{val t="#"*12;val c="##"*n;val m="#"+(if(c.size<10)c+" "*(10-c.size)else c)+"###";Array(t,m,m,m,t)}
Ungolfed version. Attempt This Online!
object Main {
def createPattern(size: Int): Array[String] = {
val topBottomRow = "#" * 12
val middleContent = "##" * size
val paddedMiddleContent = if (middleContent.length < 10) {
middleContent + " " * (10 - middleContent.length)
} else {
middleContent
}
val middleRow = "#" + paddedMiddleContent + "###"
Array(topBottomRow, middleRow, middleRow, middleRow, topBottomRow)
}
def main(args: Array[String]): Unit = {
for (n <- 0 until 6) {
val pattern = createPattern(n)
pattern.foreach(println)
println()
}
}
}
C#, 163 147 bytes
-16 From @ceilingcat's suggestion to use var s="############ \n";void F(int L){Write(s);for(int i=3;i-->0;)Write(s[(11-L*2)..][..11]+s[9..]); instead of var s="############\n";void F(int L){Write(s);for(int i=3;i-->0;)Write(s[..(L-~L)]+new string(' ',10-L*2)+s[9..]);
using static System.Console;var s="############ \n";void F(int L){Write(s);for(int i=3;i-->0;)Write(s[(11-L*2)..][..11]+s[9..]);Write(s);}
Explanation:
using static System.Console;
var s="############ \n";
void PrintBattery(int L){
Write(s); // Print the top of the battery
for(int i=3;i-->0;) // Print the three middle sections of the battery
{
Write( // Print:
s[(11-L*2)..][..11] + // The starting # and battery level #s
s[9..] // The right end of the battery '###'
);
}
Write(s); // Print the bottom of the battery
}
This code works in .NET 5+
JavaScript (Node.js), 92 bytes
b=L=>console.log([t='#'[r='repeat'](12),m='#'+'##'[r](L)+' '[r](5-L)+'###',m,m,t].join`\n`)
Acc!!, 152 148 99 bytes
N-46
Count l while l-5 {
Count c while c-14+2*0^(l%4) {
Write 32+0^((c+3)/2%7/_*(l%4))
}
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. (2024 update: looks like I didn't combine nearly "as many as possible" the first time around.)
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 l while l-5 {
Do the following (output one line of the battery) five times. (The first and fifth iterations will have l be 0 and 4, so to distinguish them from the others, we can check if l%4 is zero.)
Count c while c-14+2*0^(l%4) {
Do the following (output a character in the middle part of the battery) fourteen times; but when l%4 is zero, do it twelve times. (0^x is a convenient way to turn x=0 into 1 and x>0 into 0.)
Write 32+0^((c+3)/2%7/_*(l%4))
Write either a space or an exclamation point, based on the loop index c, the accumulator value _, and the line number l.
For the inside of the battery, 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 take 0 to that power. End result: 0^(a/b) is 1 if a<b and 0 otherwise.
To handle the first and last lines, which should be entirely exclamation points, we just need to multiply the exponent by l%4. The product a/b*(l%4) is 0 if a<b or if l is 0 or 4, and some positive number otherwise. Taking 0 to that power gives the desired 1 or 0 result.
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.
Pip, 29 27 bytes
DhYPhX6L3P[hJhXa.sXt-2*ah]y
Uses the character 9. Attempt This Online!
Explanation
DhYPhX6L3P[hJhXa.sXt-2*ah]y
; h is 100, t is 10, s is space (predefined)
Dh ; Decrement h to 99
hX6 ; String-repeat h 6 times (twelve 9s)
YP ; Print that and yank it into the y variable
L3 ; Do three times:
hXa ; Repeat h (command-line arg) times
sX ; Repeat space this many times:
t-2*a ; 10 minus 2 times command-line arg
. ; Concatenate
hJ ; Join h on that string (basically add 9 to
; the front and end of it)
[ h] ; List containing that and another copy of h
P ; Print, concatenated together
y ; After the loop, print y again
R, 92 82 bytes
Inputs L and outputs the battery status to stdout.
-10 bytes by changing the rep function alias and including 2* in the definition of L.
(golfed by pajonk)
R="#";`?`=rep;cat(s<-(R?12),"\n",c(R,R?(L<-2*scan())," "?10-L,"###\n")?3,s,sep="")
Ruby, 43 characters
->l{[s=?#*12,m='#%-10s###'%(?#*l*2),m,m,s]}
Sample run:
irb(main):001:0> puts ->l{[s=?#*12,m='#%-10s###'%(?#*l*2),m,m,s]}[3]
############
####### ###
####### ###
####### ###
############
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)
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)
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).
-[-[-<]>>+<]>->>+++++[<++>-]->++++[<...>-]<<.>+>-[>+<-----]>---<,>[<->-]<[<+>-]<[>->+>->+>->+<<<<<<-]+++++[>+>>+>>+<<<<<-]->>>>>>>-<+[[[-<+]-.>+[->+]-<[+[-<+]-..>+[->+]-<-]>+<-<[+[-<+]-<<..>>>+[->+]-<-]+[-<+]-...<.>>+[->+]<->]<<+]++++[>...<-]
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
++++[>...<-]
brainfuck, 202 bytes
+++++[>++>++++++<<-]>>++>-............<<.>>>>++++[->+++<]>>>>>+++++++[<+++++++>-],<--[>-<-]>[<+<+<+>>>-]<<<[<[-<+<+>>]<[>+<-]>>[<<++<-->>>-]<<-[-<<.>>]<[-<<.>>]<...<<.>>>>>>>[[<+>-]>]<<[<]>>]<[<<<.>>>-]
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
J, 50 46 44 bytes
(a,3 1#(a=:12#'#'),:~'# #'#~(>:,10&-,3:)@+:)
Found that building row by row can be shorter.
JavaScript, 76 70 bytes
-6 thanks to Hannesh
_=>(a=`############
`)+`#${'##'.repeat(_).padEnd(10)}###
`.repeat(3)+a
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();}
First time golfing in C++, not sure if it's good or not.
Pari/GP, 82 bytes
n->[print(l)|l<-[s="############",e=Strchr([35-3*(j>n+n&j<11)|j<-[0..13]]),e,e,s]]
This code is most probably not the shortest possible, as this is my first time golfing in Pari/GP.
Thanks @alephalpha for -14 bytes.
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)definesato be 12 number signs in a row. CombiningLET()andREPT()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.
- Create a string of 12
a&"\n"&b&b&b&aconcatenates the pieces into an output.
Canvas, 23 bytes
2×╵#×α⁵∔ ×#3×)∑3*#⁶×q;O
Explanation:
2×╵ push input*2+1 (called n)
#× repeat '#' n times
α⁵∔ × repeat ' ' 11-n times
#3× repeat '#' 3 times
)∑ join
3* repeat 3 times vertically
#⁶× repeat '#' 12 times
q;O print without popping, reverse stack, print stack
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
);
}
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);}
Java (JDK), 98, 95 bytes
L->{System.out.format("%s"+"%2$-11s###\n".repeat(3)+"%1$s","############\n","#".repeat(L-~L));}
Noether, 76 bytes
I~l12("#"P!i)?3(0~rl2*1+("#"P!r)0~r11l2*-(r0{>}{" "P}!r)"###"P?!k)12("#"P!y)
PowerShell, 45 bytes
%{'#'*12;"#{0,-10}###`n"-f('##'*$_)*3+'#'*12}
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.
05AB1E, 19 18 bytes
X12×$·×Tj7bìĆRD».∊
Outputs 1 instead of #.
Try it online or verify all test cases.
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
Tj # Pad it with leading spaces up to length 10
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)
Python 3.8, 85 72 68 49 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
-13 thx to @Aiden chow
-4 thx to @ovs
-3 thx to @Aiden chow
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);}
Charcoal, 49 bytes
P×-¹²P↓×-⁵≔Nα↓→F³«P×-×α²↓»P×-¹¹Fχ«→»P↑×-⁴F³«↑P×-³`
Try It Online! Link is to verbose version of code.
My second Charcoal answer!
Uses "-" instead of "#" because if I change, The Output uses both "-" and "#".
Explanation:
TODO: Understand the code and explain
Charcoal, 18 bytes
UO⊕⊗N³-¹²↘‖O↓UO±³-
Try it online! Link is to verbose version of code. Explanation:
UO⊕⊗N³
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.
‖O↓
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.)
UO±³-
Draw the 3×3 square end.
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.
Google Sheets, 124 bytes
=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)}
Python, 46 bytes
lambda n:[s:="#"*12,*3*[f'#{"##"*n:10}###'],s]
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]
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!
APL+WIN, 51 bytes
Prompts for n
t←14↑12⍴'#'⋄v←3 14⍴' '⋄v[;(⍳1+2×⎕),11+⍳3]←'#'⋄t⍪v⍪t
Vyxal, 18 bytes
d×*₀↲×p14e:"×12*p∞
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
Python 3, 86 bytes
a,b="#"*12,int(input())
c="#"+"#"*2*b+" "*(10-2*b)+"###"
print("\n".join([a,c,c,c,a]))
Quick and dirty, but it works.

