Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions assignments/crypto_mining/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions assignments/crypto_mining/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "crypto_mining"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"
93 changes: 93 additions & 0 deletions assignments/crypto_mining/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::io;
use rand::Rng;
use rand::distributions::WeightedIndex;
use rand::distributions::Distribution;
use std::collections::HashMap;

const BILLION: u64 = 1_000_000_000;

struct Result {
size: u8,
min: u64,
max: u64,
avg: f64,
}

fn main() {
for size in 1..100 { // fiecare lungime
println!("Size: {}", size);
let mut iter_result = Result {
size: size,
min: 0,
max: 0,
avg: 0.0,
};

for _ in 0..100 { // fiecare iteratie
let n: usize = size.into();
let mut rng = rand::thread_rng();

let mut array:Vec<u64> = vec![0; n];
// let mut weights:Vec<u64> = vec![0; (BILLION / 100) as usize];
// let mut weights:Vec<u64, u8> = vec![(0, 0); 1];
let mut weight_map = HashMap::new();

for i in 0..n {
array[i] = rng.gen_range(0..BILLION);
if weight_map.contains_key(&(array[i] / 100)) {
*weight_map.get_mut(&(array[i] / 100)).unwrap() += 1;
} else {
weight_map.insert(array[i] / 100, 1);
}
}

let mut keys:Vec<u64> = weight_map.clone().into_keys().collect();
let mut weights:Vec<u64> = weight_map.into_values().collect();

// println!("{:?} \n {:?}", keys, weights);


// println!("Weights: {dist:?}");
// println!("Array: {array:?}");

let mut counter = 0;

loop {
let mut dist = WeightedIndex::new(&weights).unwrap();
counter += 1;
let random_index = dist.sample(&mut rng);
// println!("{random_index}");
let mut num: u64 = (keys[random_index] * 100 + rng.gen_range(0..100)).try_into().unwrap();

// small hack -> weighted gen pt num / 100 + random gen 0-100

if array.contains(&num) {
let index = array.iter().position(|&x| x == num).unwrap();
array.remove(index);
// println!("Removed: {num} on iter {counter}, array is now {array:?}");
if array.is_empty() {
break;
}
weights[random_index] -= 1;
if weights[random_index] == 0 {
weights.remove(random_index);
keys.remove(random_index);
}
}
}

if counter < iter_result.min || iter_result.min == 0 {
iter_result.min = counter;
}

if counter > iter_result.max {
iter_result.max = counter;
}

iter_result.avg += counter as f64;

// break;
}
println!("Min: {}, Max: {}, Avg: {}", iter_result.min, iter_result.max, iter_result.avg / 100.0);
}
}
1 change: 1 addition & 0 deletions assignments/crypto_mining/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc_fingerprint":17643547840083297108,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.84.1 (e71f9a9a9 2025-01-27)\nbinary: rustc\ncommit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58\ncommit-date: 2025-01-27\nhost: x86_64-unknown-linux-gnu\nrelease: 1.84.1\nLLVM version: 19.1.5\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
3 changes: 3 additions & 0 deletions assignments/crypto_mining/target/CACHEDIR.TAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12714492b9401594
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":13207435774680941178,"features":"[]","declared_features":"[\"default\", \"i128\", \"std\"]","target":16903832911151110546,"profile":12206360443249279867,"path":2917980152381096082,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/byteorder-3f9b0df696b109da/dep-lib-byteorder","checksum":false}}],"rustflags":[],"metadata":5398730104718078656,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2590cf500e66af32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":13207435774680941178,"features":"[]","declared_features":"[\"compiler_builtins\", \"core\", \"rustc-dep-of-std\"]","target":11601024444410784892,"profile":12206360443249279867,"path":1175956265451947015,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-bbb8a7c192072622/dep-lib-cfg_if","checksum":false}}],"rustflags":[],"metadata":8462187951337715540,"config":2202906307356721367,"compile_kind":0}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":13207435774680941178,"features":"[]","declared_features":"[]","target":8268801525592157822,"profile":11597332650809196192,"path":10602529704205407992,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/crypto_mining-5688bd1f2e93559c/dep-bin-crypto_mining","checksum":false}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Loading