-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9c3c7a
commit cc50d1e
Showing
12 changed files
with
762 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
Oops, something went wrong.