22
\$\begingroup\$

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.

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

35 Answers 35

1
2
1
\$\begingroup\$

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);}

Try it online!

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+

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

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

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

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.

\$\endgroup\$
9
  • \$\begingroup\$ The code %{...} is a code sinppet, not a program or function. \$\endgroup\$
    – mazzy
    Commented Feb 25, 2022 at 10:58
  • \$\begingroup\$ 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? \$\endgroup\$
    – user314159
    Commented Feb 25, 2022 at 11:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – mazzy
    Commented Feb 25, 2022 at 18:06
  • \$\begingroup\$ 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. \$\endgroup\$
    – user314159
    Commented Feb 26, 2022 at 16:10
  • \$\begingroup\$ The pipeline is not stdin. The pipeline is a PowerShell expression and should be included in the counted code. \$\endgroup\$
    – mazzy
    Commented Feb 26, 2022 at 17:07
0
\$\begingroup\$

Canvas, 23 bytes

2×╵#×α⁵∔ ×#3×)∑3*#⁶×q;O

Try it here!

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
\$\endgroup\$
0
\$\begingroup\$

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()
    }
  }
}
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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