From 784cdd62685f2b12d8065e045ac4bd7b9f3d91cd Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Fri, 2 Dec 2022 16:02:56 +1100 Subject: [PATCH] merged nix builder into repo --- .gitignore | 3 +- Cargo.lock | 2 +- Cargo.toml | 2 +- flake.lock | 140 ++++++++++++++++++ flake.nix | 98 ++++++++++++ src/lib.rs | 5 +- src/main.rs | 12 +- src/problem02/mod.rs | 9 -- src/{problem01 => problem_01}/description.txt | 0 src/{problem01 => problem_01}/input.txt | 0 src/{problem01 => problem_01}/mod.rs | 18 +-- src/{problem02 => problem_02}/input.txt | 0 src/problem_02/mod.rs | 9 ++ 13 files changed, 267 insertions(+), 31 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 src/problem02/mod.rs rename src/{problem01 => problem_01}/description.txt (100%) rename src/{problem01 => problem_01}/input.txt (100%) rename src/{problem01 => problem_01}/mod.rs (61%) rename src/{problem02 => problem_02}/input.txt (100%) create mode 100644 src/problem_02/mod.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..4075fb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target +target +result diff --git a/Cargo.lock b/Cargo.lock index 6966fe4..06f16a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -207,7 +207,7 @@ dependencies = [ ] [[package]] -name = "tAoC2022" +name = "taoc2022" version = "0.1.0" dependencies = [ "clap", diff --git a/Cargo.toml b/Cargo.toml index ca3b78f..54d04fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "tAoC2022" +name = "taoc2022" version = "0.1.0" edition = "2021" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e9acb03 --- /dev/null +++ b/flake.lock @@ -0,0 +1,140 @@ +{ + "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1669846878, + "narHash": "sha256-2ks8+eP/MuH07KGUR7zO/cSRPyoYJIPQHDsj+hveN/U=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "b80f8edaa7e5ce5a6babcd61f14dddee77875c2c", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1669605882, + "narHash": "sha256-TiQtL5sUI5rp28S63v+VX25qNjcrc8Xeu+shf3g7Tj4=", + "owner": "ipetkov", + "repo": "crane", + "rev": "24591d5f8cc979f7b243b88a2d39da09976970ad", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1650374568, + "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b4a34015c698c7793d592d66adbab377907a2be8", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1669900182, + "narHash": "sha256-EKxjHxRJnP1w+2nnm8pq4Uqkqa5McnfPXcO8cG8Mxzc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "bcb6dbbe30ce7631e5a0865dff1ab9b63d92977d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1667487142, + "narHash": "sha256-bVuzLs1ZVggJAbJmEDVO9G6p8BH3HRaolK70KXvnWnU=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "cf668f737ac986c0a89e83b6b2e3c5ddbd8cf33b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1fd8b30 --- /dev/null +++ b/flake.nix @@ -0,0 +1,98 @@ +{ + description = "tAoC2022 - Nix Builder"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils.url = "github:numtide/flake-utils"; + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; + + # my rust project + # taoc2022 = { + # url = "git+ssh://gitea@git.p7.co.nz/thornAvery/tAoC2022"; + # flake = false; + # }; + + }; + + outputs = { self, nixpkgs, crane, flake-utils, advisory-db, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + + inherit (pkgs) lib; + + craneLib = crane.lib.${system}; + src = ./.; + + buildInputs = [ + ] ++ lib.optionals pkgs.stdenv.isDarwin [ + pkgs.libiconv + ]; + + cargoArtifacts = craneLib.buildDepsOnly { + inherit src buildInputs; + }; + + my-crate = craneLib.buildPackage { + inherit cargoArtifacts src buildInputs; + }; + in + { + checks = { + inherit my-crate; + + my-crate-clippy = craneLib.cargoClippy { + inherit cargoArtifacts src buildInputs; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }; + + my-crate-doc = craneLib.cargoDoc { + inherit cargoArtifacts src; + }; + + my-crate-fmt = craneLib.cargoFmt { + inherit src; + }; + + my-crate-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + my-crate-nextest = craneLib.cargoNextest { + inherit cargoArtifacts src buildInputs; + partitions = 1; + partitionType = "count"; + }; + } // lib.optionalAttrs (system == "x86_64-linux") { + # cargo-tarpaulin only supports x86_64 systems + my-crate-coverage = craneLib.cargoTarpaulin { + inherit cargoArtifacts src; + }; + }; + + packages.default = my-crate; + + apps.default = flake-utils.lib.mkApp { + drv = my-crate; + }; + + devShells.default = pkgs.mkShell { + inputsFrom = builtins.attrValues self.checks; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + nativeBuildInputs = with pkgs; [ + cargo + rustc + rust-analyzer + ]; + }; + }); +} diff --git a/src/lib.rs b/src/lib.rs index 9a9096e..e62cb92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,2 @@ -#![allow(non_snake_case)] -pub mod problem01; -pub mod problem02; +pub mod problem_01; +pub mod problem_02; diff --git a/src/main.rs b/src/main.rs index 73c6d30..58dc661 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use tAoC2022::problem01; -use tAoC2022::problem02; +use taoc2022::problem_01; +use taoc2022::problem_02; fn main() { println!("== Problem 01 =="); - println!("= a: {}", problem01::solutionA()); - println!("= b: {}", problem01::solutionB()); + println!("= a: {}", problem_01::solution_a()); + println!("= b: {}", problem_01::solution_b()); println!("== Problem 02 =="); - println!("= a: {}", problem02::solutionA()); - println!("= b: {}", problem02::solutionB()); + println!("= a: {}", problem_02::solution_a()); + println!("= b: {}", problem_02::solution_b()); } diff --git a/src/problem02/mod.rs b/src/problem02/mod.rs deleted file mode 100644 index 3b80ede..0000000 --- a/src/problem02/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -static INPUT: &'static str = include_str!("input.txt"); - -pub fn solutionA() -> i32 { - 0 -} - -pub fn solutionB() -> i32 { - 0 -} diff --git a/src/problem01/description.txt b/src/problem_01/description.txt similarity index 100% rename from src/problem01/description.txt rename to src/problem_01/description.txt diff --git a/src/problem01/input.txt b/src/problem_01/input.txt similarity index 100% rename from src/problem01/input.txt rename to src/problem_01/input.txt diff --git a/src/problem01/mod.rs b/src/problem_01/mod.rs similarity index 61% rename from src/problem01/mod.rs rename to src/problem_01/mod.rs index 52826b8..f15aa26 100644 --- a/src/problem01/mod.rs +++ b/src/problem_01/mod.rs @@ -1,10 +1,10 @@ -static INPUT: &'static str = include_str!("input.txt"); +static INPUT: &str = include_str!("input.txt"); fn solution(x: i32) -> i32 { let mut totals = vec![Default::default(); x.try_into().unwrap()]; let mut total = 0; for line in INPUT.lines() { - if line == "" { + if line.is_empty() { add_to_total(&mut totals, total); total = 0; } else { @@ -15,21 +15,19 @@ fn solution(x: i32) -> i32 { return totals.iter().sum(); } -fn add_to_total(totals: &mut Vec, total: i32) { +fn add_to_total(totals: &mut [i32], total: i32) { let mut i = total; for n in totals.iter_mut() { if i > *n { - let tmp = *n; - *n = i; - i = tmp; + std::mem::swap(&mut (*n), &mut i); }; } } -pub fn solutionA() -> i32 { - return solution(1); +pub fn solution_a() -> i32 { + solution(1) } -pub fn solutionB() -> i32 { - return solution(3); +pub fn solution_b() -> i32 { + solution(3) } diff --git a/src/problem02/input.txt b/src/problem_02/input.txt similarity index 100% rename from src/problem02/input.txt rename to src/problem_02/input.txt diff --git a/src/problem_02/mod.rs b/src/problem_02/mod.rs new file mode 100644 index 0000000..8b43c01 --- /dev/null +++ b/src/problem_02/mod.rs @@ -0,0 +1,9 @@ +// static INPUT: &'static str = include_str!("input.txt"); + +pub fn solution_a() -> i32 { + 0 +} + +pub fn solution_b() -> i32 { + 0 +}