Generate larger problems for day01

This commit is contained in:
Malte Tammena 2022-12-01 12:34:19 +01:00
parent 2332f2532d
commit 4a0588b1bf
3 changed files with 97 additions and 1 deletions

68
Cargo.lock generated
View file

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "day01"
version = "0.1.0"
@ -9,6 +15,9 @@ version = "0.1.0"
[[package]]
name = "day01-gen-problem"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "day02"
@ -101,3 +110,62 @@ version = "0.1.0"
[[package]]
name = "day24"
version = "0.1.0"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View file

@ -8,3 +8,4 @@ build = true
app = true
[dependencies]
rand = "0.8.5"

View file

@ -1,3 +1,30 @@
use std::{fs::File, io::BufWriter, io::Write};
use rand::Rng;
const PERCENT_NONE: f32 = 0.1;
const LINE_COUNT: usize = 50_000_000;
const MAX_CALORY: usize = 30_000;
fn main() {
println!("Hello, world!");
let outfile = ::std::env::args()
.skip(1)
.next()
.expect("Output file as first parameter");
let mut outfile = BufWriter::new(File::create(outfile).expect("Can write outfile"));
let mut rng = rand::thread_rng();
(0..LINE_COUNT)
.map(|_| {
if rng.gen::<f32>() < PERCENT_NONE {
None
} else {
Some(rng.gen_range(0..MAX_CALORY))
}
})
.map(|opt| match opt {
Some(cal) => format!("{cal}\n"),
None => String::from("\n"),
})
.for_each(|line| write!(outfile, "{line}").expect("Writing works"));
outfile.flush().expect("Flushing works");
}