main_testing2/wordle.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 23 Dec 2022 16:52:34 +0000
changeset 456 d076cb2e0b75
parent 439 97594b9998a8
child 463 0315d9983cd0
permissions -rw-r--r--
updated jars
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     1
// Main Part 3 about Evil Wordle
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     2
//===============================
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     4
// test bash
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     5
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     6
object M2 { 
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
import io.Source
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
import scala.util._
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    11
// ADD YOUR CODE BELOW
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    12
//======================
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    14
// test import
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    15
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    16
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    17
//(1)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
def get_wordle_list(url: String) : List[String] = {
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    19
  Try(Source.fromURL(url)("ISO-8859-1").getLines().toList).getOrElse(Nil)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
// val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt")
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    24
// secrets.length // => 12972
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    25
// secrets.filter(_.length != 5) // => Nil
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    27
//(2)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    28
def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = {
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    29
    if (n == 0) xs 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    30
    else xs match {
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    31
        case Nil => Nil
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    32
        case x::xs => 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    33
            if (x == elem) removeN(xs, x, n - 1) 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    34
            else x::removeN(xs, elem, n)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    35
    } 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    36
}    
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    39
// removeN(List(1,2,3,2,1), 3, 1)  // => List(1, 2, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    40
// removeN(List(1,2,3,2,1), 2, 1)  // => List(1, 3, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    41
// removeN(List(1,2,3,2,1), 1, 1)  // => List(2, 3, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    42
// removeN(List(1,2,3,2,1), 0, 2)  // => List(1, 2, 3, 2, 1)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    44
// (3)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
abstract class Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
case object Absent extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
case object Present extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
case object Correct extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    50
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    51
def pool(secret: String, word: String) : List[Char] = {
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  for (i <- (0 to 4).toList 
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
       if secret(i) != word(i))
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  yield secret(i) 
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = (secret, word) match {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    case (Nil, Nil) => Nil
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    case (s::srest, w::wrest) => {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
        if (s == w) Correct::aux(srest, wrest, pool)
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    61
        else if (pool.contains(w)) Present::aux(srest, wrest, removeN(pool, w, 1))
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
        else  Absent::aux(srest, wrest, pool)
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    }
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    67
def score(secret: String, word: String) : List[Tip] = 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    68
  aux(secret.toList, word.toList, pool(secret, word))
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    71
// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    72
// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    73
// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    74
// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    76
// (4)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
def eval(t: Tip) : Int = t match {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
    case Correct => 10
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
    case Present => 1
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
    case Absent => 0
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    83
def iscore(secret: String, word: String) : Int = 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    84
  score(secret, word).map(eval).sum
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    85
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    86
//iscore("chess", "caves") // => 21
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    87
//iscore("chess", "swiss") // => 20
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    88
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    89
// (5)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    90
def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = secrets match {
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    91
    case Nil => acc
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    92
    case s::srest => {
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    93
        val nscore = iscore(s, word)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    94
        if (nscore < current) lowest(srest, word, nscore, List(s)) 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    95
        else if (nscore == current) lowest(srest, word, current, s::acc)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    96
        else  lowest(srest, word, current, acc)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    97
    }
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   100
def evil(secrets: List[String], word: String) = 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   101
  lowest(secrets, word, Int.MaxValue, Nil)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   104
//evil(secrets, "stent").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   105
//evil(secrets, "hexes").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   106
//evil(secrets, "horse").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   107
//evil(secrets, "hoise").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   108
//evil(secrets, "house").length
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   110
// (6)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
def frequencies(secrets: List[String]) : Map[Char, Double] = {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
    val all = secrets.flatten
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
    all.groupBy(identity).view.mapValues(1.0D - _.size.toDouble / all.size ).toMap
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   116
// (7)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
def rank(frqs: Map[Char, Double], s: String) = {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
    s.map(frqs(_)).sum
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   121
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
def ranked_evil(secrets: List[String], word: String) = {
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
    val frqs = frequencies(secrets)
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
    val ev = evil(secrets, word)
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
    ev.groupBy(rank(frqs, _)).toList.sortBy(_._1).reverse.head._2
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   128
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   131
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   132
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   133
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   134
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   136
// This template code is subject to copyright 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   137
// by King's College London, 2022. Do not 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   138
// make the template code public in any shape 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   139
// or form, and do not exchange it with other 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   140
// students under any circumstance.