Merge remote-tracking branch 'origin/main'

This commit is contained in:
Daniel Flanagan 2024-09-13 16:31:31 -05:00
commit 1ca5f7b5b5
8 changed files with 124 additions and 1 deletions

1
nim-solution/.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
nim-solution/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/.direnv
/lotto

27
nim-solution/flake.lock Normal file
View file

@ -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
}

31
nim-solution/flake.nix Normal file
View file

@ -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;
});
};
}

13
nim-solution/lotto.nimble Normal file
View file

@ -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"

7
nim-solution/readme.md Normal file
View file

@ -0,0 +1,7 @@
# lotto
```bash
$ nimble run -d:release --passC:"-march=native"
Parse time taken: 4.723442036 seconds
Time taken: 0.07040529100000015 seconds
```

View file

@ -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"

View file

@ -1,5 +1,8 @@
# lotto # lotto
```bash ```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
``` ```