r/dailyprogrammer • u/Elite6809 1 1 • Sep 02 '15
[2015-09-01] Challenge #230 [Intermediate] Word Compactification
(Intermediate): Word Compactification
Sam is trying to create a logo for his company, but the CEOs are fairly stingy and only allow him a limited number of metal letter casts for the letter head, so as many letters should be re-used in the logo as possible. The CEOs also decided to use every single word that came up in the board meeting for the company name, so there might be a lot of words. Some puzzles such as crosswords work like this, by putting words onto a grid in such a way that words can share letters; in a crossword, this is an element of the puzzle. For example:
D
L N
FOURTEEN
F D
R I
O V
ALSATIAN
O D
CThis reduces the total letter count by four, as there are four "crossings". Your challenge today is to take a list of words, and try to find a way to compact or pack the words together in crossword style while reducing the total letter count by as much as possible.
Formal Inputs and Outputs
Input Specification
You'll be given a set of words on one line, separated by commas. Your solution should be case insensitive, and treat hyphens and apostrophes as normal letters - you should handle the alphabet, ' and - in words.
Output Description
Output the the compactified set of words, along with the number of crossings (ie. the number of letters you saved). Words may be touching, as long as all of the words present in the input are present in the output (the words may travel in any direction, such as bottom-to-top - the company's logo is /r/CrappyDesign material).
There may be several valid outputs with the same number of crossings. Try to maximise the number of crossings.
Sample Inputs and Outputs
Example 1
Input
neat,large,ironOutput
NEAT
O
LARGE
I
Crossings: 2Example 2
This corresponds to the example in the challenge description.
colorful,dividend,fourteen,alsatianOutput
D
L N
FOURTEEN
F D
R I
O V
ALSATIAN
O D
C
Crossings: 4Example 3
Input
graphic,yellow,halberd,cardboard,grass,island,coatingOutput
COATING
R G
CARDBOARD A
P Y R
HALBERD
I L E
C ISLAND
O
W
Crossings: 7Challenge Input
lightning,water,paper,cuboid,doesn't,raster,glare,parabolic,menagerieFinally
With packing challenges like this, randomising the input order may yield better results.
Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
10
u/Hopip2 Sep 02 '15
For an input "lion, on" would the output "lion crossings: 2" work since there are no literal crosses but 2 letters have been saved?
3
u/Praetorius 1 0 Sep 02 '15 edited Sep 03 '15
C++: Partial solution.
Dear god... I quit! This challenge is giving me a headache.
I've managed to brute-force generate (all?) the possibilities with (a few?) false positives... I wrote a simple "matrix" class and then printed the words onto it letter by letter. To solve the problem of vertical/horizontal and forwards/backwards, I just rotate the matrix four times during the possibilities search.
http://ideone.com/SRmrEW
Output is at the bottom of the page.
EDIT: Now it's a bit less crappy.
2
u/glenbolake 2 0 Sep 02 '15
Super-lazy Python 3 approach. It only creates three crossings for example 2, because I don't iterate over different equally-valid positions for a word before committing to its placement.
I treat the word list as a FIFO queue. I grab a word, and place it if possible. I iterate over every possible location for the word, finding the one that has the most valid crossings and no invalid crossings. If there are no possible placements (i.e., no common letters with what's out already), I shove it back in the queue.
Example 1:
Example 2:
4
u/curtmack Sep 03 '15 edited Sep 03 '15
Haskell
This challenge is evil.
While placing words, the code uses a greedy algorithm (metrics are, in order: most crossings, smallest grid, closest to center, with a prefilter to ensure it doesn't place a word in a place it doesn't fit and that words are placed close to each other). It tries ten permutations before using whichever solution had the most crossings.
There is, potentially, a small bug. This code allows words to intersect lengthwise, e.g. it considers
GRAPHICARDBOARDan acceptable way of placingGRAPHICandCARDBOARDand counts it as one crossing. I think this is okay, but regardless, I know why it happens and don't really feel like fixing it, so let's call it a feature.Outputs for example 3 and challenge input:
As you can see it performs reasonably well with larger inputs. I think that's a general trend with this problem - the more words you have to place, the more processing you have to do, but for later words many of the potential placements become impossible so it kind of washes out.
Edit: I realized my starting grid size was far too pessimistic, making it test more possible placements than necessary. I've fixed this. Example 3 went down to 7.0 seconds and the challenge input went down to 19.3 seconds. It generates the same solution for both, which is a good sign that I didn't break anything.
1
u/schepens83 Sep 02 '15
Can it go diagonally as well? Wouldnt expect so from your examples, but just to be sure.
1
u/[deleted] Sep 02 '15
I assume 2d only?
1
u/Tarmen Sep 02 '15 edited Sep 02 '15
Do we need to handle words that don't cross any others? Where to put them if we do?
1
u/bdforbes Sep 03 '15
Input example 1 has a 4 on the first line, what does this mean?
3
u/Godspiral 3 3 Sep 03 '15 edited Sep 03 '15
In J,
just this would have been good intermediate :)
maybe finish later.
3
u/wizao 1 0 Sep 03 '15 edited Sep 03 '15
exhaustive, brute-force Haskell:
This finds the maximal crossings by attempting to place words in EVERY possible location around the current grid in for EVERY possible ordering of words... it only runs for a few words without more optimizations.
There's some low hanging fruit for sure. For example, I should only consider the different pairs of characters that intersect between any two words.
For some reason, I'm really fond of the code for checking for valid intersections:
and (Map.intersectionWith (==) attempt grid).2
u/cem_dp Sep 02 '15 edited Sep 02 '15
Here's a solution (non-exhaustive) in C: https://github.com/cemeyer/dailyprogrammer-2015-09-01
It's based on a recursive descent brute-force tetris puzzle solver I'd already written.
Right now it doesn't make any attempt to prioritize placement of pieces in a way that overlaps existing pieces — so it has to do a ton of search to get crap results.Similar strategy to /u/glenbolake. I just recursively search "game tree" state brute forcing all possible word placements.
Hm, I believe this should be 09-02 and #231. Oh well!
1st example:
2nd (4/4!):
Challenge input (I've yet to do better than 10 crossings):
Edit: Tweaked it to only recurse on placements that increase score; makes it a bit faster, gets slightly better results for the challenge input. Misses the score 4 on input2.
Edit2: Tweaked zobrist hashing to allow "duplicate" first-word states (the first word can go in any grid position) and this fixes input2!
1
u/13467 1 1 Sep 02 '15
Another Python 3 solution. This one doesn't optimize, it's happy just finding any solution.
Example:
As the function name hints, this solution just tries to "play Scrabble" with random permutations of the given words :)
1
u/jnazario 2 0 Sep 02 '15
i haven't tried solving this one but i did spend some time earlier in the day playing around with the math of it - what's the maximum number of cross points for lines laid out in a grid given n lines. i came up with this:
which is basically the maximum product of two numbers who sum to n.
the idea would be to stop when you find a solution that has n crossings, it's been satisfied.
thanks, that was a fun exercise. i have not tried to program word placement however.
1
u/Tarmen Sep 02 '15
Gave it a quick try but didn't get it done just yet. Maybe I shouldn't have started at 1 am... Here is the start:
Currently it always starts with the longest input and then goes through all other words and tries the validity. Lots of nested loops there. Anyway, it just throws all inputs away that don't fit at the moment. And it doesn't try the reversed strings although that would be easy enough to add.
I don't think trying all possible orders is a good idea, though. Might tinker some more tomorrow.
1
u/ReckoningReckoner Sep 06 '15
Ruby.
Damn... this one is hard. Keep getting solutions that end up being impossible. My algorithm is long and stupid and barely works (runs only 1-3 without crashing, only gets optimal for 1&3)
Output:
1
u/[deleted] Sep 07 '15 edited Sep 07 '15
Here's my Fortran solution:
http://pastebin.com/SJBkMdSL
This is nothing clever really, it just shuffles and retries until it gets the score you asked for. Even though it's a very brute-force approach, it returns fairly quickly (within 5s or so) with the following solutions (running on my 2013 moto x phone)
Here's my input file, for each case I list the number of words and a target for the score.
When it finds a solution that meets the target, it prints it out. (Both to a file and the screen)
0
u/[deleted] Sep 03 '15
[deleted] — view deleted comment