24
1
You're given a hexagonal grid of the characters .
and #
, like this:
. . . . . . . .
. . . . # . . .
. # . . . # . .
. . . # . . . .
. . . . . # . .
. . . . . . . .
Your task is to fill the entire axis-aligned bounding box of the #
with further #
:
. . . . . . . .
. . # # # # . .
. # # # # # . .
. . # # # # # .
. . # # # # . .
. . . . . . . .
The axis-aligned bounding box is the smallest convex hexagonal shape which contains all the #
. Note that in the case of the hexagonal grid, there are three axes to consider (W/E, SW/NE, NW/SE):
Here is another example to show that in some cases, one or more sides will contain only one #
:
. . . . . . . . . . . . . . . .
. # . . . . . . . # # # # . . .
. . . . . # . . . . # # # # . .
. . # . . . . . . . # # # . . .
. . . . . . . . . . . . . . . .
You can either view these as hexagons with degenerate sides, or you can draw the bounding box around them, like I have done above, in which case they are still hexagons:
Too hard? Try Part I!
Rules
You may use any two distinct non-space printable ASCII characters (0x21 to 0x7E, inclusive), in place of #
and .
. I'll continue referring to them as #
and .
for the remainder of the specification though.
Input and output may either be a single linefeed-separated string or a list of strings (one for each line), but the format has to be consistent.
You may assume that the input contains at least one #
and all lines are the same length. Note that there are two different "kinds" of lines (starting with a space or a non-space) - you may not assume that the input always starts with the same type. You may assume that the bounding box always fits inside the grid you are given.
You may write a program or a function and use any of the our standard methods of receiving input and providing output.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so the shortest valid answer – measured in bytes – wins.
Test Cases
Each test case has input and output next to each other.
# #
. . . .
# . # # # #
. . . .
. # . #
. . . . # .
# . # .
# . # .
. . . . # .
. # . #
# . # .
# . . # # .
. # # #
. # # #
# . . # # #
. # # #
. . # . # #
. . # #
# . . # # .
# . . # # .
. . # #
. . # . # #
. . . . . . . . . . . . . . . .
. . # . # . . . . . # # # . . .
. . . . . . . . . . . # # . . .
. . . # . . . . . . . # . . . .
. . . . . . . . . . . . . . . .
. . # . . . # . . . # # # # # .
. . . . . . . . . . . # # # # .
. . . # . . . . . . . # # # . .
. . . . . . . . . . . . . . . .
. # . . . . . . . # # # # . . .
. . . . . # . . . . # # # # . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. # . . . . . . . # # # # . . .
. . . . . # . . . . # # # # . .
. . # . . . . . . . # # # . . .
. . . . # . . . . . # # # # . .
. # . . . # . . . # # # # # . .
. . . # . . . . . . # # # # # .
. . . . . # . . . . # # # # . .
1My head is spinning trying to find any obvious pattern. You said 'hexagonal' but there are only two inputs form into hexagons in the test cases. I'm lost. – Anastasiya-Romanova 秀 – 2016-08-29T12:04:54.843
1@Anastasiya-Romanova秀 If you picture the shape as going through the centres of the outer characters, then yes some hexagons will have degenerate sides (like in the rectangular grid, where you can get cases where the rectangle reduces to a line). However, if you draw the rectangle around the characters (as I have done in the diagram), all the examples are hexagons (some of which have very short sides). – Martin Ender – 2016-08-29T12:06:55.110
1@Anastasiya-Romanova秀 Does the new diagram help? – Martin Ender – 2016-08-29T12:13:07.170
3I! looks like II if I have the wrong glasses on.. – Neil – 2016-08-29T12:17:52.430
1@Neil Or, you know, too much alcohol ;) – ThreeFx – 2016-08-29T12:35:27.920
I don't understand these boxes fine. Do I have to process multiple boxes with my program, line by line, column by column? Yea, I'm beginner and english isn't my first language :p – Hydroper – 2016-08-29T13:05:06.573
1@nicematt No, you take a single grid (e.g. the very first code block in the challenge) and return a single grid (e.g. the second code block in the challenge). The final code block is just compressed a bit to save space for the large number of test cases. Your program should be able to receive any of the grids on the left and produce the corresponding grid on the right. – Martin Ender – 2016-08-29T13:08:34.217
1Sorry @MartinEnder, I still don't get it. You don't have to explain anything anymore, let's see how one approaches this challenge, maybe I can get some ideas. Thanks. – Anastasiya-Romanova 秀 – 2016-08-29T13:21:28.670
@MartinEnder Can I right-pad? – Leaky Nun – 2016-08-29T13:56:48.580
@LeakyNun Yes: "You may assume that [...] all lines are the same length." – Martin Ender – 2016-08-29T13:58:04.860
Can I also take input without the spaces? – ThreeFx – 2016-08-29T16:31:43.033
@ThreeFx No, because then there's no way to tell whether the first row was indented or not. – Martin Ender – 2016-08-29T16:34:13.527
Technically I could, since they would have different length, but I get what you mean (it's not a costly workaround relatively speaking). – ThreeFx – 2016-08-29T17:26:27.697
@ThreeFx They wouldn't always have different lengths (see for instance the 8x4-sized test cases at the end). – Martin Ender – 2016-08-29T17:37:23.387
1I know it's highly unlikely but I want to see a Hexagony solution – CAD97 – 2016-08-29T23:56:30.340
Actually a quincunx lattice and not a hexagonal one, the angles are slightly off to be hexagonal. But +1 for fun problem. – mathreadler – 2016-08-30T06:22:47.867
1@mathreadler I never said they were regular hexagons. ;) – Martin Ender – 2016-08-30T06:25:25.603
What if the total field would need to grow to contain the hexagon ? Think of an input consisting of a narrow high vertical line – Ton Hospel – 2016-09-01T11:09:17.030
@TonHospel Ohhh, very good catch. I didn't consider that at all (and apparently no one else either so far). As I doubt any of the existing answers will handle that correctly, I'll say you can assume that won't happen. – Martin Ender – 2016-09-01T11:26:45.910