Detect ECB via duplicated chunks instead of Hemming distance as I should have done in the first place.

This commit is contained in:
2022-04-27 10:34:40 -04:00
parent 06af500a52
commit 994da471c4
3 changed files with 35 additions and 23 deletions

View File

@@ -193,17 +193,28 @@ pub fn challenge8() {
rating
}
let expected_index: usize = 132;
let bytes_vector = read("data/8.txt");
let ratings: Vec<u32> = bytes_vector.iter().map(|b| rate(&b, 16)).collect();
let min_rating = ratings.iter().min().unwrap();
let index = ratings.iter().position(|e| e == min_rating).unwrap();
let average = ratings.iter().sum::<u32>() as f32 / ratings.len() as f32;
let partial_cipher = bytes_vector[index].to_hex()[..10].to_string();
if index != 132 {
if index != expected_index {
panic!("Regression in challenge 8.");
}
println!(
"[okay] Challenge 8: Cipher {} [{}...] with rating {} (average = {}) is the solution.",
index, partial_cipher, min_rating, average
);
// More elegant solution.
let bytes_vector = read("data/8.txt");
let ix: Vec<usize> = bytes_vector
.iter()
.enumerate()
.filter(|&(_, bytes)| bytes.has_duplicated_cycle(16))
.map(|(i, _)| i)
.collect();
assert_eq!(ix[0], expected_index);
}