Skip to content

Commit cc50d1e

Browse files
committed
partial 2024
1 parent f9c3c7a commit cc50d1e

File tree

12 files changed

+762
-7
lines changed

12 files changed

+762
-7
lines changed

Cargo.lock

Lines changed: 48 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ members = [
3636
"y23/d04-scratchcards",
3737
"y23/d05-if-you-give-a-seed-a-fertilizer",
3838
"y23/d06-wait-for-it",
39-
"y23/d07-camel-cards",
39+
"y23/d07-camel-cards",
4040
"y23/d08-brute-forced",
4141
"y23/d08-haunted-wasteland",
4242
"y23/d09-mirage-maintenance",
@@ -56,4 +56,10 @@ members = [
5656
"y23/d23",
5757
"y23/d24",
5858
"y23/d25",
59+
60+
"y24/d01-historian-hysteria",
61+
"y24/d05-print-queue",
62+
"y24/d06-guard-gallivant",
63+
"y24/d09-disk-fragmenter",
64+
"y24/d10-hoof-it",
5965
]

y24/d01-historian-hysteria/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "y24d01"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
aoc = { path = "../../aoc" }
10+
radsort = "0.1.1"
11+
rayon = "1.10.0"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::collections::HashMap;
2+
3+
use aoc::input_str;
4+
5+
fn part1(input: &str) -> i32 {
6+
let (mut lefts, mut rights) = input
7+
.lines()
8+
.map(|line| {
9+
let mut iter = line.split_whitespace();
10+
(
11+
iter.next().unwrap().parse::<i32>().unwrap(),
12+
iter.next().unwrap().parse::<i32>().unwrap(),
13+
)
14+
})
15+
.fold((vec![], vec![]), |(mut lefts, mut rights), (l, r)| {
16+
lefts.push(l);
17+
rights.push(r);
18+
(lefts, rights)
19+
});
20+
21+
lefts.sort_unstable();
22+
rights.sort_unstable();
23+
24+
lefts
25+
.iter()
26+
.zip(rights.iter())
27+
.map(|(l, r)| (l - r).abs())
28+
.sum()
29+
}
30+
31+
fn part2(input: &str) -> usize {
32+
let (lefts, rights): (HashMap<usize, usize>, HashMap<usize, usize>) = input
33+
.lines()
34+
.map(|line| {
35+
let mut iter = line.split_whitespace();
36+
(
37+
iter.next().unwrap().parse::<usize>().unwrap(),
38+
iter.next().unwrap().parse::<usize>().unwrap(),
39+
)
40+
})
41+
.fold(
42+
(Default::default(), Default::default()),
43+
|(mut lefts, mut rights), (l, r)| {
44+
*lefts.entry(l).or_insert(0) += 1;
45+
*rights.entry(r).or_insert(0) += 1;
46+
(lefts, rights)
47+
},
48+
);
49+
50+
lefts
51+
.into_iter()
52+
.map(|(id, l_count)| id * l_count * rights.get(&id).unwrap_or(&0))
53+
.sum()
54+
}
55+
56+
fn main() {
57+
let input = input_str!(2024, 1);
58+
59+
let start = std::time::Instant::now();
60+
println!("Part 1: {}", part1(input));
61+
println!("Time: {:?}", start.elapsed());
62+
63+
let start = std::time::Instant::now();
64+
println!("Part 2: {}", part2(input));
65+
println!("Time: {:?}", start.elapsed());
66+
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use crate::part2;
71+
72+
#[test]
73+
fn test_part2() {
74+
let input = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3";
75+
assert_eq!(part2(input), 31);
76+
}
77+
}

y24/d05-print-queue/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "d05-print-queue"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }

y24/d05-print-queue/src/main.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use std::cmp::Ordering;
2+
3+
use aoc::input_str;
4+
5+
/// Eh, every problem is a graph problem.
6+
///
7+
/// If (x, y) is in the matrix then X | Y, X comes before Y
8+
#[derive(Debug)]
9+
pub struct Graph {
10+
n: usize,
11+
matrix: Vec<bool>,
12+
}
13+
14+
impl Graph {
15+
pub fn new(n: usize) -> Self {
16+
Self {
17+
n,
18+
matrix: vec![false; (n + 1) * (n + 1)],
19+
}
20+
}
21+
22+
pub fn index(&self, x: usize, y: usize) -> usize {
23+
y * self.n + x
24+
}
25+
26+
pub fn insert(&mut self, x: usize, y: usize) {
27+
let idx = self.index(x, y);
28+
self.matrix[idx] = true
29+
}
30+
31+
pub fn contains(&self, x: usize, y: usize) -> bool {
32+
let idx = self.index(x, y);
33+
self.matrix[idx]
34+
}
35+
}
36+
37+
fn main() {
38+
let input = input_str!(2024, 5);
39+
40+
let start = std::time::Instant::now();
41+
42+
let mut lines = input.lines();
43+
44+
let mut counter = 0;
45+
let mut mapping = [None; 100];
46+
let mut reverse = vec![];
47+
let mut constraints = vec![];
48+
49+
for line in lines.by_ref() {
50+
if line.is_empty() {
51+
break;
52+
}
53+
54+
let mut split = line.split('|');
55+
let x: usize = split.next().unwrap().parse().unwrap();
56+
let y: usize = split.next().unwrap().parse().unwrap();
57+
58+
if mapping[x].is_none() {
59+
counter += 1;
60+
mapping[x] = Some(counter);
61+
reverse.push(x);
62+
}
63+
64+
if mapping[y].is_none() {
65+
counter += 1;
66+
mapping[y] = Some(counter);
67+
reverse.push(y);
68+
}
69+
70+
constraints.push((mapping[x].unwrap(), mapping[y].unwrap()));
71+
}
72+
73+
debug_assert_eq!(mapping.iter().filter(|m| m.is_some()).count(), counter);
74+
let mut graph = Graph::new(counter);
75+
for (x, y) in constraints {
76+
graph.insert(x, y);
77+
}
78+
79+
println!("Created graph: {:?}", start.elapsed());
80+
let start = std::time::Instant::now();
81+
82+
let (good, bad): (Vec<_>, Vec<_>) = lines
83+
.map(|line| {
84+
line.split(',')
85+
.map(|i| i.parse::<usize>().unwrap())
86+
.map(|i| mapping[i].unwrap())
87+
.collect::<Vec<_>>()
88+
})
89+
.partition(|pages| pages.windows(2).all(|v| graph.contains(v[0], v[1])));
90+
91+
println!("Partitioned: {:?}", start.elapsed());
92+
let start = std::time::Instant::now();
93+
94+
let part1: usize = good
95+
.into_iter()
96+
.map(|pages| reverse[pages[pages.len() / 2] - 1])
97+
.sum();
98+
99+
println!("Part 1: {:?}", start.elapsed());
100+
println!("Part 1: {}", part1);
101+
let start = std::time::Instant::now();
102+
103+
let part2: usize = bad
104+
.into_iter()
105+
.map(|mut page| {
106+
page.sort_unstable_by(|x, y| match graph.contains(*x, *y) {
107+
true => Ordering::Greater,
108+
false => Ordering::Less,
109+
});
110+
page
111+
})
112+
.map(|pages| reverse[pages[pages.len() / 2] - 1])
113+
.sum();
114+
115+
println!("Part 2: {:?}", start.elapsed());
116+
println!("Part 2: {}", part2);
117+
}

y24/d06-guard-gallivant/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "d06-guard-gallivant"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }

0 commit comments

Comments
 (0)