From 9b822cedc83222fd628bcdb092943542ce951e93 Mon Sep 17 00:00:00 2001 From: Rakarake Date: Sun, 30 Nov 2025 16:04:14 +0100 Subject: [PATCH] scaffholding --- .envrc | 1 + .gitignore | 3 ++ Cargo.lock | 7 ++++ Cargo.toml | 3 ++ README.md | 1 + aoc2024/Cargo.toml | 9 +++++ aoc2024/day1.rs | 48 +++++++++++++++++++++++ aoc2024/day2.rs | 62 ++++++++++++++++++++++++++++++ aoc2024/main.rs | 28 ++++++++++++++ flake.lock | 96 ++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 47 +++++++++++++++++++++++ 11 files changed, 305 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 aoc2024/Cargo.toml create mode 100644 aoc2024/day1.rs create mode 100644 aoc2024/day2.rs create mode 100644 aoc2024/main.rs create mode 100644 flake.lock create mode 100644 flake.nix 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..9a43a08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/target +.direnv +inputs/ 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..beb0cdd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = [ "aoc2024" ] +resolver = "3" diff --git a/README.md b/README.md new file mode 100644 index 0000000..a2dde01 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +2024 is just a test for now diff --git a/aoc2024/Cargo.toml b/aoc2024/Cargo.toml new file mode 100644 index 0000000..abf13c6 --- /dev/null +++ b/aoc2024/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc2024" +version = "0.1.0" +edition = "2024" + +[[bin]] +name = "aoc2024" +path = "main.rs" + diff --git a/aoc2024/day1.rs b/aoc2024/day1.rs new file mode 100644 index 0000000..a3be36f --- /dev/null +++ b/aoc2024/day1.rs @@ -0,0 +1,48 @@ +const TEST_INPUT: &str = "\ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 +"; + +const INPUT: &str = include_str!("../inputs/2024/day1.input"); + +fn parse(i: &str) -> Vec<(i32, i32)> { + i + .lines() + .map(|l| { + let ns = l + .split_once(" ") + .unwrap(); + (ns.0.parse::().unwrap(), ns.1.parse::().unwrap()) + } + ).collect() +} + +fn the_lists(i: Vec<(i32, i32)>) -> (Vec, Vec) { + let mut left_list: Vec = vec![]; + let mut right_list: Vec = vec![]; + for (left, right) in i { + left_list.push(left); + right_list.push(right); + } + (left_list, right_list) +} + +pub fn part1() -> i32 { + let i = parse(INPUT); + let (mut left_list, mut right_list) = the_lists(i); + left_list.sort(); + right_list.sort(); + use std::iter::zip; + zip(left_list.iter(), right_list.iter()).fold(0, |acc, (l, r)| acc + (l - r).abs()) +} + +pub fn part2() -> i32 { + let i = parse(INPUT); + let (mut left_list, mut right_list) = the_lists(i); + left_list.iter().fold(0, |acc, n1| + acc + n1 * right_list.iter().filter(|n2| n1 == *n2).count() as i32) +} diff --git a/aoc2024/day2.rs b/aoc2024/day2.rs new file mode 100644 index 0000000..620f3d5 --- /dev/null +++ b/aoc2024/day2.rs @@ -0,0 +1,62 @@ +const TEST_INPUT: &'static str = "\ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9\ +"; + +const INPUT: &'static str = include_str!("../inputs/2024/day2.input"); + +fn parse(i: &str) -> Vec> { + i.lines().map(|l| + l + .split_whitespace() + .map(|w| w.parse::().unwrap()) + .collect() + ).collect() +} + +// Might make it not return a vector +fn follows_rules(i: &Vec) -> Vec { + let mut iter = i.iter(); + let first = iter.next().unwrap(); + let second = iter.next().unwrap(); + let initial_diff = second - first; + let initial_holds = initial_diff.abs() <= 3 && initial_diff.abs() >= 1; + let (acc, _, _) = iter.fold((vec![true, initial_holds], *second, initial_diff), + |(mut acc, prev, prev_diff): (Vec, i32, i32), n: &i32| { + let diff: i32 = n - prev; + acc.push( + diff.abs() <= 3 && + diff.abs() >= 1 && + ((diff > 0 && prev_diff > 0) || (diff < 0 && prev_diff < 0)) + ); + (acc, *n, diff) + }); + acc +} + +fn try_without(mut l: Vec, index: usize) -> bool { + l.remove(index); + follows_rules(&l).iter().all(|x| *x) +} + +pub fn part1() -> u32 { + let input = parse(INPUT); + input.iter().map(|l| if follows_rules(l).iter().all(|x| *x) { 1 } else { 0 }).sum() +} + +pub fn part2() -> u32 { + let mut input = parse(INPUT); + input.iter_mut().map(|l| { + let rules = follows_rules(l); + if rules.iter().all(|x| *x) { + 1 + } else { + l.iter().enumerate().any(|(i, _)| try_without(l.clone(), i)) as u32 + } + }).sum() +} + diff --git a/aoc2024/main.rs b/aoc2024/main.rs new file mode 100644 index 0000000..503f334 --- /dev/null +++ b/aoc2024/main.rs @@ -0,0 +1,28 @@ +mod day1; +mod day2; + +fn main() { + // part 1 or 2 + let day = std::env::args().nth(1).expect("needs to specify day for first argument"); + let part = std::env::args().nth(2).expect("needs to specify 1 or 2 for second argument"); + + if day == "day1" { + if part == "1" { + println!("{}", day1::part1()); + } else if part == "2" { + println!("{}", day1::part2()); + } else { + panic!("expecting 1 or 2 for argument 2") + } + } + + if day == "day2" { + if part == "1" { + println!("{}", day2::part1()); + } else if part == "2" { + println!("{}", day2::part2()); + } else { + panic!("expecting 1 or 2 for argument 2") + } + } +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..18ee75f --- /dev/null +++ b/flake.lock @@ -0,0 +1,96 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1764242076, + "narHash": "sha256-sKoIWfnijJ0+9e4wRvIgm/HgE27bzwQxcEmo2J/gNpI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "2fad6eac6077f03fe109c4d4eb171cf96791faa4", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1744536153, + "narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1764470739, + "narHash": "sha256-sa9f81B1dWO16QtgDTWHX8DQbiHKzHndpaunY5EQtwE=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "3bfa664055e1a09c6aedab5533c5fc8d6ca5741a", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c6775a8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,47 @@ +{ + description = "Rust flake"; + inputs = + { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; # or whatever vers + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + }; + + outputs = { self, nixpkgs, flake-utils, rust-overlay, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + in + { + devShell = pkgs.mkShell rec { + LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath buildInputs}"; + packages = with pkgs; [ + (rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)) + gcc + ]; + buildInputs = with pkgs; [ + libdisplay-info + libgbm + #mesa + libinput + pixman + seatd + udev + libxkbcommon + wayland + wayland.dev + wayland-protocols + libGL + vulkan-headers vulkan-loader + vulkan-tools vulkan-tools-lunarg + vulkan-extension-layer + vulkan-validation-layers # don't need them *strictly* but immensely helpful + #libglvnd + ]; + }; + } + ); +}