Parse input from stdin, improve output

This commit is contained in:
Daniel Flanagan 2024-05-03 13:49:33 -05:00
parent ae190c27b9
commit 70df5770fb
3 changed files with 109 additions and 32 deletions

59
Cargo.lock generated
View file

@ -2,12 +2,49 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "anyhow"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
[[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 = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
[[package]]
name = "either"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.14" version = "0.2.14"
@ -29,7 +66,9 @@ checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
name = "lotto" name = "lotto"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"rand", "rand",
"rayon",
] ]
[[package]] [[package]]
@ -68,6 +107,26 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View file

@ -6,4 +6,6 @@ edition = "2021"
# 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]
anyhow = "1.0.82"
rand = "0.8.5" rand = "0.8.5"
rayon = "1.10.0"

View file

@ -1,34 +1,37 @@
use rand::Rng; use anyhow::anyhow;
use std::time::Instant; use std::io::BufRead;
use std::{
io::{stdin, BufReader},
time::Instant,
};
type Ticket = [u8; 5]; type Ticket = [u8; 5];
const NUM_ENTRIES: usize = 10_000_000; const NUM_ENTRIES: usize = 10_000_000;
// println!("char: {}", rand::random::<char>()); fn ticket_from_str(s: &str) -> Result<Ticket, anyhow::Error> {
let mut nums = s.split(" ").take(5).map(|s| s.parse::<u8>()).into_iter();
fn gen_num() -> u8 { let num1 = nums.next();
rand::thread_rng().gen_range(0..90) let num2 = nums.next();
let num3 = nums.next();
let num4 = nums.next();
let num5 = nums.next();
if num1.is_none() || num2.is_none() || num3.is_none() || num4.is_none() || num5.is_none() {
return Err(anyhow!("invalid lotto number"));
}
let r = [
num1.unwrap()?,
num2.unwrap()?,
num3.unwrap()?,
num4.unwrap()?,
num5.unwrap()?,
];
if r[0] > 90 || r[1] > 90 || r[2] > 90 || r[3] > 90 || r[4] > 90 {
return Err(anyhow!("lotto number too high"));
} }
fn gen_ticket() -> Ticket { Ok(r)
let num1 = gen_num();
let mut num2: u8 = gen_num();
while num2 == num1 {
num2 = gen_num()
}
let mut num3: u8 = gen_num();
while num3 == num1 || num3 == num2 {
num3 = gen_num()
}
let mut num4: u8 = gen_num();
while num4 == num1 || num4 == num2 || num4 == num3 {
num4 = gen_num()
}
let mut num5: u8 = gen_num();
while num5 == num1 || num5 == num2 || num5 == num3 || num5 == num4 {
num5 = gen_num()
}
[num1, num2, num3, num4, num5]
} }
fn num_matches(t1: Ticket, t2: Ticket) -> usize { fn num_matches(t1: Ticket, t2: Ticket) -> usize {
@ -43,17 +46,27 @@ fn num_matches(t1: Ticket, t2: Ticket) -> usize {
result result
} }
fn main() { fn main() -> Result<(), anyhow::Error> {
println!("Generating entries"); println!("Parsing entries");
let mut entries: Vec<Ticket> = Vec::with_capacity(NUM_ENTRIES);
for _ in 0..NUM_ENTRIES { let reader = BufReader::new(stdin());
entries.push(gen_ticket()); let mut entries = Vec::with_capacity(NUM_ENTRIES);
for line in reader.lines() {
match line {
Ok(l) => match ticket_from_str(&l) {
Ok(n) => entries.push(n),
Err(_) => {} // noop
},
Err(_) => break,
} }
let winning = gen_ticket(); }
println!("{} entries parsed from stdin", entries.len());
let winning = [68, 81, 40, 34, 85];
let start = Instant::now(); let start = Instant::now();
println!("Counting winners... (timer started)");
println!("Counting winners...");
let mut winners: [usize; 6] = [0, 0, 0, 0, 0, 0]; let mut winners: [usize; 6] = [0, 0, 0, 0, 0, 0];
for n in entries { for n in entries {
@ -61,6 +74,7 @@ fn main() {
winners[num_winning] += 1 winners[num_winning] += 1
} }
println!("Done counting winners (timer stopped)");
let duration = start.elapsed(); let duration = start.elapsed();
println!( println!(
@ -68,4 +82,6 @@ fn main() {
winners, winners,
duration.subsec_millis() duration.subsec_millis()
); );
Ok(())
} }