See Hole #1 if you are confused.
What does every kid do after he/she collects buckets of candy over Halloween?
Sort it by type and size, of course1!
The Challenge
Given a dumped-out bag of candy of varying shapes and sizes, sort the candy from left to right based on:
- First: The amount of candy (so 5 of one will be more left than 4 of another)
- Second (if there are any ties after the first): If the amount is the same, the candy with more internal area (based on number of characters) will rank higher.
If after the second sort there is still a tie, then you may choose either to be first.
Input
You will be given the candy through stdin; strewn about. See examples below.
Output
Output the candy ordered in the correct order. Note, candy should always be placed in very neat columns to appease your OCD fish2. The candy of the same type should be placed directly under one another. See examples below.
What do you mean by "interior area"?
The interior area of a piece of candy is measured by the total characters making up the candy as a whole.
Any whitespace within a "border" is considered part of the area of the candy.
A border is any connected loop of characters, each character diagonal or next to it's neighbor.
For example,
+--------+
| |
| |
| |
| |
+--------+
has more area than
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
even though is has less characters overall.
Examples
input:
_ \| |/ _
_ lllllll -*------------*- -\ /- lllllll
lllllll lllllllll | /\ /\ / | +|\ooooo/|+ lllllllll
lllllllll llll+llll | / \/ \/ | ||o o|| llll+llll
llll+llll lllllllll -*------------*- ||o o|| lllllllll
lllllllll lllllll /| |\ +|/ooooo\|+ lllllll
lllllll | -/ \- |
| | _ |
| -\ /- | lllllll |
| +|\ooooo/|+ | lllllllll |
| ||o o|| | llll+llll |
| ||o o|| + lllllllll rrr--rrr +
+ +|/ooooo\|+ lllllll rr||rr
-/ \- | | || |
| | || |
| | || |
| | || |
| \| |/ | || |
+ -*------------*- | || |
| /\ /\ / | | || |
-\ /- | / \/ \/ | | || |
+|\ooooo/|+ -*------------*- rr||rr
||o o|| /| |\ rrr--rrr
||o o||
+|/ooooo\|+
-/ \-
Would become
_ \| |/ -\ /- rrr--rrr
lllllll -*------------*- +|\ooooo/|+ rr||rr
lllllllll | /\ /\ / | ||o o|| | || |
llll+llll | / \/ \/ | ||o o|| | || |
lllllllll -*------------*- +|/ooooo\|+ | || |
lllllll /| |\ -/ \- | || |
| | || |
| \| |/ -\ /- | || |
| -*------------*- +|\ooooo/|+ | || |
| | /\ /\ / | ||o o|| | || |
| | / \/ \/ | ||o o|| rr||rr
+ -*------------*- +|/ooooo\|+ rrr--rrr
/| |\ -/ \-
_
lllllll \| |/ -\ /-
lllllllll -*------------*- +|\ooooo/|+
llll+llll | /\ /\ / | ||o o||
lllllllll | / \/ \/ | ||o o||
lllllll -*------------*- +|/ooooo\|+
| /| |\ -/ \-
|
|
|
|
+
_
lllllll
lllllllll
llll+llll
lllllllll
lllllll
|
|
|
|
|
+
_
lllllll
lllllllll
llll+llll
lllllllll
lllllll
|
|
|
|
|
+
A second example:
qq \/
qq qq qq +-----+
qq qq qq + |
jjjjjjjj qq qq | |
jjjjjj \/ qq qq | |
jjjj +-----+ <---notice that the left side is not connected qq +-------+
jj jj + | <--> <-->
j j | |
jj <> jj | | <--> qq jjjjjjjj
jj jj +-------+ qq jjjjjj
jjjj qq qq jjjj
qq qq jj jj
j j
+---------------------------------------------------------+ jj <> jj
ooooo +---------------------------------------------------------+ jj jj
o yyyyyy jjjj
o ww - notice diagonal border, allowed
o ww jjjjjjjj
o yyyyyy ooooo ooooo jjjjjj
ooooo o yyyyyy o yyyyyy jjjj
o ww o ww jj jj
o ww o ww j j
o yyyyyy o yyyyyy jj <> jj
ooooo ooooo jj jj
jjjj
Solution:
qq ooooo jjjjjjjj <--> \/ +---------------------------------------------------------+
qq o yyyyyy jjjjjj +-----+ +---------------------------------------------------------+
o ww jjjj <--> + |
qq o ww jj jj | |
qq o yyyyyy j j <--> | |
ooooo jj <> jj +-------+
qq jj jj
qq ooooo jjjj \/
o yyyyyy +-----+
qq o ww jjjjjjjj + |
qq o ww jjjjjj | |
o yyyyyy jjjj | |
qq ooooo jj jj +-------+
qq j j
ooooo jj <> jj
qq o yyyyyy jj jj
qq o ww jjjj
o ww
qq o yyyyyy jjjjjjjj
qq ooooo jjjjjj
jjjj
qq jj jj
qq j j
jj <> jj
qq jj jj
qq jjjj
Note that columns have 1 character of spacing between them, and are aligned horizontally at the top. Also note that each candy is in an exact columns, each candy with 1 character of spacing in between.
Scoring
This is code-golf, so shortest program (in bytes) wins.
1 What else would you do? Obviously you want to display your awesome might and power in the amount of candy you collected, right?
2 I know what you're thinking! A fish has to look through a curved bowl at your candy, so therefore it would be distorted anyway! Well, my fish lived (before it died) in a rectangular aquarium.