4
\$\begingroup\$

(Note: I'm not in the army so feel free to correct me if my formation is wrong, but I will not be changing the question. I will change any terminology mistakes though, because I'm trying to avoid air-cadet-specific wording choices)

(Note: this follows the standards for Canadian Air Cadets, not any actual army thing)

Challenge

Given some configurations for a squadron, output what it should look like.

You will use 1 non-whitespace character to represent people.

Configuration

The configuration will consist of a mandatory non-empty list, a mandatory positive integer, and an optional positive integer. The list represents the number of people per "squad", the mandatory integer represents the maximum number of rows, and the optional integer represents the number of "squads" in the squadron; in other words, the length of the list.

Each squad looks like this (without the commanders):

X X X X X
X   X X X
X   X X X

This would be a squad with 13 people in 3 rows.

A squad must be strictly wider than it is tall, unless it's a single person, in which case it's 1 × 1. It must also be as tall as possible (within the restriction for the number of rows); that is, once it is one larger than a perfect square, it should form another row. An exception is that when there are 5 people, it should still be a single row.** The gap is always placed in the second column from the left, starting from the second row and moving downwards. For example, with 28 people and 5 rows maximum:

X X X X X X
X   X X X X
X   X X X X
X X X X X X
X X X X X X

Then, after the squad is formed, a commander is to be placed 2 rows above and below the squad, in the exact centre. For odd width squads:

    X

X X X X X
X X X X X
X X X X X

    X

And for even width squads:

   X

X X X X
X X X X
X X X X

   X

Hopefully you now understand how an individual squad is formed. Then, to form a squadron, each squad has 7 blank columns between it. For example, with a maximum row count of 3 and three squads of size 15, 16, and 17:

                      X

    X                X                 X

X X X X X       X X X X X X       X X X X X X
X X X X X       X   X X X X       X   X X X X
X X X X X       X   X X X X       X X X X X X

    X                X                 X

Then, in the final step, a commander is placed two rows above the row of front commanders, directly in the centre of the squadron, to the left if it has even width. In the example above, that would be the X at the very top.

The final output may contain trailing whitespace on each line and an optional trailing newline.

Here's a reference implementation: Try It Online! (Python 3)

Input

Input will be given in any flexible format with the 3 components listed above: list, int[, int].

Rules

  • Standard loopholes apply
  • Output must be in this exact format (of course, you may choose the character to use)
  • Newline may be CR, LF, or CRLF
  • Trailing whitespace is allowed
  • Trailing newline is allowed

**Except our squadron's Flag Party formed a 3x2 pseudorectangle :P

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

Python 3, 308 314 309 305 269 bytes

Input is the number rows in a squad and a list with a single integer for each squad, indicating the number of persons in the squad.

Could probably be golfed quite a bit.

Edit: +6 bytes, fixed a bug for squads with the same number of persons as rows

Edit: -6 bytes thanks to @Mr.Xcoder

Edit: -36 bytes by combining everything to one function

def z(r,l,v=" ",z="X "):q=[z,v]+[(v*6).join(([z.center((w//r+(w%r>0))*2),v]+[r*[z],[z+[z,v*2][I*w%r and r>=w%r+I]+z*(w//r+(w%r>0)-2)for I in range(r)]][w>r]+[v,z.center((w//r+(w%r>0))*2)])[i]for w in l)for i in range(r+4)];return"\n".join(p.center(len(q[2]))for p in q)

Try it online!

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

Batch, 628 bytes

@echo off
call:s 1 %1 %4
call:s 2 %2 %4
call:s 3 %3 %4
set s=%s1%X%s1%       %s2%X%s2%       %s3%X
echo %s1%    %s2%    %s3%X
echo(
echo %s%
echo(
for /l %%i in (1,1,%4)do call:r %%i
echo(
echo %s%
exit/b
:q
set/ah=h%1,f=f%1
call set x=%%x%1%%
if %2 gtr %f% set x=X  %x:~3%
if %2 gtr %h% set x=%x:X= %
set r=%r%%x%       
exit/b
:r
set r=
for %%j in (1 2 3)do call:q %%j %1
if not "%r: =%"=="" echo %r%
exit/b
:s
set h=%3
if %2==5 set h=1
:t
set/aw=~-%2/h+1
if %w% leq %h% set/ah-=1&goto t
set x=X
for /l %%i in (2,1,%w%)do call set x=%%x%% X
set/ah%1=h,f%1=%2-h*~-w
set x%1=%x%
set s%1=%x:X=%

Takes the three squad sizes and the height as parameters. Explanation:

call:s 1 %1 %4
call:s 2 %2 %4
call:s 3 %3 %4

Calculate the dimensions of each squad.

set s=%s1%X%s1%       %s2%X%s2%       %s3%X

Calculate the squad commanders. The sN variables hold a number of spaces equal to half the width in characters of the squads rounded down, so adding this on both sides centres each commander.

echo %s1%    %s2%    %s3%X
echo(

Calculate and output the position of the front commander. This is similar to the case of the individual commanders except for the additional spacing between the squads, which has to be rounded up to allow for the sN being rounded down.

echo %s%
echo(

Output the first row of individual commanders.

for /l %%i in (1,1,%4)do call:r %%i

Output the squads.

echo(
echo %s%
exit/b

Finish by outputting the second row of per-squad commanders.

:q
set/ah=h%1,f=f%1
call set x=%%x%1%%

Get the height, full width rows and full width cadet string for the squad.

if %2 gtr %f% set x=X  %x:~3%

If this is not a full width row, remove the second cadet.

if %2 gtr %h% set x=%x:X= %

If all the rows have already been output, remove all the cadets.

set r=%r%%x%       
exit/b

Add any remaining cadets to the overall row.

:r
set r=
for %%j in (1 2 3)do call:q %%j %1
if not "%r: =%"=="" echo %t%
exit/b

Given a row in %1, add each squad's cadets for that row and output the result.

:s
set h=%3
if %2==5 set h=1
:t
set/aw=~-%2/h+1
if %w% leq %h% set/ah-=1&goto t

Calculate the number of rows and columns for this squad.

set x=X
for /l %%i in (2,1,%w%)do call set x=%%x%% X

Calculate the full width cadet string for this squad.

set/ah%1=h,f%1=%2-h*~-w
set x%1=%x%

Calculate the number of full width rows and save all the results in per-squad variables.

set s%1=%x:X=%

Also calculate the half-width string for centring purposes.

\$\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.