summaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/10/rust/Cargo.lock7
-rw-r--r--2024/10/rust/Cargo.toml6
-rw-r--r--2024/10/rust/src/main.rs52
3 files changed, 65 insertions, 0 deletions
diff --git a/2024/10/rust/Cargo.lock b/2024/10/rust/Cargo.lock
new file mode 100644
index 0000000..de24ddf
--- /dev/null
+++ b/2024/10/rust/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "aoc_2024-10"
+version = "0.1.0"
diff --git a/2024/10/rust/Cargo.toml b/2024/10/rust/Cargo.toml
new file mode 100644
index 0000000..787d795
--- /dev/null
+++ b/2024/10/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "aoc_2024-10"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/2024/10/rust/src/main.rs b/2024/10/rust/src/main.rs
new file mode 100644
index 0000000..b5b3825
--- /dev/null
+++ b/2024/10/rust/src/main.rs
@@ -0,0 +1,52 @@
+use std::collections::HashSet;
+use std::io;
+
+fn main() -> io::Result<()> {
+ let grid: Vec<Vec<u8>> = io::stdin()
+ .lines()
+ .flatten()
+ .map(|s| s.chars().map(|c| c.to_digit(10).unwrap() as u8).collect())
+ .collect();
+
+ let starts = grid
+ .iter()
+ .enumerate()
+ .flat_map(|(y, row)| row.iter().enumerate().map(move |(x, d)| (x, y, d)))
+ .filter_map(|(x, y, d)| (*d == 0).then_some((y, x)))
+ .collect::<Vec<_>>();
+
+ let silver: usize = starts.iter().map(|&s| trailheads(s, &grid, false)).sum();
+ let gold: usize = starts.iter().map(|&s| trailheads(s, &grid, true)).sum();
+
+ println!("silver: {silver}");
+ println!("gold: {gold}");
+
+ return Ok(());
+}
+
+fn trailheads(start: (usize, usize), grid: &Vec<Vec<u8>>, count_paths: bool) -> usize {
+ let mut seen = HashSet::new();
+ let mut tot = 0;
+ let mut q = vec![start];
+
+ while let Some(curr @ (y, x)) = q.pop() {
+ seen.insert(curr);
+ let h = grid[y][x];
+ if h == 9 {
+ tot += 1;
+ continue;
+ }
+
+ [
+ ((y + 1).min(grid.len() - 1), x),
+ (y, (x + 1).min(grid[0].len() - 1)),
+ (y.saturating_sub(1), x),
+ (y, x.saturating_sub(1)),
+ ]
+ .iter()
+ .filter(|&&(ny, nx)| grid[ny][nx] == h + 1)
+ .filter(|next| count_paths || !seen.contains(next))
+ .for_each(|&n| q.push(n));
+ }
+ tot
+}