From 6d9895a15275beddb05d02a74e016df9f517cd10 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 23 Oct 2018 15:07:50 +0200 Subject: [PATCH] Update intro-crypto u1 --- school/intro-crypto/uebung/01/01.tex | 2 +- school/intro-crypto/uebung/01/u01/src/main.rs | 86 ++++++++++++++++--- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/school/intro-crypto/uebung/01/01.tex b/school/intro-crypto/uebung/01/01.tex index f88e5ce..a061e40 100644 --- a/school/intro-crypto/uebung/01/01.tex +++ b/school/intro-crypto/uebung/01/01.tex @@ -21,7 +21,7 @@ \def \matrikel {108018274494} % % \def \pname {Vorname2 Nachname2} % % \def \pmatrikel {Matrikelnummer2} % -\def \gruppe {Gruppenkuerzel} % +\def \gruppe {Gruppe 193} % \def \uebung {1} % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/school/intro-crypto/uebung/01/u01/src/main.rs b/school/intro-crypto/uebung/01/u01/src/main.rs index 93d3a5e..80e61df 100644 --- a/school/intro-crypto/uebung/01/u01/src/main.rs +++ b/school/intro-crypto/uebung/01/u01/src/main.rs @@ -4,10 +4,61 @@ use countmap::CountMap; use std::{ collections::HashMap, fs::File, - io::{BufRead, BufReader}, + io::{self, BufRead, BufReader}, path::Path, }; +fn analyze_text>(p: P) -> io::Result> { + let mut map = CountMap::new(); + let read = BufReader::new(File::open(p)?); + read.lines() + .filter(Result::is_ok) + .map(Result::unwrap) + .for_each(|l| { + l.chars().filter(|c| c.is_alphabetic()).for_each(|c| { + map.insert_or_increment(dumb_to_lowercase(c)); + }); + }); + Ok(map) +} + +fn create_mapping_table(natural: I1, target: I2) -> HashMap +where + I1: IntoIterator, + I2: IntoIterator, +{ + let mut natural: Vec<_> = natural.into_iter().collect(); + natural.sort_by(|(_, u1), (_, u2)| u2.cmp(u1)); + let mut target: Vec<_> = target.into_iter().collect(); + target.sort_by(|(_, u1), (_, u2)| u2.cmp(u1)); + natural + .into_iter() + .zip(target.into_iter()) + .map(|((c1, _), (c2, _))| (c1, c2)) + .collect() +} + +fn auto_decrypt(natural: P1, target: P2) -> io::Result +where + P1: AsRef, + P2: AsRef, +{ + let natural_map = analyze_text(natural)?; + let target_map = analyze_text(&target)?; + let mapping = create_mapping_table(natural_map, target_map); + let read = BufReader::new(File::open(target)?); + let res: Vec<_> = read + .lines() + .filter(Result::is_ok) + .map(Result::unwrap) + .map(|l| { + l.chars() + .map(|c| mapping.get(&dumb_to_lowercase(c)).cloned().unwrap_or(c)) + .collect::() + }).collect(); + Ok(res.as_slice().join("\n").to_owned()) +} + fn count() { let args: Vec<_> = std::env::args().collect(); let file = &args[1]; @@ -38,6 +89,10 @@ fn count() { println!("sum &\\text{{:}}& {}", sum); } +fn dumb_to_lowercase(c: char) -> char { + c.to_lowercase().to_string().chars().nth(0).unwrap_or(c) +} + fn create_map>(path: P) -> std::io::Result> { let read = BufReader::new(File::open(path)?); let mut map = HashMap::new(); @@ -55,18 +110,21 @@ fn create_map>(path: P) -> std::io::Result> { } fn main() { - count(); let args: Vec<_> = std::env::args().collect(); - let target = &args[1]; - let map = create_map(&args[2]).unwrap(); - let read = BufReader::new(File::open(target).unwrap()); - for line in read.lines() { - if let Ok(line) = line { - let s: String = line - .chars() - .map(|c| map.get(&c).cloned().unwrap_or(c)) - .collect(); - println!("{}", s); - } - } + let res = auto_decrypt(&args[1], &args[2]).unwrap(); + println!("{}", res); + // count(); + // let args: Vec<_> = std::env::args().collect(); + // let target = &args[1]; + // let map = create_map(&args[2]).unwrap(); + // let read = BufReader::new(File::open(target).unwrap()); + // for line in read.lines() { + // if let Ok(line) = line { + // let s: String = line + // .chars() + // .map(|c| map.get(&c).cloned().unwrap_or(c)) + // .collect(); + // println!("{}", s); + // } + // } }