From 5f6842ba9e66389f3f867654d3fe4056fa838fc2 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Thu, 1 Dec 2022 12:10:44 +0000 Subject: [PATCH] Add day 1 --- .envrc | 1 + .gitignore | 3 + Cargo.lock | 7 ++ Cargo.toml | 6 ++ flake.lock | 176 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 50 ++++++++++++++ src/bin/day1.rs | 57 ++++++++++++++++ src/lib.rs | 1 + src/main.rs | 3 + 9 files changed, 304 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/bin/day1.rs create mode 100644 src/lib.rs 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..7293b54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.direnv +/target +/input diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d37200f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc2022" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c97a6c7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc2022" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..249ba45 --- /dev/null +++ b/flake.lock @@ -0,0 +1,176 @@ +{ + "nodes": { + "devshell": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1667210711, + "narHash": "sha256-IoErjXZAkzYWHEpQqwu/DeRNJGFdR7X2OGbkhMqMrpw=", + "owner": "numtide", + "repo": "devshell", + "rev": "96a9dd12b8a447840cc246e17a47b81a4268bba7", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1642700792, + "narHash": "sha256-XqHrk7hFb+zBvRg6Ghl+AZDq03ov6OshJLiSWOoX5es=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "846b2ae0fc4cc943637d3d1def4454213e203cba", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1662220400, + "narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=", + "owner": "nix-community", + "repo": "naersk", + "rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1643381941, + "narHash": "sha256-pHTwvnN4tTsEKkWlXQ8JMY423epos8wUOhthpwJjtpc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5efc8ca954272c4376ac929f4c5ffefcc20551d5", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1669809720, + "narHash": "sha256-RMT77f6CPOYtLLQ2esj+EJ1BPVWxf4RDidjrSvA5OhI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "227de2b3bbec142f912c09d5e8a1b4e778aa54fb", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1669809720, + "narHash": "sha256-RMT77f6CPOYtLLQ2esj+EJ1BPVWxf4RDidjrSvA5OhI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "227de2b3bbec142f912c09d5e8a1b4e778aa54fb", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_4": { + "locked": { + "lastModified": 1665296151, + "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devshell": "devshell", + "naersk": "naersk", + "nixpkgs": "nixpkgs_3", + "rust-overlay": "rust-overlay", + "utils": "utils" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_4" + }, + "locked": { + "lastModified": 1669862269, + "narHash": "sha256-/ar+cbAKAxd2Ng9b7EhrIMz9CP353RbmLecvyOidyUM=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "bfdf688742cf984c4837dbbe1c6cbca550365613", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "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" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4ae1d6c --- /dev/null +++ b/flake.nix @@ -0,0 +1,50 @@ +{ + description = "Another cool rust disaster from samw."; + + inputs = { + utils.url = "github:numtide/flake-utils"; + devshell.url = "github:numtide/devshell"; + naersk.url = "github:nix-community/naersk"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + outputs = { + self, + nixpkgs, + utils, + naersk, + devshell, + rust-overlay, + }: + utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [(import rust-overlay)]; + }; + rust = pkgs.rust-bin.stable.latest.default; + # Override naersk to use our chosen rust version from rust-overlay + naersk-lib = naersk.lib.${system}.override { + cargo = rust; + rustc = rust; + }; + in rec { + packages.default = naersk-lib.buildPackage { + pname = "cool-rust-disaster"; + root = ./.; + }; + + apps.default = utils.lib.mkApp {drv = packages.default;}; + + # Provide a dev env with rust and rls + devShells.default = let + pkgs = import nixpkgs { + inherit system; + overlays = [devshell.overlay]; + }; + in + pkgs.devshell.mkShell { + packages = with pkgs; [rust rust-analyzer]; + }; + formatter = pkgs.alejandra; + }); +} diff --git a/src/bin/day1.rs b/src/bin/day1.rs new file mode 100644 index 0000000..01a42aa --- /dev/null +++ b/src/bin/day1.rs @@ -0,0 +1,57 @@ +use aoc2022::StdError; +use std::fs::File; +use std::io::{BufRead, BufReader}; + +struct BlanklineSplitter { + iter: T, +} + +impl> BlanklineSplitter { + fn new(iter: T) -> Self { + BlanklineSplitter { iter } + } +} + +impl> Iterator for BlanklineSplitter { + type Item = Vec; + fn next(&mut self) -> Option { + let mut acc = vec![]; + loop { + match self.iter.next().as_deref() { + Some("") => { + // Skip over multiple empty lines + if !acc.is_empty() { + break; + } + } + Some(l) => acc.push(l.into()), + None => break, + } + } + if acc.is_empty() { + return None; + } else { + return Some(acc); + } + } +} + +fn main() -> StdError<()> { + let lines = BufReader::new(File::open("input/day1.txt")?) + .lines() + .filter_map(Result::ok); + let mut sums: Vec<_> = BlanklineSplitter::new(lines) + .map(|e| { + e.iter() + .map(|l| l.parse::()) + .filter_map(Result::ok) + .sum::() + }) + .collect(); + sums.sort_by(|a, b| b.cmp(a)); + let max = sums.iter().max().unwrap(); + println!("Part 1: {}", max); + let top3 = sums[0..3].iter().sum::(); + println!("Part 2: {}", top3); + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..71e8ff9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub type StdError = Result>; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}