commit 7f5c39165683c1d4c4f5eede0908e0f55c191c89 Author: Lucy Date: Mon Dec 2 07:25:29 2024 +0100 feat: day 1 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..937e123 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.cache +target +.direnv +inputs/*/input.txt diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9be5db9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aoc2024" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..186bac2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc2024" +version = "0.1.0" +edition = "2021" + + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..56922a3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,128 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1730504689, + "narHash": "sha256-hgmguH29K2fvs9szpq2r3pz2/8cJd2LPS+b4tfNFCwE=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "506278e768c2a08bec68eb62932193e341f55c90", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1732837521, + "narHash": "sha256-jNRNr49UiuIwaarqijgdTR2qLPifxsVhlJrKzQ8XUIE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "970e93b9f82e2a0f3675757eb0bfc73297cc6370", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1730504152, + "narHash": "sha256-lXvH/vOfb4aGYyvFmZK/HlsNsr/0CVWlwYvo2rxJk3s=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/cc2f28000298e1269cea6612cd06ec9979dd5d7f.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/cc2f28000298e1269cea6612cd06ec9979dd5d7f.tar.gz" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1728538411, + "narHash": "sha256-f0SBJz1eZ2yOuKUr5CA9BHULGXVSn6miBuUWdTyhUhU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b69de56fac8c2b6f8fd27f2eca01dcda8e0a4221", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1731890469, + "narHash": "sha256-D1FNZ70NmQEwNxpSSdTXCSklBH1z2isPR84J6DQrJGs=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5083ec887760adfe12af64830a66807423a859a7", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay", + "treefmt": "treefmt" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1733020719, + "narHash": "sha256-Chv9+3zrf1DhdB9JyskjoV0vJbCQEgkVqrU3p4RPLv8=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "8e18f10703112e6c33e1c0d8b93e8305f6f0a75c", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "treefmt": { + "inputs": { + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1732894027, + "narHash": "sha256-2qbdorpq0TXHBWbVXaTqKoikN4bqAtAplTwGuII+oAc=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "6209c381904cab55796c5d7350e89681d3b2a8ef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..0211d13 --- /dev/null +++ b/flake.nix @@ -0,0 +1,96 @@ +{ + description = "Description for the project"; + + inputs = { + flake-parts.url = "github:hercules-ci/flake-parts"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + rust-overlay.url = "github:oxalica/rust-overlay"; + treefmt.url = "github:numtide/treefmt-nix"; + }; + + outputs = + inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ inputs.treefmt.flakeModule ]; + systems = [ + "x86_64-linux" + "aarch64-linux" + "aarch64-darwin" + "x86_64-darwin" + ]; + perSystem = + { + config, + self', + inputs', + pkgs, + system, + ... + }: + let + runtimeDeps = with pkgs; [ + alsa-lib + speechd + ]; + buildDeps = with pkgs; [ + pkg-config + rustPlatform.bindgenHook + ]; + devDeps = with pkgs; [ gdb ]; + + cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml); + + rustPackage = + features: + (pkgs.makeRustPlatform { + cargo = pkgs.rust-bin.stable.latest.minimal; + rustc = pkgs.rust-bin.stable.latest.minimal; + }).buildRustPackage + { + inherit (cargoToml.package) name version; + src = ./.; + cargoLock.lockFile = ./Cargo.lock; + buildFeatures = features; + buildInputs = runtimeDeps; + nativeBuildInputs = buildDeps; + # Uncomment if your cargo tests require networking or otherwise + # don't play nicely with the Nix build sandbox: + # doCheck = false; + }; + + in + { + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ (import inputs.rust-overlay) ]; + }; + + packages.default = self'.packages.example; + devShells.default = pkgs.mkShell { + shellHook = '' + export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} + ''; + buildInputs = runtimeDeps; + nativeBuildInputs = buildDeps ++ devDeps ++ [ (pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)) ]; + }; + + packages.example = (rustPackage "foobar"); + packages.example-base = (rustPackage ""); + + + treefmt = { + projectRootFile = "./flake.nix"; + programs = { + nixfmt.enable = true; + rustfmt.enable = true; + }; + }; + }; + flake = { + # The usual flake attributes can be defined here, including system- + # agnostic ones like nixosModule and system-enumerating ones, although + # those are more easily expressed in perSystem. + + }; + }; +} diff --git a/inputs/day01/example.txt b/inputs/day01/example.txt new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/inputs/day01/example.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/src/day01.rs b/src/day01.rs new file mode 100644 index 0000000..9b30a03 --- /dev/null +++ b/src/day01.rs @@ -0,0 +1,52 @@ +pub fn pt1(input: &str) -> i32 { + let elems = input + .lines() + .filter_map(|l| l.split_once(" ")) + .map(|(l, r)| (l.parse::(), r.parse::())) + .filter(|(l, r)| l.is_ok() && r.is_ok()) + .map(|(l, r)| (l.unwrap(), r.unwrap())); + let mut lefts = elems.clone().map(|(l, _)| l).collect::>(); + let mut rights = elems.map(|(_, r)| r).collect::>(); + lefts.sort_unstable(); + rights.sort_unstable(); + let res: u32 = lefts + .iter() + .zip(rights.iter()) + .map(|(l, r)| l.abs_diff(*r)) + .sum(); + res as i32 +} +pub fn pt2(input: &str) -> i32 { + let elems = input + .lines() + .filter_map(|l| l.split_once(" ")) + .map(|(l, r)| (l.parse::(), r.parse::())) + .filter(|(l, r)| l.is_ok() && r.is_ok()) + .map(|(l, r)| (l.unwrap(), r.unwrap())); + let lefts = elems.clone().map(|(l, _)| l); + let rights = elems.map(|(_, r)| r).collect::>(); + lefts + .map(|l| rights.clone().iter().filter(|r| **r == l).count() as i32 * l) + .sum() +} + +#[cfg(test)] +mod tests { + use std::fs::read_to_string; + + use crate::day01::{pt1, pt2}; + + #[test] + pub fn test_pt1() { + let input = + read_to_string("./inputs/day01/example.txt").expect("Missing example.txt for day01"); + assert_eq!(pt1(&input), 11) + } + #[test] + pub fn test_pt2() { + let input = + read_to_string("./inputs/day01/example.txt").expect("Missing example.txt for day01"); + + assert_eq!(pt2(&input), 31) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bda1585 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +use std::fs::read_to_string; + +use crate::day01::{pt1, pt2}; + +mod day01; +fn main() { + println!("day 01!"); + let input = read_to_string("./inputs/day01/input.txt").expect("Missing input.txt for day01"); + println!("And the result is: {}", pt1(&input)); + println!("Part 2: {}", pt2(&input)); +}