Alice is an intern in a company that uses Brainfuck as a primary language for both client-side and server-side development. Alice just has written her first code and she's a bit nervous as she's getting ready for her first code review.
Alice wants to make her code properly formatted and look nice, but she has no time to read the 328-pages company code style guide, so she decided to format it as a perfect square. Alas, code length may be insufficient to form a square, so she decided to leave a rectangular gap in the middle. Gaps must be perfectly centered and as close to square as possible.
++++++ +++++ +++++ +++++ +++++ +++++ ++++++ +++++ +++++ + ++ ++ ++ +++++ ++++++ ++ ++ + + + ++ + + ++++++ +++++ +++++ +++++ ++ ++ +++++ ++++++ +++++ +++++ +++++ +++++ +++++ Perfect OK Acceptable Unacceptable No way! Nope.
Write a program or a function to help Alice.
Given Alice's code as an input string, output properly formatted code as described below if possible.
If formatting is impossible, output crying emoji
This is code-golf so answers are scored in bytes with fewer bytes being the aim.
- Your program or function should take a single string as an input and output one or more lines of text (or return multi-line string or array of strings, if you implement a function).
- Input string may contain any ASCII characters including whitespaces.
- All whitespaces in input must be ignored. They shouldn't count for code length and they shouldn't be used in output.
- Input string contains at least one non-whitespace character.
- Formatted code must have the same non-whitespace characters in the same order as in input code.
- Formatted code must be a perfect square, i.e. all lines should have same length, and number of lines should be equal to the lines' length.
- Formatted code may contain a gap in the middle.
- Only space characters (ASCII code 32) may be used in the gap.
- Gap (if present) must be rectangular.
- Every line of formatted code must contain at least one non-whitespace character, i.e. gap width must be strictly less than square width (gap 5x1 is unacceptable for 5x5 square).
- Gap should be horizontal, i.e. gap width should be greater than or equal to gap height.
- Gap must be perfectly centered.
- Consequently gap width and height parity should be the same as square width parity (for example for 5x5 square gap may be 1x1, 3x1, or 3x3).
- If possible, output square with no gap at all.
- In case of multiple solutions, choose one with gap closest to square, i.e. difference between gap width and gap height is minimal (for example: gap 10x10 is more preferable than 8x6, and 8x6 is more preferable than 6x2).
- If still there's a tie, choose solution with minimal gap area (for example gap 2x2 is more preferable than 4x4).
- If it is impossible to format code at all, output
- Newline after last line is optional.
- [New] You may safely assume that any character with code below 33 is a white-space. I hope it will help you for golfing.
Input Output Code length Comment +++++++++ +++ 9 Alice is lucky, +++ her code perfectly fits a square. +++ ++++++++ +++ 8 Though code length isn't enough for a square, + + a small gap fixes it. +++ ++++++ :~( 6 No luck, code cannot be formatted. Hello, Hell 12 Input may contain any ASCII characters, World! o , but whitespaces in input should be ignored. W o rld! +++++ + +++++ +++++ 22 Gap is not required to be a square, +++++ + +++++ +++++ it can be a rectangle. + + +++++ +++++ +++ + +++ ++++++++ 28 There exists another solution: +++ + +++ + + 6x6 square with 4x2 gap, +++ + +++ + + but in Alice's opinion square gap +++ + +++ + + makes code more readable. + + + + + + ++++++++
This must be Thism 24 7x7 with 5x5 gap looks good, 5x5 with 1x1 ustbe but 5x5 with 1x1 gap is better, gap. 5x 5w because gap area is smaller. ith1x 1gap. +++ +++ +++ :~( 18 In case you tried 5x5 square +++ +++ +++ with 7x1 gap ;)
To save space you can find sample code and extra test cases on tio.run
[New] You may take a look at the table of accepted solutions for inputs upto 100 characters. I swapped width and height because it seems to look more intuituve.
Inspired by: A square of text
Added 2 tests, fixed bug in sample code.
Added table of solutions upto 100, added white-space clarification.