main_testing2/wordle.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 06 Jun 2024 22:18:15 +0100
changeset 490 4778fefecd0c
parent 475 59e005dcf163
permissions -rw-r--r--
updated
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
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     5
object M2 { 
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
import io.Source
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
import scala.util._
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    10
// ADD YOUR CODE BELOW
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    11
//======================
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    13
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    14
//(1)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
def get_wordle_list(url: String) : List[String] = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    16
    Try(Source.fromURL(url).getLines.toList).getOrElse(List())
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
}
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    18
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
// 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
    20
// secrets.length // => 12972
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    21
// secrets.filter(_.length != 5) // => Nil
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    23
//(2) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    24
def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = xs match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    25
    case Nil => Nil
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    26
    case x :: tail if x == elem && n > 0 => removeN(tail, elem, n - 1)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    27
    case x :: tail => x :: removeN(tail, elem, n)
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    28
}
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    30
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    31
// 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
    32
// 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
    33
// 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
    34
// 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
    35
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    36
// (3)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
abstract class Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
case object Absent extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
case object Present extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
case object Correct extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    42
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    43
def pool(secret: String, word: String) : List[Char] = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    44
    (for(index <- (0 to word.length-1); if (word(index) != secret(index))) yield secret(index).toChar).toList
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    46
// t
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    47
def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    48
    if (secret.isEmpty) List()
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    49
    else {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    50
        if (secret.head == word.head) Correct :: aux(secret.tail, word.tail, pool)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    51
        else if (pool.contains(word.head)) Present :: aux(secret.tail, word.tail, pool.filterNot(_ == word.head))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    52
        else Absent :: aux(secret.tail, word.tail, pool)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    }
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    56
def score(secret: String, word: String) : List[Tip] = aux(secret.toList, word.toList, pool(secret, word))
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    59
// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    60
// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    61
// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    62
// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    64
// (4)
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    65
def eval(t: Tip) : Int = t match {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    66
    case Correct => 10
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    67
    case Present => 1
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    68
    case Absent => 0 
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
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    71
def iscore(secret: String, word: String) : Int =  (for(current <- score(secret, word)) yield eval(current)).sum
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    72
    
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    73
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    74
//iscore("chess", "caves") // => 21
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    75
//iscore("chess", "swiss") // => 20
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    76
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    77
// (5) t
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    78
def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    79
    if (secrets.isEmpty) acc
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    80
    else {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    81
        val score = iscore(secrets.head, word)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    82
        
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    83
        if (score == current) lowest(secrets.tail, word, current, secrets.head :: acc)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    84
        else if (score < current) lowest(secrets.tail, word, score, List(secrets.head))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    85
        else lowest(secrets.tail, word, current, acc)
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    86
    }
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    89
def evil(secrets: List[String], word: String) = lowest(secrets, word, Int.MaxValue ,List())
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    92
//evil(secrets, "stent").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    93
//evil(secrets, "hexes").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    94
//evil(secrets, "horse").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    95
//evil(secrets, "hoise").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    96
//evil(secrets, "house").length
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    98
// (6) t
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
def frequencies(secrets: List[String]) : Map[Char, Double] = {
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   100
    val occurrences = secrets.mkString.groupBy(identity).mapValues(_.length)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   101
    val all_letters = secrets.mkString.length.toDouble
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   102
    occurrences.map{ case (c, n) => c -> (1 - n/all_letters) }.toMap
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   105
// (7) 
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   106
def rank(frqs: Map[Char, Double], s: String) : Double = s.map(c => frqs(c)).sum
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   108
475
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   109
def ranked_evil(secrets: List[String], word: String): List[String] = {
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   110
    val frqs = frequencies(secrets)
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   111
    val ranked_secrets = evil(secrets, word).map(s => (s, rank(frqs, s)))
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   112
    List((ranked_secrets.sortWith(_._2 > _._2)).map(_._1).head )
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   113
}
59e005dcf163 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
   114
    
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   117
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   118
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   119
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   120
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   122
// This template code is subject to copyright 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   123
// by King's College London, 2022. Do not 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   124
// make the template code public in any shape 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   125
// or form, and do not exchange it with other 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   126
// students under any circumstance.