12
\$\begingroup\$

Given the signal level L in the range 0-4. Your program should draw the following ASCII art.

When L=0:

               ....
               ....
          .... ....
          .... ....
     .... .... ....
     .... .... ....
.... .... .... ....
.... .... .... ....
.... .... .... ....

When L=1:

               ....
               ....
          .... ....
          .... ....
     .... .... ....
     .... .... ....
oooo .... .... ....
oooo .... .... ....
oooo .... .... ....

When L=2:

               ....
               ....
          .... ....
          .... ....
     oooo .... ....
     oooo .... ....
oooo oooo .... ....
oooo oooo .... ....
oooo oooo .... ....

And etc. You can change the characters o and . to others, it's up to you. Trailing spaces are allowed.

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

\$\endgroup\$
2
  • \$\begingroup\$ That would include allowing trailing spaces on each line, correct? \$\endgroup\$ Feb 24, 2022 at 1:50
  • \$\begingroup\$ @SuperJedi224 Yes, it is would be correct. \$\endgroup\$
    – sinvec
    Feb 24, 2022 at 1:55

17 Answers 17

4
\$\begingroup\$

J, 47 46 bytes

' .o'{~[:|.@|:[:,/(4,&0@#"+3+2*i.4)#"+1+4{.$&1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

APL+WIN, 48 bytes

Prompts for n

⊖⍉⊃((n↑m)⍴¨⊂⊂4⍴'0'),((-4-n←⎕)↑m←1+2×⍳4)⍴¨⊂⊂4⍴','

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

Python, 75 bytes

def f(n,j=4):print(*(" .o"[(i>=j)*~(n<i)]*4for i in b""));1/j;f(n,j-.5)

Attempt This Online!

Finishes with an exception. The b"" string contains the literal bytes 0x01 0x02 0x03 0x04, but they're invisible here.

Could probably be improved with some more tweaking.

76 bytes

def f(n,j=4):print(*(" .o"[(i>=j)*~(n<i)]*4for i in b""));j>0!=f(n,j-.5)

Attempt This Online!

No error.

\$\endgroup\$
5
  • \$\begingroup\$ Ok how do you know when to use lambda or when to use regular function? Also what is that b"..." string doing \$\endgroup\$
    – Aiden Chow
    Feb 24, 2022 at 10:07
  • \$\begingroup\$ In this case the function doesn't return anything, it prints to stdout, so it's shorter to use def. Here the b"" string acts as equivalent to [1,2,3,4], but it's shorter: see this tip. \$\endgroup\$
    – pxeger
    Feb 24, 2022 at 10:10
  • \$\begingroup\$ @Neil I'm not sure whether you're joking, but in either case "substandard" doesn't come across very nice. Also, ATO and TIO both generate markdown which renders as those 4 literal bytes, but they're invisible in both cases (I was wrong; it's not SE markdown's fault), so I changed it to the clearer \1\2\3\4. You're welcome to submit constructive feedback on the GitHub repository: github.com/attempt-this-online/attempt-this-online \$\endgroup\$
    – pxeger
    Feb 24, 2022 at 15:47
  • \$\begingroup\$ Thanks for correcting your post. \$\endgroup\$
    – Neil
    Feb 24, 2022 at 19:36
  • \$\begingroup\$ I checked my poor shoddy inferior imperfect inadequate second-rate unacceptable thesaurus but it didn't come up with a better synonym. Any suggestions? \$\endgroup\$
    – Neil
    Feb 25, 2022 at 19:42
2
\$\begingroup\$

Vyxal, 17 bytes

4ɾ≥vS4ɾd›*¶+4*⁋↵§

Try it Online!

-3 thanks to Seggan.

4ɾ≥               # Create a map of ones and zeroes corresponding to the bar characters.
   vS             # Stringify those
     4ɾd›         # Create the range [3, 5, 7, 9]
         *        # And repeat each char of those
          ¶+      # Append a newline to each
            4*    # Repeat each four times
              ⁋↵  # Turn into a list of lines
                § # Rotate 90°
\$\endgroup\$
2
  • \$\begingroup\$ Vyxal doesn't have something to repeat an integer as string? The challenge allows other characters, so no need for the ‛.o$İ if you can interpret the 0/1 as strings so the * repeats it instead of multiply. I see there is also to repeat, but that doesn't seem to work either. \$\endgroup\$ Feb 24, 2022 at 10:59
  • 1
    \$\begingroup\$ 17 bytes @KevinCruijssen yeah, vS works \$\endgroup\$
    – Seggan
    Feb 24, 2022 at 15:39
2
\$\begingroup\$

Charcoal, 17 bytes

↑E⁴E⁴×§.o›Iθι⁺³⊗ι

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

  ⁴                 Literal integer `4`
 E                  Map over implicit range
    ⁴               Literal integer `4`
   E                Map over implicit range
       .o           Literal string `.o`
      §             Indexed by
           θ        Input `L`
          I         Cast to integer
         ›          Is greater than
            ι       Outer value
     ×              Repeated by
              ³     Literal integer `3`
             ⁺      Plus
                ι   Outer value
               ⊗    Doubled
↑                   Print rotated 90° anticlockwise

Charcoal prints each array element in its own row (or column, if rotated as it is here) and automatically double-spaces nested arrays when printing them.

14 bytes by using the custom characters 1 and 0 instead of o and .:

↑E⁴E⁴⭆⁺³⊗ι›Iθι

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

  ⁴             Literal integer `4`
 E              Map over implicit range
    ⁴           Literal integer `4`
   E            Map over implicit range
        ³       Literal integer `3`
       ⁺        Plus
          ι     Outer value
         ⊗      Doubled
     ⭆          Map over implicit range and join
            θ   Input `L`
           I    Cast to integer
          ›     Is greater than
             ι  Outer value
↑               Print rotated 90° anticlockwise
\$\endgroup\$
3
  • \$\begingroup\$ The challenge allows other characters than .o, so you can simply use the 01 for 15 bytes. \$\endgroup\$ Feb 24, 2022 at 11:03
  • \$\begingroup\$ @KevinCruijssen I guess I could add a 14 byte alternative to show how it should be done using 0 and 1... \$\endgroup\$
    – Neil
    Feb 24, 2022 at 13:13
  • 1
    \$\begingroup\$ @KevinCruijssen I've posted that 14 byte alternative now. \$\endgroup\$
    – Neil
    Feb 24, 2022 at 19:38
2
\$\begingroup\$

Python 3, 76 bytes

f=lambda L,i=0:i<36and". o"[i%4+i/8<3or-(i%4<L)]*4+" \n"[i%4>2]+f(L,i+1)or""

Try it online!

-2 bytes by dingledooper


Python 3, 91 bytes

f=lambda L:L<45and" ".join(4*" o."[j+j+L//5>5and~(j<L%5)]for j in[0,1,2,3])+"\n"+f(L+5)or""

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 77 bytes \$\endgroup\$ Feb 24, 2022 at 20:30
  • \$\begingroup\$ @dingledooper Good idea. But it could be shorter by changing to /8 instead of *8. \$\endgroup\$
    – tsh
    Feb 25, 2022 at 2:22
1
\$\begingroup\$

05AB1E (legacy), 18 bytes

4L‹ā·>×ε4иø}ζR€.B»

Uses 01 instead of o. respectively.

Try it online or verify all test cases.

Explanation:

Uses the legacy version of 05AB1E because it can zip/transpose on a list of strings, where the new version would require a character-matrix.

4L      # Push list [1,2,3,4]
  ‹     # Check for each whether it's larger than the (implicit) input
   ā    # Push a list in the range [1,length] (without popping): [1,2,3,4]
    ·   # Double each: [2,4,6,8]
     >  # Increase each by 1: [3,5,7,9]
      × # Repeat the earlier checks that many times as strings
        #  (e.g. input=2 → [0,0,1,1] → ["000","00000","1111111","111111111"])
ε       # Map over each string:
 4и     #  Repeat it 4 times as list
   ø    #  Zip/transpose; swapping rows/columns
}ζ      # After the map: zip/transpose again, with implicit " " as filler
  R     # Reverse this list of lists
   €    # Map over each inner list:
    .B  #  Box: expand the single filler space to length 4 by adding trailing
        #  spaces based on the string with the longest length in the list
      » # Join each inner list by spaces; and then each string by newlines
        # (after which the result is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 65 bytes

->n{(0..8).map{|x|(0..3).map{|y|(y<3-x/2?" ":y<n ??o:?.)*3}*' '}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 26 bytes

Uses 1/0 instead of o/..

3 5 7 9,.{¯9 5↑⍺4⍴⍕⍵}4↑⍴∘1

Try it online!

⍴∘1: Reshape of 1. Creates a vector of L 1's.
4↑: Take 4. Extend to length 4 by appending 0's.

,.{...}: Inner product. Pair elements from the computed boolean vector and 3 5 7 9 and call the inner dfn on it. Reduce the resulting list by concatenating character matrices horizontally.
⍕⍵: Format. Convert the 1 or 0 to a string.
⍺4⍴: Reshape into a ⍺×4 character matrix by replicating the digit.
¯9 5↑: Extend to a 9×5 matrix by padding with spaces to the top and right.

\$\endgroup\$
2
  • \$\begingroup\$ Can you add a quick explanation? I understand most of it but not all and want to see if I can steal any ideas to improve my J answer. \$\endgroup\$
    – Jonah
    Feb 24, 2022 at 15:03
  • 1
    \$\begingroup\$ @Jonah Added a bit of text. Reshape and Overtake do most of the work here, the inner product combines a reduction (,/) and ¨ to save a byte. \$\endgroup\$
    – ovs
    Feb 24, 2022 at 15:16
1
\$\begingroup\$

C (gcc), 78 bytes

j,i;f(x){for(i=180;--i;putchar(j?i%5&&i/35+j/5<5?j/5+x>3?79:46:32:10))j=i%20;}

Try it online!

Pretty straightforward approach:
often times in C is convenient to iterate backwards the entire volume so we start with i=lines x width.
Each time we print the proper character based on div modulus values.

Luckily I found that I could save some Bytes by using i%35 instead of i%40 which would require some extra operations to fit the right shape of each bar.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 15 bytes

4RðḤ‘ṁ"@>z⁶Ṛx4Y

Prints with o as 0 and . as 1.

Try it online!

How?

4RðḤ‘x"@>z⁶Ṛx4Y - Main Link: integer, L
4               - four
 R              - range -> [1,2,3,4]
  ð             - new dyadic chain - f(X=[1,2,3,4], L)
   Ḥ            - double X          -> [2,4,6,8]
    ‘           - increment         -> [3,5,7,9]
        >       - X greater than L? -> [1>L,2>L,3>L,4>L]
       @        - with swapped arguments - f([1>L,2>L,3>L,4>L], [3,5,7,9]):
      "         -   zip with:
     x          -     times -> [[1>L,1>L,1>L],[2>L,2>L,2>L,2>L,2>L],[3>L,...],[4>L...]]
         z⁶     - transpose with filler of space characters
           Ṛ    - reverse
            x4  - times four (vectorises)
              Y - join with newline characters
                - implicit, smashing print
\$\endgroup\$
1
\$\begingroup\$

Javascript, 99 bytes

x=>"0004003402341234"[r="replace"](/\d{4}/g,"$&\n$&\n")[r](/\d/g,y=>+y?x<y?".... ":"oooo ":"     ")

(Didn't it use to be $0 for this and not $&?)

\$\endgroup\$
2
  • \$\begingroup\$ The + is unnecessary for -1 byte \$\endgroup\$
    – emanresu A
    Feb 24, 2022 at 6:13
  • \$\begingroup\$ @tsh fixed, I think \$\endgroup\$ Feb 24, 2022 at 12:21
1
\$\begingroup\$

JavaScript (Node.js), 73 bytes

L=>(g=i=>i--?` . 
 o`[i%20?i%5&&i/20+2*(x=i/5&3)<9|x+L&4:3]+g(i):'')(180)

Try it online!

-1 byte by Arnauld


JavaScript (Node.js), 90 bytes

L=>(g=i=>i>8?'':[1,2,3,4].map(j=>' o.'[j+j+i<8?0:j>L?2:1].repeat(4)).join` `+`
`+g(++i))``

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I was just about to post my own js answer and you beat it by half \$\endgroup\$ Feb 24, 2022 at 2:01
  • 1
    \$\begingroup\$ @SuperJedi224 Don't worry about posting not so well-golfed code. Idea behind could still be interesting somehow. \$\endgroup\$
    – tsh
    Feb 24, 2022 at 2:35
  • \$\begingroup\$ 73 bytes \$\endgroup\$
    – Arnauld
    Feb 24, 2022 at 12:47
1
\$\begingroup\$

Python 3.8 (pre-release), 104 100 93 bytes

lambda n:[f'{"oooo "*(b:=n-4+(k:=min(i,7)//2+1))+".... "*(k-max(0,b)):>20}'for i in range(9)]

Try it online!

Returns a list of strings, allowed here.

Thanks everyone for all the tips on my previous python answer. I used some of those tips here!

\$\endgroup\$
2
  • \$\begingroup\$ @DialFrost yeah, i don't even know how tsh's answer even works. recursion is way out of my league :P \$\endgroup\$
    – Aiden Chow
    Feb 24, 2022 at 7:01
  • \$\begingroup\$ tsh's answer builds the 36 parts individually. \$\endgroup\$
    – ophact
    Feb 24, 2022 at 9:28
1
\$\begingroup\$

Python 3, 76 bytes

lambda n:"".join(". o"[i<~i%4*8or-(i%4<n)]*4+"   \n"[i%4]for i in range(36))

Try it online!

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

brainfuck, 544 bytes

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

Try it online!

Builds each line in segments. Ungolfed:

memory layout
0    1    2    3    4    5    6    7    8    9    10   11   12   13
' '  dot  'o'  \n   cnt  inp  0    Nge1 0    Nge2 0    Nge3 0    Nge4

constants
space = 32
+[-->---[-<]>]>
[<<+>>>+<-]<
o = 111
-[>--<-------]>+
[<+>-]
dot = 46
>[<+>-]
+++++++[<++>-]
\n = 10
>+++++[<++>-]>
get input
-[>+<-----]>---<,>[<->-]<#

check 0
[
  >>+<<
  check 1
  -[
    >>>>+<<<<
    check 2
    -[
     >>>>>>+<<<<<<
     check 3
      -[
        >>>>>>>>+<<<<<<<<
        check 4
        -
      ]
    ]
  ]
]#
first 2 lines
++[
  15 spaces
  <+++++[<<<<...>>>>-]>-
  4 bar segment
  >>>>>>>>[<]<<<<<<<<<<<....
  [>]<.>>
]
next 2 lines
++[
  10 spaces
  <+++++[<<<<..>>>>-]>-
  3 bar segment
  >>>>>>[<]<<<<<<<<<....
  space
  [<]>.[>]>
  4 bar segment
  >>>>>>>>[<]<<<<<<<<<<<....
  [>]<.>>
]
next 2 lines
++[
  5 spaces
  <<<<<.....>>>>>-
  2 bar segment
  >>>>[<]<<<<<<<....
  space
  [<]>.[>]>
  3 bar segment
  >>>>>>[<]<<<<<<<<<....
  space
  [<]>.[>]>
  4 bar segment
  >>>>>>>>[<]<<<<<<<<<<<....
  [>]<.>>
]
last 3 lines
+++[
  -
  1 bar segment
  >>[<]<<<<<....
  space
  [<]>.[>]>
  2 bar segment
  >>>>[<]<<<<<<<....
  space
  [<]>.[>]>
  3 bar segment
  >>>>>>[<]<<<<<<<<<....
  space
  [<]>.[>]>
  4 bar segment
  >>>>>>>>[<]<<<<<<<<<<<....
  [>]<.>>
]

It could probably be shorter with a complex loop to handle generating each line but I couldn't get something like that to work.

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

Java 11, 105 102 bytes

n->{for(int i=36;i-->0;)System.out.print((i/4>8-i%4*2?" ":n<i%4?".":"o").repeat(4)+(i%4<1?"\n":" "));}

Could probably be golfed slightly more by removing the .repeat(4) and looping 144 times instead, but I don't have the time to figure it out for now.

Try it online.

Explanation:

n->{                   // Method with integer parameter and no return-type
  for(int i=36;i-->0;) //  Loop `i` in the range (36,0]:
    System.out.print(  //   Print:
       (i/4            //    If `i` integer-divided by 4
        >              //    is larger than
        8-i%4*2?       //    8 minus `i` modulo-4 times 2
         " "           //     Use a space
        :n<i%4?        //    Else-if the input is smaller than `i` modulo-4:
         "."           //     Use a dot
        :              //    Else:
         "o"           //     Use an O
       ).repeat(4)     //    Repeat this character 4 times
       +(i%4<1?        //    Once every four iterations:
         "\n"          //     Append a newline
        :              //    Every other iteration:
         " "));        //     Append a space instead
\$\endgroup\$
0

Your Answer

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

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