commit a1c11f6352bdcade6e50aed49958a6d1d300233a Author: Louis Vallat Date: Wed Feb 2 10:47:12 2022 +0100 Initial commit Signed-off-by: Louis Vallat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4015e1a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "metallibruh" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.8.4" + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..80fc9df --- /dev/null +++ b/src/main.rs @@ -0,0 +1,46 @@ +use std::{fs, env, collections::{HashMap, HashSet}}; +use rand::{seq::SliceRandom, prelude::ThreadRng}; + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> HashMap> { + let mut c = HashMap::new(); + let mut p; + for l in s.lines() { + p = "".to_string(); + for w in l.split_whitespace() { + let mut h = c.get(&p.to_string()).unwrap_or(&HashSet::new()).clone(); + h.insert(w.to_string()); + c.insert(p, h); + p = w.to_string(); + } + } + return c; +} + +fn get_random_next_from(w: &String, m: &HashMap>, r: &mut ThreadRng) -> String { + let _h = HashSet::new(); + let v = m.get(w).unwrap_or(&_h).iter().collect::>(); + return v.choose(r).unwrap().to_string(); +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let input = read_input(&arg); + let vec_in = parse_input(&input); + let mut rng = &mut rand::thread_rng(); + let mut sen = vec![]; + loop { + while !sen.last().unwrap_or(&"".to_string()).ends_with(".") { + sen.push(get_random_next_from(sen.last().unwrap_or(&"".to_string()), &vec_in, &mut rng)); + } + if !input.contains(sen.join(" ").as_str()) { break; } else { sen.clear(); } + } + println!("[{}]", &arg); + println!("{}", sen.join(" ")); + } +} +