LetterPress solver

stuff about computer science and programming
Post Reply
User avatar
Site Admin
Posts: 114
Joined: Wed Oct 10, 2018 3:48 am

LetterPress solver

Post by dendiz » Wed Oct 10, 2018 11:12 pm

LetterPress is a nice combination of Go and scrabble. Here are a few lines of scala code that will give a decisive advantage

Code: Select all

import scala.io.Source
def freq(w:String) : Map[Char,Int] = {
 w.map(x=>(x,1)).groupBy(_._1).map { 
  case (key,values) => (key, values.map(_._2).sum)

val src = freq("rfcdpnrxqgeruenaolhntutim")
//val src = freq("wasttaxnmerhenriehxldtihr")

val words = Source.fromFile("corncob_lowercase.txt").getLines.toList.map(_.trim)
val f = words.map(x=>(x, freq(x)))
f.filter(x=> x._2.map({ case (k,v) => src.contains(k) && src(k) >= v}).forall(x=>x == true) )
 .sortWith(_._1.size > _._1.size)
The logic behind it is very simple:

* count the frequencies of the letters on the board (the src var has this)
* count the frequencies of all the letters per word in the dictionary ( (word, Map(w->1,o->1,r->1,d->) )
* filter out all the words that have letter frequencies less than or equal to the letters on the board
* sort these by word length

Post Reply