feat: finish 22
This commit is contained in:
8
problems/p22/Cargo.toml
Normal file
8
problems/p22/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "p22"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
common = { path = "../../common/" }
|
||||||
|
rand = { workspace = true }
|
||||||
42
problems/p22/src/main.rs
Normal file
42
problems/p22/src/main.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
use common::MT19937;
|
||||||
|
use rand::{prelude::*, rng};
|
||||||
|
use std::thread::sleep;
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
fn get_sec() -> u32 {
|
||||||
|
let now = SystemTime::now();
|
||||||
|
let duration_since_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards"); // Handle potential errors if system time is earlier than epoch.
|
||||||
|
|
||||||
|
let unix_timestamp_seconds = duration_since_epoch.as_secs();
|
||||||
|
// overflow at 2038
|
||||||
|
unix_timestamp_seconds as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crack(rand_num: u32) -> u32 {
|
||||||
|
let now = get_sec();
|
||||||
|
for i in 5..20 {
|
||||||
|
let mut mt19937 = MT19937::new(now - i);
|
||||||
|
let result = mt19937.extract_number();
|
||||||
|
if rand_num == result {
|
||||||
|
return now - i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut rng = rng();
|
||||||
|
let sleep_time: u64 = rng.random_range(5..20);
|
||||||
|
sleep(Duration::from_secs(sleep_time));
|
||||||
|
let seed = get_sec();
|
||||||
|
let mut mt19937 = MT19937::new(seed);
|
||||||
|
let sleep_time: u64 = rng.random_range(5..20);
|
||||||
|
sleep(Duration::from_secs(sleep_time));
|
||||||
|
let result = mt19937.extract_number();
|
||||||
|
let cracked = crack(result);
|
||||||
|
if cracked == seed {
|
||||||
|
println!("cracked");
|
||||||
|
} else {
|
||||||
|
println!("failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user