Today is Purim on which one custom is to give out triangle-shaped cookies with filling called hamantaschen (singular: hamantasch). Another custom is to drink heavily.
I'm not the most perfect baker.... I have so many irregularly-sized hamantaschen to give out and so many friends to give them to! If I sent you a picture of my cookies, can you tell me how many I have of which size and filling? But because it's Purim and I'm too drunk to read much code, it needs to be as small code as you can make.
Definitions
Size
A hamantasch can be any size. The smallest hamantasch is size 1 and looks like these:
/\ --
-- \/
Sometimes, multiple hamantaschen can overlap. The shape below counts as two hamantaschen (one size 1, one size 2):
/\
/\ \
----
Some hamantaschen have filling. This will be indicated by filling all whitespace inside with a character. Note that size 1 hamantaschen cannot have filling.
We will name hamantaschen based on filling and size. Let's use the format <filling> <size>
and if unfilled, - <size>
(you can use a space instead of a -
, but markdown doesn't like that).
Here are a . 2
, a . 4
, and a - 3
:
/\
/./\
---- /./ \
\../ /./ \
\/ --------
These are a @ 3
, a . 2
and a - 4
:
/\
/ /\
/\ / /@@\
/..\ / /@@@@\
---- --------
Here's something more difficult. See how the & 2
has less filling than you've come to expect due to the slant from the overlapping - 3
? It has a - 1
, a & 2
a - 3
and a & 4
:
--------
\ \/&/
\ /\/
\/&/
\/
Input
You will be given a text file or single string of hamantaschen (optional trailing newline and optionally padded trailing whitespace to be even).
Limits
- You can expect the string to be valid - that is, every non-whitespace character contributes to a deliciously sweet hamantasch (why waste dough?).
- You can also expect it to be properly filled or not - that is, each hamantasch it will be entirely filled with a consistent ASCII character - ASCII 32 for unfilled, or anything 32..127 for filled (excluding
/
,\
and-
). - These hamantaschen are not stacked in 3-space. All
/
and\
will be visible. All-
that are not blocked by/
and\
will be visible. Filling comes very last. - All hamantaschen will have at least half of their horizontal line (rounding up) visible.
- Any contiguous block of filling only fills the smallest hamantasch that surrounds it.
Output
Return a list of "names" of all the hamantaschen that can be found meeting the above criteria. The output can be in whatever form you'd like (a string, a hash, stdout, etc).
Test cases
Test case #1
Input #1:
/\
/ /\
/\ / /@@\
/..\ / /@@@@\
---- --------
/\
/**\
/*/\*\
/*/..\*\
--------
Output #1:
. 2
. 2
- 4
@ 3
* 4
Test Case #2
Input #2:
/\----
/\/\*\/
/ /\d\/
------
Output #2:
- 3
- 2
d 2
- 1
* 2
- 1
Test #3
Input #3:
----
\/\/
/\/\ /\
---- /::\
----
Output #3:
- 1
- 1
- 2
- 1
- 1
- 2
: 2
Test #4
Input #4:
/\/\
/ /\$\
-/--/\\
--/--\
/xxx/\
/xxx/##\
---/----\
/ \
--------
Output #4:
$ 2
x 4
- 3
- 2
- 4
- 1
- 1
# 2
Invalid Test Case #5
Input:
/\
\/
Output:
You do not need to handle this.
/
and\
, and-
will always trump filling. \$\endgroup\$(1,0)
, are off by+1
. Still, I know what you mean, and I disagree. What indication is there that(2, 2)
is the top center of a- 2
and not just the top right and left of the two upper- 1
s? None that I can see. And the same logic applies to(3, 2)
. Unless you want to add a rule to assume maximum possible hamantaschen... \$\endgroup\$