Add day 1

This commit is contained in:
Sam W 2022-12-01 12:10:44 +00:00
commit 5f6842ba9e
9 changed files with 304 additions and 0 deletions

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View File

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

7
Cargo.lock generated Normal file
View File

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

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "aoc2022"
version = "0.1.0"
edition = "2021"
[dependencies]

176
flake.lock Normal file
View File

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

50
flake.nix Normal file
View File

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

57
src/bin/day1.rs Normal file
View File

@ -0,0 +1,57 @@
use aoc2022::StdError;
use std::fs::File;
use std::io::{BufRead, BufReader};
struct BlanklineSplitter<T: Iterator> {
iter: T,
}
impl<T: Iterator<Item = String>> BlanklineSplitter<T> {
fn new(iter: T) -> Self {
BlanklineSplitter { iter }
}
}
impl<T: Iterator<Item = String>> Iterator for BlanklineSplitter<T> {
type Item = Vec<String>;
fn next(&mut self) -> Option<Self::Item> {
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::<u32>())
.filter_map(Result::ok)
.sum::<u32>()
})
.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::<u32>();
println!("Part 2: {}", top3);
Ok(())
}

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub type StdError<T> = Result<T, Box<dyn std::error::Error>>;

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}