it works, comitting now before i destroy it all
This commit is contained in:
parent
6ea2c887d6
commit
ce8ef709d6
3 changed files with 160 additions and 0 deletions
23
Cargo.toml
Normal file
23
Cargo.toml
Normal file
|
@ -0,0 +1,23 @@
|
|||
[package]
|
||||
name = "graveler-softlockpicking-years-wasted-calculator"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[profile.release]
|
||||
opt-level = 3
|
||||
codegen-units = 1
|
||||
panic = "abort"
|
||||
|
||||
[profile.release.package."*"]
|
||||
opt-level = 3
|
||||
codegen-units = 1
|
||||
#panic = "abort"
|
||||
|
||||
[dependencies]
|
||||
rand = { version = "0.8.5", optional = true }
|
||||
num_cpus = { version = "1.16.0", optional = true }
|
||||
|
||||
[features]
|
||||
default = ["rust_random", "auto_thread_count"]
|
||||
rust_random = ["dep:rand"]
|
||||
auto_thread_count = ["dep:num_cpus"]
|
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
|
@ -0,0 +1 @@
|
|||
hard_tabs=true
|
136
src/main.rs
Normal file
136
src/main.rs
Normal file
|
@ -0,0 +1,136 @@
|
|||
use std::{
|
||||
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
//const ITER_COUNT: usize = 1_000_000_000;
|
||||
const ITER_COUNT: usize = 1_000_000;
|
||||
|
||||
static THREAD_STOP: AtomicBool = AtomicBool::new(false);
|
||||
static ROLL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
static HIGH_SCORE: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
fn main() {
|
||||
#[cfg(not(feature = "rust_random"))]
|
||||
{
|
||||
// forgive this mess, i just cant be arsed to make a proper command line interface
|
||||
// in case youre wondering a usize is a non floating point number that can fit inside your cpus max bit size
|
||||
let seed = std::env::args()
|
||||
.collect::<Vec<String>>()
|
||||
.get(1) // we use 1 here because 90% of platforms use the path/program name as the first argument
|
||||
.expect("You must supply a usize argument, for example 75293")
|
||||
.parse::<usize>()
|
||||
.expect("First argument is not a valid usize, for example 926304");
|
||||
SEED.store(seed, Ordering::SeqCst);
|
||||
|
||||
// give it some time to become a tad more "random"
|
||||
for _ in 0..SEED.load(Ordering::SeqCst) {
|
||||
dbg!(seeded_rpower_random());
|
||||
}
|
||||
}
|
||||
|
||||
// get the total thread count we wish to use
|
||||
#[cfg(feature = "auto_thread_count")]
|
||||
let thread_count: usize = num_cpus::get();
|
||||
#[cfg(not(feature = "auto_thread_count"))]
|
||||
let thread_count: usize = 4; // Change me to any number you want to use as the thread count, please note you must not be using the 'auto_thread_count' feature, see README.md for details
|
||||
|
||||
// divide the total iter count among multiple threads
|
||||
let thread_dispatch = {
|
||||
let mut vec = vec![0; thread_count];
|
||||
|
||||
for i in 0..(thread_count) {
|
||||
vec[i] = ITER_COUNT / thread_count;
|
||||
}
|
||||
|
||||
// add the remainder of the division to the last thread as not ever number is cleanly divisible by an unknown thread count
|
||||
vec[thread_count - 1] = vec[thread_count - 1] + (ITER_COUNT % thread_count);
|
||||
vec
|
||||
};
|
||||
|
||||
// for verification purposes
|
||||
//let mut adder = 0;
|
||||
//for i in &thread_dispatch {
|
||||
// adder += i;
|
||||
//}
|
||||
//println!("{adder}");
|
||||
|
||||
let mut thread_join_handles = vec![];
|
||||
|
||||
for i in thread_dispatch {
|
||||
thread_join_handles.push(thread::spawn(move || number_cruncher(i)));
|
||||
}
|
||||
|
||||
loop {
|
||||
println!("{}", HIGH_SCORE.load(Ordering::SeqCst));
|
||||
|
||||
// if all threads are done break the loop
|
||||
{
|
||||
let mut any_thread_running = false;
|
||||
for i in &thread_join_handles {
|
||||
if !i.is_finished() {
|
||||
any_thread_running = true;
|
||||
}
|
||||
}
|
||||
if !any_thread_running {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
|
||||
println!("ROLL_COUNT: {}", ROLL_COUNT.load(Ordering::SeqCst));
|
||||
println!("HIGH_SCORE: {}", HIGH_SCORE.load(Ordering::SeqCst));
|
||||
}
|
||||
|
||||
fn number_cruncher(iter_count: usize) {
|
||||
{
|
||||
for _ in 0..iter_count {
|
||||
if THREAD_STOP.load(Ordering::SeqCst) {
|
||||
break;
|
||||
}
|
||||
ROLL_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||
|
||||
let mut correct_count = 0;
|
||||
|
||||
// turns
|
||||
for _ in 0..231 {
|
||||
// TODO compare this with a random usize % 4, or the classic (SEED^3)%4 trick
|
||||
#[cfg(feature = "rust_random")]
|
||||
let rand = rand::Rng::gen_range(&mut rand::thread_rng(), 0..4);
|
||||
#[cfg(not(feature = "rust_random"))]
|
||||
let rand = seeded_rpower_random() % 4;
|
||||
match rand {
|
||||
0 => {
|
||||
correct_count += 1;
|
||||
}
|
||||
_ => { /* do nothing */ }
|
||||
}
|
||||
|
||||
if correct_count > HIGH_SCORE.load(Ordering::SeqCst) {
|
||||
HIGH_SCORE.swap(correct_count, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
if correct_count > 177 {
|
||||
THREAD_STOP.store(true, Ordering::SeqCst);
|
||||
println!("HIT: {correct_count}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SEED: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[cfg(not(feature = "rust_random"))]
|
||||
fn seeded_rpower_random() -> usize {
|
||||
let mut cseed = SEED.load(Ordering::SeqCst);
|
||||
|
||||
cseed ^= 3;
|
||||
|
||||
SEED.store(cseed, Ordering::SeqCst);
|
||||
|
||||
cseed
|
||||
}
|
Loading…
Add table
Reference in a new issue