Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
1ca5f7b5b5
1
nim-solution/.envrc
Normal file
1
nim-solution/.envrc
Normal file
|
@ -0,0 +1 @@
|
|||
use flake
|
3
nim-solution/.gitignore
vendored
Normal file
3
nim-solution/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
/.direnv
|
||||
/lotto
|
27
nim-solution/flake.lock
Normal file
27
nim-solution/flake.lock
Normal 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
31
nim-solution/flake.nix
Normal 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
13
nim-solution/lotto.nimble
Normal 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
7
nim-solution/readme.md
Normal 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
|
||||
```
|
38
nim-solution/src/lotto.nim
Normal file
38
nim-solution/src/lotto.nim
Normal 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"
|
Loading…
Reference in a new issue