DEV Community

Discussion on: Daily Challenge #243 - Redacted!

Collapse
 
exts profile image
Lamonte • Edited

Dart, bit longer, but it's fine lol. I still abide by the rule that not everything needs to be creative. Readability is my philosophy. If you can read the code and understand what's happening, the better for everything reading the code. Personally.

void main() {
  var doc1 = "TOP SECRET:\nThe missile launch code for Sunday XXXXXXXXXX is:\nXXXXXXXXXXXXXXXXX";
  var doc2 = "TOP SECRET:\nThe missile launch code for Sunday 5th August is:\n7-ZERO-8X-ALPHA-1";
  print(allegedlySimilar(doc1, doc2)); // true

  doc1 = "The name of the mole is Professor XXXXX";
  doc2 = "The name of the mole is Professor Dinglemouse";
  print(allegedlySimilar(doc1, doc2));  // false

  doc1 = "XXXXXXXX XXXXXXX XXXXXXXXXXXXXXXXXXX\nXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXX XXXXX";
  doc2 = "Area-51. Medical Report. 23/Oct/1969\nE.T. subject 4 was given an asprin after reporting sick for duty today";
  print(allegedlySimilar(doc1, doc2)); // true
}

bool allegedlySimilar(String doc1, String doc2) {
  var redacted = (data) {
    List<String> joined = List<String>();
    var lines = data.split("\n");
    for(var line in lines) {
      var data = StringBuffer();
      line.split('').forEach((char) => data.write('X'));
      joined.add(data.toString());
    }
    return joined.join("\n");
  };
  return redacted(doc1) == redacted(doc2);
}