12
\$\begingroup\$

The barbells at my gym look like this:

=========[]-----------------------[]=========

They can hold plates of five different sizes, 2.5 pounds, five pounds, ten pounds, 25 pounds, and 45 pounds:

                .
        .   !   |
.   !   |   |   |
|   |   |   |   |
'   !   |   |   |
        '   !   |
                '

For safety, we also add a clip ] or [ on the outside of all of our plates if there are any. The bar itself weighs 45 pounds. We always put the heaviest plates closest to the center, with no gaps between any plates, and put identical plates on both sides. We also always use the minimum number of plates possible, e.g. we never use two five-pound plates on one side instead of a single ten-pound plate. So if I want to lift 215 pounds, my bar looks like this:

        .                           .
      .!|                           |!.
     !|||                           |||!
====]||||[]-----------------------[]||||[====
     !|||                           |||!
      '!|                           |!'
        '                           '

Your code, a function or complete program, must take an integer from 45 to 575, always a multiple of 5, and output the bar that adds up to that weight. For example:

Input: 45

Output (note there are no clips on an empty bar):

=========[]-----------------------[]=========

Input: 100

Output:

        !                           !
       .|                           |.
======]||[]-----------------------[]||[======
       '|                           |'
        !                           !

Input: 575

Output:

    .....                           .....
  .!|||||                           |||||!.
 !|||||||                           |||||||!
]||||||||[]-----------------------[]||||||||[
 !|||||||                           |||||||!
  '!|||||                           |||||!'
    '''''                           '''''

You can have trailing spaces on each line or not, but your output cannot have leading or trailing empty lines (the output for 45 should be one line, for 50 should be three lines, for 65 should be five lines, and so on.)

This is code golf, shortest code wins!

\$\endgroup\$
  • \$\begingroup\$ Are we allowed to print one trailing empty line for every case, including the 7-high ones, as if the string was printed using Python's print? \$\endgroup\$ – PurkkaKoodari Nov 11 '15 at 1:13
1
\$\begingroup\$

Pyth, 126 bytes

K[Z5TyT50 90)jfrT6.e::++J+?qk3\=dsm@bxKdhfqQ+45sTSSM^K8?qk3r"[]23-[]"9*27d_J"=\|""]|""\|=""|["c7s@L". !|='"jC"¾ª±À£¤¯aàI7"6

The source code contains unprintable characters, so here it is as an XXD dump:

0000000: 4b5b 5a35 5479 5435 3020 3930 296a 6672  K[Z5TyT50 90)jfr
0000010: 5436 2e65 3a3a 2b2b 4a2b 3f71 6b33 5c3d  T6.e::++J+?qk3\=
0000020: 6473 6d40 6278 4b64 6866 7151 2b34 3573  dsm@bxKdhfqQ+45s
0000030: 5453 534d 5e4b 383f 716b 3372 225b 5d32  TSSM^K8?qk3r"[]2
0000040: 332d 5b5d 2239 2a32 3764 5f4a 223d 5c7c  3-[]"9*27d_J"=\|
0000050: 2222 5d7c 2222 5c7c 3d22 227c 5b22 6337  ""]|""\|=""|["c7
0000060: 7340 4c22 2e20 217c 3d27 226a 4322 04be  s@L". !|='"jC"..
0000070: aa1f b1c0 a3a4 81af 61e0 4937 2236       ........a.I7"6

This code is extremely slow, to the point of no actual use. You can speed it up by about 1000 times by adding a .{ (set) call in between, while keeping the code functionally equivalent. Here's a copy-paste friendly version of the resulting code:

K[Z5TyT50 90)jfrT6.e::++J+?qk3\=dsm@bxKdhfqQ+45sTS.{SM^K8?qk3r"[]23-[]"9*27d_J"=\|""]|""\|=""|["c7s@L". !|='"j96235640060099376576144045263159 6
\$\endgroup\$
2
\$\begingroup\$

Python 2, 295 bytes

i=input()-45
w=90,50,20,10,5;p=".|||||'"," !|||! "," .|||' ","  !|!  ","  .|'  "
a=[' '*46]
b=zip(*a*3+['='*9+'[]'+'-'*24+'[]'+'='*9]+a*3)
v=8
j=0
while i:
 if i>=w[j]:i-=w[j];b[v]=b[-v-1]=p[j];v-=1
 else:j+=1
if v<8:b[v]=b[10];b[-v-1]=b[9]
for l in zip(*b):
 L=''.join(l).rstrip()
 if L:print L

Builds the bar vertically, then rotates and prints non-empty lines.

\$\endgroup\$
  • \$\begingroup\$ -v-1 is a prime candidate for bitwise negation; ~v. \$\endgroup\$ – Jonathan Frech Apr 28 '18 at 13:51
1
\$\begingroup\$

Charcoal, 65 bytes

¹²[]P×=⁹≔⁻N⁴⁵θWΦ⟦⁵χ²⁰¦⁵⁰¦⁹⁰⟧¬›κθ«P|§⪪”{⊞⧴&β1←Z↶RΣ”,Lι→P[≧⁻⊟ιθ»‖B←

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

¹²[]P×=⁹

Print 12 -s, the [] and 9 =s. This comprises half of the bar. The cursor is left at the beginning of the =s.

≔⁻N⁴⁵θ

Subtract 45 from the input to allow for the weight of the bar.

WΦ⟦⁵χ²⁰¦⁵⁰¦⁹⁰⟧¬›κθ«

Filter a list of the possible weights of pairs of weights for those that are not greater than the input, and repeat while the list is not empty.

P|§⪪”{⊞⧴&β1←Z↶RΣ”,Lι

Split the string |||.,|.,|!,||.,||! on commas and select the piece corresponding to the heaviest possible weight, and print it both upwards and downwards. (Alternatively, it's possible to just print it upwards and reflect in the ¬ direction at the end; a number of variations have the same length.)

→P[

Print the clip (will get overwritten by the next weight if any).

≧⁻⊟ιθ»

Subtract the weight from the input.

‖B←

Reflect to complete the barbell.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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