r/dailyprogrammer • u/Cosmologicon 2 3 • Dec 08 '16
[2016-12-07] Challenge #294 [Intermediate] Rack management 2
Description
Today's challenge is loosely inspired by the board game Scrabble. You will need to download the enable1 English word list in order to check your solution. You will also need the point value of each letter tile. For instance, a is worth 1, b is worth 3, etc. Here's the point values of the letters a through z:
[1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10]
For this challenge, the score of a word is defined as 1x the first letter's point value, plus 2x the second letters, 3x the third letter's, and so on. For instance, the score of the word daily is 1x2 + 2x1 + 3x1 + 4x1 + 5x4 = 31.
Given a set of 10 tiles, find the highest score possible for a single word from the word list that can be made using the tiles.
Examples
In all these examples, there is a single word in the word list that has the maximum score, but that won't always be the case.
highest("iogsvooely") -> 44 ("oology")
highest("seevurtfci") -> 52 ("service")
highest("vepredequi") -> 78 ("reequip")
highest("umnyeoumcp") -> ???
highest("orhvtudmcz") -> ???
highest("fyilnprtia") -> ???
Optional bonus 1
Make your solution more efficient than testing every single word in the word list to see whether it can be formed. For this you can spend time "pre-processing" the word list however you like, as long as you don't need to know the tile set to do your pre-processing. The goal is, once you're given the set of tiles, to return your answer as quickly as possible.
How fast can get the maximum score for each of 100,000 sets of 10 tiles? Here's a shell command to generate 100,000 random sets, if you want to challenge yourself:
cat /dev/urandom | tr A-Z eeeeeaaaaiiiooonnrrttlsudg | tr -dc a-z | fold -w 10 | head -n 100000
Optional bonus 2
Handle up to 20 tiles, as well as blank tiles (represented with ?). These are "wild card" tiles that may stand in for any letter, but are always worth 0 points. For instance, "?ai?y" is a valid play (beacuse of the word daily) worth 1x0 + 2x1 + 3x1 + 4x0 + 5x4 = 25 points.
highest("yleualaaoitoai??????") -> 171 ("semiautomatically")
highest("afaimznqxtiaar??????") -> 239 ("ventriloquize")
highest("yrkavtargoenem??????") -> ???
highest("gasfreubevuiex??????") -> ???
Here's a shell command for 20-set tiles that also includes a few blanks:
cat /dev/urandom | tr A-Z eeeeeaaaaiiiooonnrrttlsudg | tr 0-9 ? | tr -dc a-z? | fold -w 20 | head -n 100000
4
u/skeeto -9 8 Dec 08 '16
C, with bonus 1. For each word it computes a histogram and a bitmask of that histogram (bits set where the histogram is non-zero). All words with a common histogram bitmask have their indices added to a common set. For a given input tileset, it only checks against words from bitmask-compatible sets. I couldn't figure out how to fit wildcards into this scheme, so no bonus 2.
On my computer it finds the best solution for 100,000 random tilesets in just under 30 seconds.
1
u/thorwing Dec 08 '16
paging /u/Cosmologicon
Questions: When multiple words are the "best" answer to a given tileset, do we return nothing or just any of them? I have windows with powershell in it, but I take it I still can't use that script for my computer? (I'll just generate one myself then) Thanks in advance. I'll be working on it tonight.
3
u/Boom_Rang Dec 08 '16 edited Dec 08 '16
Haskell, with bonus 1
38 seconds for 100000 highest scoring scrabble lookups!
I am using a rose tree to keep the dictionary in memory, this makes lookups very fast and building it is not too bad (and definitely not a problem when doing 100000 scrabble lookups). The code for bonus 2 is there (commented out) but is way too slow: each '?' creates exponentially many more paths for lookups in the rose tree. Since parallelism is relatively easy to achieve in Haskell and the question doesn't seem to care about preserving the order of the lookups (since they're random) I went ahead and computed it on 4 cores.
And here is my code:
Edit: small improvement since I am only attempting bonus 1: remove all entries of the dictionary that are longer than 10 characters long when building the dictionary. This improves the time from 45 seconds to about 38 seconds.
3
u/thorwing Dec 08 '16 edited Dec 08 '16
Java 8 with bonus1
This took some time to figure out a fast way to do but I think I got it. This algorithm is however under the assumption that the file containing 10000 board setups isn't preproccesable (it's the realtime value given that can be evaluated on preprocessed data). In any case; I do the following: Every word precalculates its score but also calculates it's unique, prime-product value. I ordered characters in order of occurence in the english language and mapped them to a prime number ('e' being very common and thus valued at 2, and 'z' being very uncommon and valued at 101). I then order this map by their logical ordering based on score and reiterate through it in reverse, meaning highest score first. Java 8 now has unsigned longs (or can at least treat longs as unsigned and therefore, with my unique prime mapping, boards of size 20 will not go out of range). Thanks /u/Nordiii for letting me think about the problem more. Takes less than 20 seconds on my pc. Now to think on how to do this algorithm, but with wildcards...
3
u/thestoicattack Dec 09 '16 edited Dec 09 '16
C++14. Bonus 1. Searches a trie, alternately keeping or dropping a letter at each node. 100,000 sets: 5 second on -O3.
code:
EDIT: you can save another half-second by not doing the map lookup twice, as in
instead of using find() followed by at().
EDIT2: you might be able to do better by using a comparator for the max calls which only looks at scores, since if the scores are the same the standard operator< will fall back to a string comparison we don't care about. (2a: yes, you get about another half-second. Down to 4.5.)
2
u/[deleted] Dec 08 '16 edited Jan 30 '17
[deleted]
0.5079
2
u/Poseprix Dec 14 '16
Go. With both bonuses. Works by inserting our dictionary into a trie.
Bonus 1 runs in about 30 seconds. Searches words in parallell. Didn't bother trying to run 100k samples from bonus 2, as that takes way way longer. Running on 100 samples takes approximately 3 minutes, so it should take somewhere around 50-55 hours. Wrote tests and benchmarks using go's built-in testing framework. I've thought about doing some optimizations, like removing all words longer than the rack we are given from the trie, but not sure how much performance would improve.
rack2.go
The output is printed a bit differently, the original rack is not identical to the one we started with, but it contains the same characters as the original.
Output
1
u/franza73 Dec 08 '16 edited Dec 08 '16
Python 2.7 - with bonus 2:
Output:
1
u/vaughands Dec 08 '16
Nothing impressive -- wildcards would be pretty easy to handle...
1
u/elpasmo Dec 08 '16 edited Dec 08 '16
Python3 with bonus 2
Edit: Processing 20 tiles with wildcards takes approximately 1.4313230514526367 seconds so with this code I estimate it will take 1 day, 15 hours, 45 minutes and 32 seconds to complete 100000 tiles with wildcards.
1
u/Blocks_ Dec 08 '16
Python 3, no bonuses (although can also handle 20 tiles). Feedback is appreciated. ^-^
1
u/Xmer Dec 08 '16
C# Gist with bonuses,
I did notice on bonus 2 semiautomatically was marked at 171 points but it should be 169.
Feedback appreciated!
1
u/Minolwa Dec 08 '16 edited Dec 08 '16
Scala
Supports any length of tiles, with wildcards
1
u/michaelquinlan Dec 08 '16
C# with bonuses
https://gist.github.com/mikequinlan/f99ddaed7535a89b4c7df4a2e6f74c06
1
u/draegtun Dec 08 '16
Rebol (with bonus 2)
Only required minimal changes to my earlier easy challenge solution
Example usage in Rebol console:
NB. Tested in Rebol 3
1
u/M4D5-Music Dec 09 '16
C++ attempt to maximize speed on bonus 1 with multithreading. Completes bonus 1 in about 25 seconds on my 4 core machine. Not super experienced with c++, so if i'm doing anything naive here feel free to let me know.
1
u/A-Shitty-Engineer Dec 09 '16
Java 8 (no working bonuses). Written so that the user can input their own racks of tiles.
How the output looks in the terminal:
1
u/seabombs Dec 09 '16
Python3
Bonus 1 and 2. I've only timed bonus 1, takes about 0.003 seconds per input, or about 320 seconds for the whole 100,000. For bonus 2, each input takes anywhere from ~10 seconds to a couple minutes.
Output:
1
u/glenbolake 2 0 Dec 09 '16
Python 3. I tried sorting enable descending by score, but bonus 1 still takes 4.5 hours to run (with the blanks logic commented out).
1
u/smokeyrobot Dec 09 '16
Java 8. Terribly inefficient but it works for both bonuses with a minor bug. My score for this entry is 167. highest("yleualaaoitoai??????") -> 171 ("semiautomatically")
By hand I have "?e?iauto?a?i?ally" as the replacement which is still scoring 167. Because of this my program outputs:
highest("yleualaaoitoai??????") -> 169 ("configurationally")
Any help would be appreciated.
1
u/FelixMaxwell 1 0 Dec 10 '16
Python 3
By precomputing a trie I was able to get execution time for bonus 1 under 2 minutes. However, because of how the trie is set up it will take more work to get bonus 2 working and it is already 4am.
Output:
1
u/Popey456963 Dec 10 '16
Python 3 - Bonus 1 - 5 Minutes 100,000
Python 3 - Bonus 2 - 12 Minutes 100,000
Turns out I was probably meant to do this challenge before attempting the hard one, but by going backwards I can also solve this challenge and the bonuses. My code doesn't use a trie, unlike some of the others, because as pointed out before trie's are incredibly hard to get to work with unknown values.
Instead, I precompute the score of all words, and reorder them to be in reverse alphabetical frequency order (i.e. amz --> zma, because z is least popular and a is most popular). In bonus 1, I take these scores verbatim because I don't need to worry about "?", whereas in bonus 2 I just use this as a guide and recompute later on to take into account the unknowns. I use
pypy.exeas my JIT compiler and some sneaky string manipulation to allow my deep copies to be less CPU intensive.1
u/bokisa12 Dec 11 '16 edited Dec 11 '16
JavaScript (ES6), no bonuses for now:
WHERE
util.getDictreturns an array of all the words from theenable1dictionary,util.scrabblechecks whether a word can be scrambled using the passedtiles(works with wildcards?) andutil.pointValuesis an object containing the point values for each character, including the wildcard?. You can find these functions on the last few posts on my profile from the 1st EASY rack management challenge.1
u/ASpueW Dec 12 '16
Rust, bonus 1 - 48 sec
Output:
1
u/darderp Dec 15 '16 edited Dec 18 '16
Javascript (ES6)
(No bonuses)
First try at a challenge from this subreddit! My goal with this was to use as many
Array.prototypemethods and functional patterns as I could. Not entirely sure if I'm happy with the result because I don't think it's as legible as a traditional approach, but what the hey - I tried.Output
1
u/Beat_Button Dec 25 '16 edited Jan 02 '17
Python 3, bonus 2 only because I don't know what data structure I would use to fulfill bonus 1.
1
u/fmpundit Dec 29 '16
Python 3. No bonses. bounced over to this thread after finishing all the bonses in the easy challenge and thought one of the bonses there could easily be adapted.
output:
1
u/ff8c00 Jan 18 '17 edited Mar 10 '17
C# Gist with bonus 1
1
u/Zestysavage Jan 20 '17
Nimrod solution: import strutils import sequtils
1
u/ranDumbProgrammer Jan 30 '17
C# with bonus 2
1
u/KidsMaker Jan 31 '17
Haven't optimized yet but here it is in Java:
import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Stream;
public class Programm_294 {
}
1
u/thestoicattack Feb 11 '17
Haskell. Bonus 1. 100,000 sets: 10 seconds under
ghc -O3. Uses a trie. When I left Haskell, I felt like you always had to use some obscure package or do crazy strictness techniques for performance. I was pleased to find that wasn't the case here. StandardStrings throughout, the trie children are aData.Map. The only explicit strictness is afoldl'so the trie isn't a giant thunk. A previous version, where the trie children are an association list, ran in 16 seconds.1
u/popillol Feb 20 '17 edited Feb 20 '17
Go / Golang with both bonuses. Playground Link. Not sure how fast it is yet, I'm limited to playground testing at the moment so I took a sample of the enable1.txt.
Edit: Was able to test it on pc using full enable1.txt dictionary. Can solve 20-letter inputs with wildcards in 8.8 seconds, so about .0043 seconds/input. So about 21.9 seconds for 5000 inputs. Interpolating would mean it can do a full 100k inputs in 6 min 15 sec. Using a stock i5-4690k.
Code assumes
enable1is assigned to the enable1.txt dictionaryFeedback appreciated. I got sloppy when it didn't work after I thought it should. Turned out I wasn't calculating scores of words that used wildcards. That's fixed in the code below.
1
u/rjckE Dec 08 '16
Java:
Output: