diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-03-15 11:21:15 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-03-15 11:21:15 -0400 |
| commit | 60c68b3209b3c09d3cbd29b4ef33080546a43fbd (patch) | |
| tree | 48d43fa4f9c8ea2492e7ab11146b7df7bb1a1679 /challenge-004/zapwai/rust | |
| parent | 2a68a16c1d8727b183d85c88f31ae6cec6a869b1 (diff) | |
| download | perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.tar.gz perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.tar.bz2 perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.zip | |
Weekly Challenge Blast from the Past
Diffstat (limited to 'challenge-004/zapwai/rust')
| -rw-r--r-- | challenge-004/zapwai/rust/ch-1.rs | 17 | ||||
| -rw-r--r-- | challenge-004/zapwai/rust/ch-2.rs | 45 |
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-004/zapwai/rust/ch-1.rs b/challenge-004/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..494269972c --- /dev/null +++ b/challenge-004/zapwai/rust/ch-1.rs @@ -0,0 +1,17 @@ +use std::process::Command; +use std::io::prelude::*; +use std::fs::File; +use std::io::SeekFrom; +fn main(){ + Command::new("curl").arg("-s").arg("http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html").arg("-o").arg("pi.txt").output().expect("no curl"); + let mut file = File::open("pi.txt").expect("no pi"); + file.seek(SeekFrom::Start(326)).unwrap(); + let mut buffer = [0;590]; + file.read_exact(&mut buffer).unwrap(); + let string_buffer = String::from_utf8_lossy(&buffer); + for c in string_buffer.split("") { + if c != "\n" { + print!("{}", c); + } + } +} diff --git a/challenge-004/zapwai/rust/ch-2.rs b/challenge-004/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..c23fb6f568 --- /dev/null +++ b/challenge-004/zapwai/rust/ch-2.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; +use std::fs; + +fn main() { + let allow = "docgotrark"; + let file_path = "004-file.txt"; + let contents = fs::read_to_string(file_path) + .expect("No file?"); + let words = contents.split("\n"); +// let words = ["dog", "chair", "rack", "doggo"]; + let mut f = HashMap::new(); + fill(&mut f, allow); + println!("Input: {allow}"); + let mut ans = Vec::new(); + for word in words { + let mut g = HashMap::new(); + fill(&mut g, word); + if comp(&f, &g) { + ans.push(word); + } + } + ans.pop(); + println!("Output: {:?}",ans); +} + +fn fill(f :&mut HashMap<char, i32>, allow :&str) { + for c in allow.chars() { + f.insert(c, 0); + } + for c in allow.chars() { + f.insert(c, f.get(&c).unwrap() + 1); + } +} + +fn comp(f : &HashMap<char, i32>, g : &HashMap<char, i32>) -> bool { + for k in g.keys() { + if !f.contains_key(k) { + return false; + } + if f.get(k) < g.get(k) { + return false; + } + } + return true; +} |
