scaffholding

This commit is contained in:
Rakarake 2025-11-30 16:04:14 +01:00
commit 9b822cedc8
11 changed files with 305 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 @@
**/target
.direnv
inputs/

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 = 4
[[package]]
name = "aoc2024"
version = "0.1.0"

3
Cargo.toml Normal file
View file

@ -0,0 +1,3 @@
[workspace]
members = [ "aoc2024" ]
resolver = "3"

1
README.md Normal file
View file

@ -0,0 +1 @@
2024 is just a test for now

9
aoc2024/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "aoc2024"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "aoc2024"
path = "main.rs"

48
aoc2024/day1.rs Normal file
View file

@ -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::<i32>().unwrap(), ns.1.parse::<i32>().unwrap())
}
).collect()
}
fn the_lists(i: Vec<(i32, i32)>) -> (Vec<i32>, Vec<i32>) {
let mut left_list: Vec<i32> = vec![];
let mut right_list: Vec<i32> = 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)
}

62
aoc2024/day2.rs Normal file
View file

@ -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<Vec<i32>> {
i.lines().map(|l|
l
.split_whitespace()
.map(|w| w.parse::<i32>().unwrap())
.collect()
).collect()
}
// Might make it not return a vector
fn follows_rules(i: &Vec<i32>) -> Vec<bool> {
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<bool>, 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<i32>, 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()
}

28
aoc2024/main.rs Normal file
View file

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

96
flake.lock generated Normal file
View file

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

47
flake.nix Normal file
View file

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