diff --git a/nim-solution/.envrc b/nim-solution/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/nim-solution/.envrc @@ -0,0 +1 @@ +use flake diff --git a/nim-solution/.gitignore b/nim-solution/.gitignore new file mode 100644 index 0000000..ed0f309 --- /dev/null +++ b/nim-solution/.gitignore @@ -0,0 +1,3 @@ +/target +/.direnv +/lotto diff --git a/nim-solution/flake.lock b/nim-solution/flake.lock new file mode 100644 index 0000000..5913a4e --- /dev/null +++ b/nim-solution/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/nim-solution/flake.nix b/nim-solution/flake.nix new file mode 100644 index 0000000..daf4697 --- /dev/null +++ b/nim-solution/flake.nix @@ -0,0 +1,31 @@ +{ + 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 { + nim-dev = pkgs.mkShell { + buildInputs = with pkgs; [ + nim + nimble + nimlangserver + ]; + }; + + default = outputs.devShells.${system}.nim-dev; + }); + }; +} diff --git a/nim-solution/lotto.nimble b/nim-solution/lotto.nimble new file mode 100644 index 0000000..fdce34f --- /dev/null +++ b/nim-solution/lotto.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "Daniel Flanagan" +description = "Lotto winner finder" +license = "Proprietary" +srcDir = "src" +bin = @["lotto"] + + +# Dependencies + +requires "nim >= 2.0.2" diff --git a/nim-solution/readme.md b/nim-solution/readme.md new file mode 100644 index 0000000..74df185 --- /dev/null +++ b/nim-solution/readme.md @@ -0,0 +1,7 @@ +# lotto + +```bash +$ nimble run -d:release --passC:"-march=native" +Parse time taken: 4.723442036 seconds +Time taken: 0.07040529100000015 seconds +``` diff --git a/nim-solution/src/lotto.nim b/nim-solution/src/lotto.nim new file mode 100644 index 0000000..94f68c3 --- /dev/null +++ b/nim-solution/src/lotto.nim @@ -0,0 +1,38 @@ +import std/[times, strutils, sequtils, options] + +type Ticket = array[5, uint8] + +proc to_ticket(s: seq[int]): Ticket = + for i, n in result.mpairs: + n = s[i].uint8 + +proc parse_ticket(l: string): Option[Ticket] = + let candidate = l.split(' ', 5).map(parseInt).filterIt(it <= 90 and it > 0) + if candidate.len < 5: + return Ticket.none + candidate.to_ticket().some() + +proc num_matches(t1: Ticket, t2: Ticket): uint = + for n1 in t1: + for n2 in t2: + if n1 == n2: + result.inc + +let startTime = cpuTime() +echo "Starting..." +const NUM_ENTRIES = 10_000_000 +var entries = newSeqOfCap[Ticket] NUM_ENTRIES + +for l in "/mytmpfs/10m-v2.txt".lines: + let t = l.parse_ticket + if t.isSome: + entries.add t.get + +echo "Parse time taken: ", (cpuTime() - startTime), " seconds" + +let winning: Ticket = [68, 81, 40, 34, 85] +let countTime = cpuTime() +var winners: array[6, uint] = [0, 0, 0, 0, 0, 0] +for n in entries: + winners[n.num_matches(winning)].inc +echo winners, "\nTime taken: ", (cpuTime() - countTime), " seconds" diff --git a/readme.md b/readme.md index 1835a52..706fa5e 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,8 @@ # lotto ```bash -cargo run --release +$ RUSTFLAGS='-C opt-level=3 -C target-cpu=native' cargo run --release +... +9999997 entries parsed from stdin in 792ms +Time Elapsed: 29ms ```