Update intro-crypto u1
All checks were successful
the build was successful

This commit is contained in:
Valentin Brandl 2018-10-23 15:07:50 +02:00
parent 726448a181
commit 6d9895a152
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 73 additions and 15 deletions

View File

@ -21,7 +21,7 @@
\def \matrikel {108018274494} % \def \matrikel {108018274494} %
% \def \pname {Vorname2 Nachname2} % % \def \pname {Vorname2 Nachname2} %
% \def \pmatrikel {Matrikelnummer2} % % \def \pmatrikel {Matrikelnummer2} %
\def \gruppe {Gruppenkuerzel} % \def \gruppe {Gruppe 193} %
\def \uebung {1} % \def \uebung {1} %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -4,10 +4,61 @@ use countmap::CountMap;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::File, fs::File,
io::{BufRead, BufReader}, io::{self, BufRead, BufReader},
path::Path, path::Path,
}; };
fn analyze_text<P: AsRef<Path>>(p: P) -> io::Result<CountMap<char, u32>> {
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<I1, I2>(natural: I1, target: I2) -> HashMap<char, char>
where
I1: IntoIterator<Item = (char, u32)>,
I2: IntoIterator<Item = (char, u32)>,
{
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<P1, P2>(natural: P1, target: P2) -> io::Result<String>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
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::<String>()
}).collect();
Ok(res.as_slice().join("\n").to_owned())
}
fn count() { fn count() {
let args: Vec<_> = std::env::args().collect(); let args: Vec<_> = std::env::args().collect();
let file = &args[1]; let file = &args[1];
@ -38,6 +89,10 @@ fn count() {
println!("sum &\\text{{:}}& {}", sum); 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<P: AsRef<Path>>(path: P) -> std::io::Result<HashMap<char, char>> { fn create_map<P: AsRef<Path>>(path: P) -> std::io::Result<HashMap<char, char>> {
let read = BufReader::new(File::open(path)?); let read = BufReader::new(File::open(path)?);
let mut map = HashMap::new(); let mut map = HashMap::new();
@ -55,18 +110,21 @@ fn create_map<P: AsRef<Path>>(path: P) -> std::io::Result<HashMap<char, char>> {
} }
fn main() { fn main() {
count();
let args: Vec<_> = std::env::args().collect(); let args: Vec<_> = std::env::args().collect();
let target = &args[1]; let res = auto_decrypt(&args[1], &args[2]).unwrap();
let map = create_map(&args[2]).unwrap(); println!("{}", res);
let read = BufReader::new(File::open(target).unwrap()); // count();
for line in read.lines() { // let args: Vec<_> = std::env::args().collect();
if let Ok(line) = line { // let target = &args[1];
let s: String = line // let map = create_map(&args[2]).unwrap();
.chars() // let read = BufReader::new(File::open(target).unwrap());
.map(|c| map.get(&c).cloned().unwrap_or(c)) // for line in read.lines() {
.collect(); // if let Ok(line) = line {
println!("{}", s); // let s: String = line
} // .chars()
} // .map(|c| map.get(&c).cloned().unwrap_or(c))
// .collect();
// println!("{}", s);
// }
// }
} }