18
\$\begingroup\$

Challenge

You just bought a brand new chair! Problem is, you have no idea how to control it so you'll need to write a program to do it for you.

Example of an adjustable chair

You can only sit in the unadjusted chair for so long. So your code will need to be as short as possible.

Examples

5,3,2,2,1

O
|
|
| _
| |
|_|_
  |
  |
  O

3,5,2,3,5

O
|  _
|  |
|__|__
   |
   |
 __|__
 OOOOO

2,13,1,1,13

O
|      _
|______|______
 ______|______
 OOOOOOOOOOOOO

Am I the only one who thinks this look like an airplane?

Chair parts

The chair has various components:

O
|
|  <- Backrest
|  _  <- Armrest
|  |
|__|__ <- Seat
   |  
   |   <- Leg
  _|_
  OOO  <- Wheels

Input

You will be outputting a chair given various variables.

The input will be in the following order, all inputs will be positive integers (always greater than 0) and will always be provided.

  1. Backrest Height
  2. Seat Width always odd
  3. Armrest Height Always less than backrest height
  4. Leg Height
  5. Wheel count Always less than or equal to seat width and Always odd

Detailed part Descriptions

The parts of the chair are:


Backrest: This amount of | is the Backrest Height

O
|
|

Seat: The amount of _ is the Seat Width, insert a | in the middle for the armrest.

__|__

Armrest: The amount of | is the Armrest Height. This should be inserted in the middle of the seat.

_
|
|

Leg: The amount of | is determined by Leg Height

|
|

Wheels: The wheels are centered below the legs. If they are more than one, all but the center wheel should have _ in the line above them.

_ _
OOO

Output

The output may have a trailing newline. Each line may have trailing spaces. Leading whitespace is not allowed

\$\endgroup\$
  • \$\begingroup\$ The third test case appears to be wrong. The seat width is far more than 2. \$\endgroup\$ – Doorknob Dec 18 '15 at 2:36
  • \$\begingroup\$ @Doorknob冰 whoops, that was a problem with the input order, I've fixed it. \$\endgroup\$ – Downgoat Dec 18 '15 at 2:38
  • \$\begingroup\$ The "airplane" one reminds me a lot of these tanks from Super Mario Bros 3. \$\endgroup\$ – El'endia Starman Dec 18 '15 at 2:58
2
\$\begingroup\$

LabVIEW, 146 LabVIEW Primitives

This got a lot bigger than I imagined...

There won't be a GIF this time, on the one Hand because of the size on the other hand because there's no way anyone can follow all the data flowing.

It does work, unless you get LabVIEW and suggest a file hoster there will be no way to check though.

\$\endgroup\$
9
\$\begingroup\$

JavaScript (ES6), 197 bytes

(b,s,a,l,w)=>eval('o="";for(i=0;i<b+l+2;i++)o+=(i?i>b?x[r](s/2-w/2+1)+(i>b+l?"O"[r](w):(p=(i<b+l?x:"_")[r](w/2))+"|"+p):"|"+(p=(i-b?x=" ":"_")[r="repeat"](s/2))+(i>b-a?"|":i<b-a?x:"-")+p:"O")+`\n`')

Explanation

Well this got rather convoluted quite quickly. I'll just comment on what each line does at a high level. Hopefully with this formatting it's not too hard to follow all the ternary operators.

(b,s,a,l,w)=>
  eval(
    o="";
    for(i=0;i<b+l+2;i++)                        // for each line
      o+=(i?                                    // add to output

        // Leg lines
        i>b?
          x[r](s/2-w/2+1)                       // spaces before wheels
          +(
            i>b+l?
              "O"[r](w)                         // wheels
            :
              (p=(i<b+l?x:"_")[r](w/2))         // spaces or wheel cover before leg
              +"|"                              // leg
              +p                                // spaces or wheel cover after leg
          )

        // Backrest lines
        :
          "|"                                   // backrest
          +(p=(i-b?x=" ":"_")[r="repeat"](s/2)) // spaces or seat before armrest
          +(i>b-a?"|":i<b-a?x:"-")              // armrest
          +p                                    // spaces or seat after armrest

      // Headrest line
      :"O")
      +\`\n\`
  )

Test

var solution = (b,s,a,l,w)=>eval('o="";for(i=0;i<b+l+2;i++)o+=(i?i>b?x[r](s/2-w/2+1)+(i>b+l?"O"[r](w):(p=(i<b+l?x:"_")[r](w/2))+"|"+p):"|"+(p=(i-b?x=" ":"_")[r="repeat"](s/2))+(i>b-a?"|":i<b-a?x:"-")+p:"O")+`\n`')
Input separated by commas (,) = <input type="text" id="input" value="5,3,2,3,5" />
<button onclick="result.textContent=solution(...input.value.split(',').map(x=>+x))">Go</button>
<pre id="result"></pre>

\$\endgroup\$
3
\$\begingroup\$

Python 2, 157 bytes

b,s,a,l,w=input()
y,z,n,A='|_\n '
s/=2;W=w/2;x=A*s;X=n+A*(s-W+1);Y=n+y
print'O'+Y*(b-a-1)+Y+x+z+(Y+x+y)*(a-1)+n+(y+z*s)*2+(n+A+x+y)*(l-1)+X+z*W+y+z*W+X+'O'*w

Example:

3,7,1,2,5

O
|
|   _
|___|___
    |
  __|__
  OOOOO
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 182

An anonymous function. Using template strings, there are 3 newlines that are significant an included in the byte count.

(b,s,a,l,w,R=(x=s/2,c=' ')=>c.repeat(x),f=`
`+R(s/2+1-w/2),h=R(s/2,'_'),g=R(w/2,'_'),v='|')=>'O'+R(b+~a,u=`
`+v)+u+R()+'_'+R(a-1,u+R()+v)+u+h+v+h+R(l-1,`
 `+R()+v)+f+g+v+g+f+R(w,'O')

No loops, using repeat for the same purpose.

Less golfed

(b, s, a, l, w, // parameters
 // local variables as parameters with default values
 R = (x=s/2,c=' ')=>c.repeat(x), // shortcut for repeat 
 f = `\n`+R(s/2+1-w/2), // filler before the wheels (used twice)
 h = R(s/2,'_'), // half seat (used twice)
 g = R(w/2,'_'), // last wheel support (used twice)
 v = '|', // vertical bar (used many times)
 u = `\n`+v // newline + bar (define below in the golfed version)
)=>
  'O'+R(b+~a, u)+  // backrest 
  u+R()+'_'+       // backrest + armrest top
  R(a-1, u+R()+v)+ // backrest + armrest
  u+h+v+h+         // seat
  R(l-1, `\n`+R()+v)+ // leg
  f+g+v+g+         // wheels support
  f+R(w, 'O')      // wheels

Test Snippet

f=(b,s,a,l,w,R=(x=s/2,c=' ')=>c.repeat(x),f=`
`+R(s/2+1-w/2),h=R(s/2,'_'),g=R(w/2,'_'),v='|')=>'O'+R(b+~a,u=`
`+v)+u+R()+'_'+R(a-1,u+R()+v)+u+h+v+h+R(l-1,`
 `+R()+v)+f+g+v+g+f+R(w,'O')
  
console.log=x=>O.innerHTML=x+'\n'

function test()
{
  p=I.value.match(/\d+/g).map(x=>+x)
  console.log(p+'\n'+f(...p)+'\n')
}
Input <input type="text" id="I" value="5,5,3,2,3" />
<button onclick="test()">Go</button>
<pre id="O"></pre>

\$\endgroup\$
  • 1
    \$\begingroup\$ +1 for more of your bytes being in the function parameters than the body of the function. \$\endgroup\$ – user81655 Dec 18 '15 at 14:37
1
\$\begingroup\$

SpecBAS - 185 168 bytes

1 INPUT b's'a'l'w: sx=(s/2)+1,wx=sx-(w/2)
2  ?"o"'("|"#13)*b
3  ?AT b,1;"_"*s;AT b+l,wx;"_"*w;AT b-a,sx;"-"
4 FOR i=b+1-a TO b+l: ?AT i,sx;"|": NEXT i
5  ?AT b+l+1,wx;"0"*w

Now that SpecBAS allows ? to be used instead of PRINT and doesn't need LET before variable assignment, it starts being a bit better at golfing.

Also helps that you can specify y,x print positions to avoid having to work out lots of spacing. This prints back, seat, arms and legs then wheels.

EDIT:

to print the back, replace a FOR...NEXT loop with pipe + return multiplied by the height (("|"#13)*b)

enter image description here

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

Perl, 181 bytes

includes +1 for -n. Put in a file and echo 5,3,2,2,1 | perl -n file.pl

($b,$s,$a,$l,$w)=/\d+/g;$,=$/;print'O',('|')x($b-$a-1),($D='|'.$"x($s/=2)).'_',("$D|")x--$a,($q='|'.'_'x$s).$q,($"x$s.' |')x--$l,($x=$"x(1+$s-($W=$w/2))).'_'x$W.'|'.'_'x$W,$x.O x$w

With comments:

#!/usr/bin/perl -n              # -n: iterate stdin, put in $_

($b,$s,$a,$l,$w)=/\d+/g;        # get the parameters

$,=$/;                          # set $OUTPUT_FIELD_SEPARATOR to \n;
                                # this is printed between each of print's args

print                           # shorter than `-p` and @_=(...); $"=$/; $_="@_";
    'O',                        # the headrest

    ('|')x($b-$a-1),            # space between headrest and armrest.
                                # (...) x $n: produces $n lists.

    ($D='|' . $"x($s/=2))       # backrest and padding ($"=' ') up to armrest
    . '_',                      # armrest.

    ("$D|")x--$a,               # backrest, padding, armrest

    ($q='|' . '_'x$s) . $q,     # backrest bottom and 1/2 seat, armrest bottom and 1/2 seat

    ($" x $s . ' |' ) x --$l,   # padding before leg and leg, repeated leg-1 times

    ( $x =                     
      $" x (1+$s - ($W=$w/2) )  # padding before wheel top
    )
    . '_'x$W . '|'              # left side of wheel top, wheel top,
    . '_'x$W,                   # right side of wheel top

    $x . O x$w                  # padding before wheels, wheels
\$\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.