Skip to content

Commit

Permalink
partial 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
Alextopher committed Dec 12, 2024
1 parent f9c3c7a commit cc50d1e
Show file tree
Hide file tree
Showing 12 changed files with 762 additions and 7 deletions.
54 changes: 48 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ members = [
"y23/d04-scratchcards",
"y23/d05-if-you-give-a-seed-a-fertilizer",
"y23/d06-wait-for-it",
"y23/d07-camel-cards",
"y23/d07-camel-cards",
"y23/d08-brute-forced",
"y23/d08-haunted-wasteland",
"y23/d09-mirage-maintenance",
Expand All @@ -56,4 +56,10 @@ members = [
"y23/d23",
"y23/d24",
"y23/d25",

"y24/d01-historian-hysteria",
"y24/d05-print-queue",
"y24/d06-guard-gallivant",
"y24/d09-disk-fragmenter",
"y24/d10-hoof-it",
]
11 changes: 11 additions & 0 deletions y24/d01-historian-hysteria/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "y24d01"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aoc = { path = "../../aoc" }
radsort = "0.1.1"
rayon = "1.10.0"
77 changes: 77 additions & 0 deletions y24/d01-historian-hysteria/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::collections::HashMap;

use aoc::input_str;

fn part1(input: &str) -> i32 {
let (mut lefts, mut rights) = input
.lines()
.map(|line| {
let mut iter = line.split_whitespace();
(
iter.next().unwrap().parse::<i32>().unwrap(),
iter.next().unwrap().parse::<i32>().unwrap(),
)
})
.fold((vec![], vec![]), |(mut lefts, mut rights), (l, r)| {
lefts.push(l);
rights.push(r);
(lefts, rights)
});

lefts.sort_unstable();
rights.sort_unstable();

lefts
.iter()
.zip(rights.iter())
.map(|(l, r)| (l - r).abs())
.sum()
}

fn part2(input: &str) -> usize {
let (lefts, rights): (HashMap<usize, usize>, HashMap<usize, usize>) = input
.lines()
.map(|line| {
let mut iter = line.split_whitespace();
(
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
)
})
.fold(
(Default::default(), Default::default()),
|(mut lefts, mut rights), (l, r)| {
*lefts.entry(l).or_insert(0) += 1;
*rights.entry(r).or_insert(0) += 1;
(lefts, rights)
},
);

lefts
.into_iter()
.map(|(id, l_count)| id * l_count * rights.get(&id).unwrap_or(&0))
.sum()
}

fn main() {
let input = input_str!(2024, 1);

let start = std::time::Instant::now();
println!("Part 1: {}", part1(input));
println!("Time: {:?}", start.elapsed());

let start = std::time::Instant::now();
println!("Part 2: {}", part2(input));
println!("Time: {:?}", start.elapsed());
}

#[cfg(test)]
mod tests {
use crate::part2;

#[test]
fn test_part2() {
let input = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3";
assert_eq!(part2(input), 31);
}
}
7 changes: 7 additions & 0 deletions y24/d05-print-queue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "d05-print-queue"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../../aoc" }
117 changes: 117 additions & 0 deletions y24/d05-print-queue/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::cmp::Ordering;

use aoc::input_str;

/// Eh, every problem is a graph problem.
///
/// If (x, y) is in the matrix then X | Y, X comes before Y
#[derive(Debug)]
pub struct Graph {
n: usize,
matrix: Vec<bool>,
}

impl Graph {
pub fn new(n: usize) -> Self {
Self {
n,
matrix: vec![false; (n + 1) * (n + 1)],
}
}

pub fn index(&self, x: usize, y: usize) -> usize {
y * self.n + x
}

pub fn insert(&mut self, x: usize, y: usize) {
let idx = self.index(x, y);
self.matrix[idx] = true
}

pub fn contains(&self, x: usize, y: usize) -> bool {
let idx = self.index(x, y);
self.matrix[idx]
}
}

fn main() {
let input = input_str!(2024, 5);

let start = std::time::Instant::now();

let mut lines = input.lines();

let mut counter = 0;
let mut mapping = [None; 100];
let mut reverse = vec![];
let mut constraints = vec![];

for line in lines.by_ref() {
if line.is_empty() {
break;
}

let mut split = line.split('|');
let x: usize = split.next().unwrap().parse().unwrap();
let y: usize = split.next().unwrap().parse().unwrap();

if mapping[x].is_none() {
counter += 1;
mapping[x] = Some(counter);
reverse.push(x);
}

if mapping[y].is_none() {
counter += 1;
mapping[y] = Some(counter);
reverse.push(y);
}

constraints.push((mapping[x].unwrap(), mapping[y].unwrap()));
}

debug_assert_eq!(mapping.iter().filter(|m| m.is_some()).count(), counter);
let mut graph = Graph::new(counter);
for (x, y) in constraints {
graph.insert(x, y);
}

println!("Created graph: {:?}", start.elapsed());
let start = std::time::Instant::now();

let (good, bad): (Vec<_>, Vec<_>) = lines
.map(|line| {
line.split(',')
.map(|i| i.parse::<usize>().unwrap())
.map(|i| mapping[i].unwrap())
.collect::<Vec<_>>()
})
.partition(|pages| pages.windows(2).all(|v| graph.contains(v[0], v[1])));

println!("Partitioned: {:?}", start.elapsed());
let start = std::time::Instant::now();

let part1: usize = good
.into_iter()
.map(|pages| reverse[pages[pages.len() / 2] - 1])
.sum();

println!("Part 1: {:?}", start.elapsed());
println!("Part 1: {}", part1);
let start = std::time::Instant::now();

let part2: usize = bad
.into_iter()
.map(|mut page| {
page.sort_unstable_by(|x, y| match graph.contains(*x, *y) {
true => Ordering::Greater,
false => Ordering::Less,
});
page
})
.map(|pages| reverse[pages[pages.len() / 2] - 1])
.sum();

println!("Part 2: {:?}", start.elapsed());
println!("Part 2: {}", part2);
}
7 changes: 7 additions & 0 deletions y24/d06-guard-gallivant/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "d06-guard-gallivant"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../../aoc" }
Loading

0 comments on commit cc50d1e

Please sign in to comment.