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
50 changes: 37 additions & 13 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ edition = "2018"

[dependencies]
lazy_static = "1.4.0"
bigint = "4.4.3"
uint = "0.8.5"
rocket = "0.4.5"
rocket_contrib = "0.4.5"
rocket_cors = "0.5.1"
serde = { version = "1.0.116", features = ["derive"] }
rayon = "1.1"
itertools = "0.9.0"
44 changes: 25 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ extern crate rocket;
#[macro_use]
extern crate lazy_static;

use bigint::U512;
use uint::construct_uint;
construct_uint! {
pub struct U512(8);
}
use std::ops::Div;
use std::ops::Rem;
use itertools::iproduct;

use serde::{Serialize, Deserialize};
use rocket_contrib::json::Json;
Expand Down Expand Up @@ -257,15 +263,14 @@ impl PrimeElem {
fn plus(&self, rhs: &PrimeElem) -> PrimeElem {
let (sum, overflowed) = self.x.overflowing_add(rhs.x);
assert!(!overflowed);
let (res, overflowed) = sum.overflowing_rem(*P);
assert!(!overflowed);
let res = sum.rem(*P);
PrimeElem { x: res }
}

fn times(&self, rhs: &PrimeElem) -> PrimeElem {
let (prod, overflowed) = self.x.overflowing_mul(rhs.x);
assert!(!overflowed);
let (res, overflowed) = prod.overflowing_rem(*P);
let res = prod.rem(*P);
assert!(!overflowed);
PrimeElem { x: res }
}
Expand Down Expand Up @@ -380,22 +385,23 @@ fn mine(task: Json<Task>) -> Json<Response> {
let y = task.chunkFootprint.bottomLeft.y;
let size = task.chunkFootprint.sideLength;

let (threshold, overflowed) = P.overflowing_div(U512::from(task.planetRarity));
assert!(!overflowed);

let planets = (x..(x + size)).into_par_iter().map(|xi| {
let mut planets = Vec::new();
for yi in y..(y + size) {
let hash = MimcState::sponge(vec![xi, yi], 1, 220)[0].x;
if hash < threshold {
planets.push(Planet {
coords: Coords { x: xi, y: yi },
hash: hash.to_string(),
});
}
let threshold = P.div(U512::from(task.planetRarity));

let planets = iproduct!(x..(x + size), y..(y + size))
.par_bridge()
.filter_map(|(xi, yi)| {
let hash = MimcState::sponge(vec![xi, yi], 1, 220)[0].x;
if hash < threshold {
Some(Planet {
coords: Coords { x: xi, y: yi },
hash: hash.to_string(),
})
} else {
None
}
planets
}).flatten().collect::<Vec<_>>();
})
.collect::<Vec<Planet>>();


Json(Response {
chunkFootprint: task.chunkFootprint.clone(),
Expand Down