Compare commits

..

No commits in common. "0a9debfd3f0862f0b5b684e5ac7882f69d196f84" and "aa2a5f6108093dfb622374d4eec91ed579db2000" have entirely different histories.

4 changed files with 17 additions and 354 deletions

View File

@ -1,12 +0,0 @@
image: "rust:latest"
stages:
- build
build-job:
stage: build
artifacts:
paths:
- target/release/skyums-protocol
script:
- cargo build --release

255
Cargo.lock generated
View File

@ -2,53 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.7" version = "0.2.7"
@ -60,36 +19,6 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.126" version = "0.2.126"
@ -102,48 +31,6 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.8.5" version = "0.8.5"
@ -174,157 +61,15 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "ryu"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]]
name = "serde"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "skyums-protocol" name = "skyums-protocol"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"rand", "rand",
"serde",
"serde_json",
"structopt",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
[[package]]
name = "unicode-segmentation"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -2,16 +2,8 @@
name = "skyums-protocol" name = "skyums-protocol"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
authors = [ "Nicolás A. Ortega Froysa" ]
description = "An implementation of Skyum's Protocol."
readme = "./README.md"
repository = "https://gitlab.com/naortega/skyums-protocol"
license = "Zlib"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
structopt = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -24,116 +24,54 @@
extern crate rand; extern crate rand;
use structopt::StructOpt; fn main() {
use serde::{Serialize,Deserialize}; let size = 100;
use serde_json::json;
// define the commandline parameters // initialize the boxes with randomly assigned numbers
#[derive(StructOpt)]
#[structopt(about = "An implementation of the Skyum's Protocol.")]
struct Opt
{
#[structopt(short, long, default_value="100", help = "The number of inmates in the problem")]
size:u32,
#[structopt(long, help = "Print to stdout the JSON for the results")]
json:bool,
}
// define the prisoner data
#[derive(Serialize, Deserialize)]
struct Prisoner
{
// prisoner id number
id:u32,
// if the prisoner found his number
found:bool,
// number of attempt he found it in
attempts:u32,
}
fn main()
{
// read the commandline parameters
let opts = Opt::from_args();
// set number of prisoners in experiment
let size = opts.size;
// initialize the boxes with capacity for `size` number of boxes
let mut boxes:Vec<u32> = Vec::with_capacity(size as usize); let mut boxes:Vec<u32> = Vec::with_capacity(size as usize);
{ {
// create vector of numbers to select randomly from
let mut nums:Vec<u32> = (0..size).collect(); let mut nums:Vec<u32> = (0..size).collect();
while nums.len() > 0 while nums.len() > 0
{ {
// get a random index for nums
let rand_i:usize = rand::random::<usize>() % nums.len(); let rand_i:usize = rand::random::<usize>() % nums.len();
// add that element to the boxes vector
boxes.push(nums[rand_i]); boxes.push(nums[rand_i]);
// remove the entry from the nums vector
nums.remove(rand_i); nums.remove(rand_i);
} }
} }
// create the vector of prisoners // have prisoners find their number
let mut prisoners:Vec<Prisoner> = Vec::with_capacity(size as usize); let mut all_found = true;
// for every prisoner in the jail
for i in 0..size for i in 0..size
{ {
// create the prisoner for index `i`
let mut prisoner_i = Prisoner {
// assign the id number
id: i,
// assume the prisoner does not find his box
found: false,
// set number of attempts to 0
attempts: 0,
};
// the next (first) box the prisoner is to open is the one with his number (`i`)
let mut next_box:usize = i as usize; let mut next_box:usize = i as usize;
// while the prisoner still has attempts left AND he hasn't found his number let mut found = false;
while prisoner_i.attempts < size/2 && !prisoner_i.found for _j in 0..(size/2)
{ {
// count attempt
prisoner_i.attempts += 1;
// if the number inside the box is his number
if boxes[next_box] == i if boxes[next_box] == i
{ {
// assign `found` to true found = true;
prisoner_i.found = true; break;
} }
else else
{ {
// if not found set the next_box to the number contained in the current box
next_box = boxes[next_box] as usize; next_box = boxes[next_box] as usize;
} }
} }
if !found
// add prisoner to list of prisoners in experiment {
prisoners.push(prisoner_i); all_found = false;
}
} }
if !opts.json // print result
if all_found
{ {
// if any prisoner has not found their number println!("All prisoners found their numbers!");
if prisoners.iter().any(|i| !i.found)
{
println!("failure");
} }
else else
{ {
println!("success"); println!("Some prisoners didn't find their numbers!");
}
}
else
{
// export data to JSON format
let out_json = json!({
"prisoners": prisoners
});
println!("{}", out_json);
} }
} }