From bbb92418697e95e3f2ce193dbde046d684ba1c01 Mon Sep 17 00:00:00 2001 From: Oliver Atkinson Date: Mon, 16 Dec 2024 11:34:44 -0700 Subject: [PATCH] messing arround --- src/main.rs | 77 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 75eca5f..4bc3d3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,14 +6,29 @@ use std::time::{Duration, Instant}; use num_format::{Locale, ToFormattedString}; -const ALL_CHARS_95: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.<>/?;':\"[]{}\\|-=_+`~!@#$%^&*() "; -// const ALL_ALPHA: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; -// const ALL_ALPHA_NUM: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - -const LEN: usize = 8; +/// The length of the combination +const LEN: usize = 5; +/// How many combinations have we create thus far static mut TOTAL: usize = 0; +#[allow(dead_code)] +enum Charset { + AllChars, + AllAlpha, + AllAlphaAndNum +} + +impl Charset { + fn as_str(&self) -> &'static str { + match self { + Charset::AllChars => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.<>/?;':\"[]{}\\|-=_+`~!@#$%^&*() ", + Charset::AllAlpha => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + Charset::AllAlphaAndNum => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", + } + } +} + /// Pseudo main /// /// Starts the number crunching (blocking on main thread!) and the @@ -24,7 +39,7 @@ fn start(char_list: &str, output_len: usize) { /// timer threads. static mut IS_DONE: bool = false; /// Seconds between the calculating p/s and percentage done. - const CHECK_IN_DELAY: u64 = 1; + const CHECK_IN_DELAY: u64 = 2; let start = Instant::now(); // Try multithreading... @@ -32,7 +47,7 @@ fn start(char_list: &str, output_len: usize) { let mut counters = Vec::new(); let mut buffers = Vec::new(); - // Start threads + // Start generating threads for i in char_list.chars() { let counter = Arc::new(RwLock::new(0u128)); let buffer = Arc::new(RwLock::new(Vec::new())); @@ -47,18 +62,18 @@ fn start(char_list: &str, output_len: usize) { tasks.push(worker); } - // Just to report stats + // A thread just to report stats let timer = thread::spawn(move || { let mut out = stderr(); while unsafe { !IS_DONE } { - let mut elapsed = start.elapsed().as_secs() as u128; + let mut seconds_elapsed = start.elapsed().as_secs() as u128; // prevent div by zero err - if elapsed == 0 { elapsed = 1; } + if seconds_elapsed == 0 { seconds_elapsed = 1; } - let mut sofar =0; - for ele in &counters { - sofar += *ele.read().unwrap(); + let mut combinations =0; + for i in &counters { + combinations += *i.read().unwrap(); }; // This doesn't free the memory fast enough! // FIXME @@ -69,15 +84,26 @@ fn start(char_list: &str, output_len: usize) { } // prevent memory footprint from getting too big i.write().unwrap().clear(); - println!("clear") } - // output P/s + let time_remaining = { + let seconds_remaining = (unsafe {TOTAL as u128} - combinations) * seconds_elapsed; + // TODO hours in inacuate? + let hours = (seconds_remaining / 60) / 60; + let minutes = (seconds_remaining / 60) % 60; + let seconds = seconds_remaining % 60; + + format!("{hours}h:{minutes}m:{seconds}s") + }; + + // output stats let _ = out.write( - format!("\rCalculated {}, at {}p/s, {:.5}%", - sofar.to_formatted_string(&Locale::en_NA), - (sofar / elapsed).to_formatted_string(&Locale::en_NA), - sofar as f64 / unsafe { TOTAL as f64 } * 100f64, + format!("\rCalculated {}, at {}p/s, {:.5}%, elapsed: {}s {}", + combinations.to_formatted_string(&Locale::en_NA), + (combinations / seconds_elapsed).to_formatted_string(&Locale::en_NA), + combinations as f64 / unsafe { TOTAL as f64 } * 100f64, + seconds_elapsed, + time_remaining ).as_bytes()); let _ = out.flush(); sleep(Duration::from_secs(CHECK_IN_DELAY)); @@ -89,21 +115,26 @@ fn start(char_list: &str, output_len: usize) { unsafe { IS_DONE = true; } timer.join().unwrap(); - eprintln!("\r{}ms to calculate the permutations (p) for {} chars (n) across {} slots (r).", start.elapsed().as_millis().to_formatted_string(&Locale::en_NA), char_list.len(), output_len); + eprintln!("\r{}ms to calculate the {} permutations (p) for {} chars (n) across {} slots (r).", + start.elapsed().as_millis().to_formatted_string(&Locale::en_NA), + unsafe {TOTAL}.to_formatted_string(&Locale::en_NA), + char_list.len(), + output_len); } fn main() { + let charset = Charset::AllAlpha.as_str(); // All the status messages are printed on stderr // This way you can pipe the actual permutations into // a file whilst still getting debug messages. - let t = (ALL_CHARS_95.len() as u128).pow(LEN.try_into().unwrap()); + let t = (charset.len() as u128).pow(LEN.try_into().unwrap()); unsafe { TOTAL = t as usize }; eprintln!( - "Calculating {} combinations...\n(Number may be truncated if too large)\n", + "\nCalculating {} combinations...\n(Number may be truncated if too large)\n", unsafe { TOTAL }.to_formatted_string(&Locale::en_NA) ); - start(ALL_CHARS_95, LEN); + start(&charset, LEN); } /// Recursively create and print every combination of