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