Compare commits

..

2 commits

Author SHA1 Message Date
9983278598 Bruteforce garbage 2024-05-24 15:10:35 -05:00
7008ffee68 Window impl 2024-05-17 16:37:51 -05:00
5 changed files with 174 additions and 264 deletions

160
Cargo.lock generated
View file

@ -2,166 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "getrandom"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]]
name = "lotto"
version = "0.1.0"
dependencies = [
"anyhow",
"memory-stats",
"rand",
"rayon",
]
[[package]]
name = "memory-stats"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34f79cf9964c5c9545493acda1263f1912f8d2c56c8a2ffee2606cb960acaacc"
dependencies = [
"libc",
"winapi",
]
[[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 = "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]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
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

@ -3,10 +3,14 @@ name = "lotto"
version = "0.1.0"
edition = "2021"
[lib]
name = "lotto"
path = "src/lib.rs"
[[bin]]
name = "lotto"
path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.82"
memory-stats = "1.1.0"
rand = "0.8.5"
rayon = "1.10.0"

54
src/lib.rs Normal file
View file

@ -0,0 +1,54 @@
use std::collections::BTreeMap;
#[derive(Debug)]
pub struct LRUCache {
keys: BTreeMap<i32, i32>,
keys_used: BTreeMap<i32, u64>,
counter: u64,
capacity: i32,
}
impl LRUCache {
fn new(capacity: i32) -> Self {
println!("new {capacity}");
Self {
keys: BTreeMap::new(),
keys_used: BTreeMap::new(),
counter: 0,
capacity,
}
}
fn get(&mut self, key: i32) -> i32 {
println!("getting {key}");
let result = self.keys.get(&key).unwrap_or(&-1);
if result != &-1 {
*self.keys_used.get_mut(&key).unwrap() = self.counter;
self.counter += 1;
}
*result
}
fn put(&mut self, key: i32, value: i32) {
println!("inserting {key} {value}");
self.keys.insert(key, value);
self.keys_used.insert(key, self.counter);
if self.keys_used.len() >= self.capacity.try_into().unwrap() {
let mut lowest = u64::MAX;
let mut lowest_key = -1;
for (k, v) in self.keys_used.iter() {
if v < &lowest {
lowest = *v;
lowest_key = *k;
}
}
println!("evicting {lowest_key}");
self.keys.remove(&lowest_key);
self.keys_used.remove(&lowest_key);
}
self.counter += 1;
}
}
#[cfg(test)]
mod test {}

102
src/lotto.rs Normal file
View file

@ -0,0 +1,102 @@
use anyhow::anyhow;
use std::fs::File;
use std::io::BufRead;
use std::{io::BufReader, time::Instant};
type Ticket = [u8; 5];
const NUM_ENTRIES: usize = 10_000_000;
fn ticket_from_str(s: &str) -> Result<Ticket, anyhow::Error> {
let mut nums = s.split(" ").take(5).map(|s| s.parse::<u8>()).into_iter();
let num1 = nums.next();
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] < 1
|| r[1] < 1
|| r[2] < 1
|| r[3] < 1
|| r[4] < 1
|| r[0] > 90
|| r[1] > 90
|| r[2] > 90
|| r[3] > 90
|| r[4] > 90
{
return Err(anyhow!("lotto number out of bounds"));
}
Ok(r)
}
fn num_matches(t1: Ticket, t2: Ticket) -> usize {
let mut result = 0;
for n1 in t1 {
for n2 in t2 {
if n1 == n2 {
result += 1;
}
}
}
result
}
fn main() -> Result<(), anyhow::Error> {
let start = Instant::now();
println!("Parsing entries from stdin...");
let f = File::open("/mytmpfs/10m-v2.txt")?;
let reader = BufReader::new(f);
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 duration = start.elapsed();
println!(
"{} entries parsed from stdin in {}ms",
entries.len(),
duration.as_millis()
);
let winning = [68, 81, 40, 34, 85];
let start = Instant::now();
println!("Counting winners... (timer started)");
let mut winners: [usize; 6] = [0, 0, 0, 0, 0, 0];
for n in entries {
let num_winning = num_matches(n, winning);
winners[num_winning] += 1
}
println!("Done counting winners (timer stopped)");
let duration = start.elapsed();
println!(
"Winners: {:?}\nTime Elapsed: {}ms",
winners.into_iter().skip(2).collect::<Vec<usize>>(),
duration.subsec_millis(),
);
Ok(())
}

File diff suppressed because one or more lines are too long