From 7b03d63760c4af699592b48c3d047e80ebd8964a Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 3 May 2024 13:06:15 -0500 Subject: [PATCH] Lotto --- .envrc | 1 + .gitignore | 16 ++++++++++++ Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 +++++++ flake.lock | 27 +++++++++++++++++++ flake.nix | 35 +++++++++++++++++++++++++ src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 234 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ac290 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +/target +/.direnv + + +# Added by cargo +# +# already existing elements were commented out + +#/target + + +# Added by cargo +# +# already existing elements were commented out + +#/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..76ef877 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# 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 = "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 = [ + "rand", +] + +[[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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a07e463 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "lotto" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5913a4e --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1714635257, + "narHash": "sha256-4cPymbty65RvF1DWQfc+Bc8B233A1BWxJnNULJKQ1EY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "63c3a29ca82437c87573e4c6919b09a24ea61b0f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..be6b512 --- /dev/null +++ b/flake.nix @@ -0,0 +1,35 @@ +{ + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + outputs = { + self, + nixpkgs, + }: let + inherit (self) outputs; + supportedSystems = [ + "x86_64-linux" + "aarch64-linux" + + "x86_64-darwin" + "aarch64-darwin" + ]; + forEachSupportedSystem = nixpkgs.lib.genAttrs supportedSystems; + in { + devShells = forEachSupportedSystem (system: let + pkgs = import nixpkgs {inherit system;}; + in { + rust-dev = pkgs.mkShell { + buildInputs = with pkgs; [ + cargo + rustc + rustfmt + rustPackages.clippy + rust-analyzer + lldb + pkg-config + ]; + }; + + default = outputs.devShells.${system}.rust-dev; + }); + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..62b00a3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,71 @@ +use rand::Rng; +use std::time::Instant; + +type Ticket = [u8; 5]; +const NUM_ENTRIES: usize = 10_000_000; + +// println!("char: {}", rand::random::()); + +fn gen_num() -> u8 { + rand::thread_rng().gen_range(0..90) +} + +fn gen_ticket() -> Ticket { + 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 { + let mut result = 0; + for n1 in t1 { + for n2 in t2 { + if n1 == n2 { + result += 1; + } + } + } + result +} + +fn main() { + println!("Generating entries"); + let mut entries: Vec = Vec::with_capacity(NUM_ENTRIES); + for _ in 0..NUM_ENTRIES { + entries.push(gen_ticket()); + } + let winning = gen_ticket(); + + let start = Instant::now(); + + println!("Counting winners..."); + 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 + } + + let duration = start.elapsed(); + + println!( + "Winners: {:?}\nTime Elapsed: {}ms", + winners, + duration.subsec_millis() + ); +}