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

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