summaryrefslogtreecommitdiff
path: root/aoc/2024/10
diff options
context:
space:
mode:
authormhsn <mail@mhsn.net>2026-03-18 21:48:13 +0000
committermhsn <mail@mhsn.net>2026-03-18 21:48:13 +0000
commit86bac31392a76da84817eec020d2b84d099b3cc1 (patch)
treee2ee52db59b86b914d5b4bcceb19c9b5d899fff4 /aoc/2024/10
parent62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff)
downloadpuzzles-master.tar.gz
puzzles-master.zip
add other challenges supportHEADmaster
Diffstat (limited to 'aoc/2024/10')
-rwxr-xr-xaoc/2024/10/python.py43
-rw-r--r--aoc/2024/10/rust/Cargo.lock7
-rw-r--r--aoc/2024/10/rust/Cargo.toml6
-rw-r--r--aoc/2024/10/rust/src/main.rs52
4 files changed, 108 insertions, 0 deletions
diff --git a/aoc/2024/10/python.py b/aoc/2024/10/python.py
new file mode 100755
index 0000000..88892b6
--- /dev/null
+++ b/aoc/2024/10/python.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+from fileinput import input
+
+grid = [[int(c) for c in line.strip()] for line in input()]
+
+starts = {
+ (idx, idy) for idy, line in enumerate(grid) for idx, n in enumerate(line) if n == 0
+}
+
+# print(*grid, sep="\n")
+# print(starts)
+
+
+def score_trailhead(start, gold):
+ score = 0
+ q = [start]
+ seen = set()
+ while q:
+ x, y = q.pop()
+ seen.add((x, y))
+
+ h = grid[y][x]
+ if h == 9:
+ score += 1
+ continue
+ for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
+ nx, ny = x + dx, y + dy
+ if ((nx, ny) in seen and not gold) or not (
+ 0 <= nx < len(grid) and 0 <= ny < len(grid[0])
+ ):
+ continue
+
+ if grid[ny][nx] == h + 1:
+ q.append((nx, ny))
+ return score
+
+
+silver = sum(score_trailhead(start, False) for start in starts)
+gold = sum(score_trailhead(start, True) for start in starts)
+
+print("silver:", silver)
+print("gold:", gold)
diff --git a/aoc/2024/10/rust/Cargo.lock b/aoc/2024/10/rust/Cargo.lock
new file mode 100644
index 0000000..ac79b17
--- /dev/null
+++ b/aoc/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 = "puzzle"
+version = "0.1.0"
diff --git a/aoc/2024/10/rust/Cargo.toml b/aoc/2024/10/rust/Cargo.toml
new file mode 100644
index 0000000..88e7b42
--- /dev/null
+++ b/aoc/2024/10/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "puzzle"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/aoc/2024/10/rust/src/main.rs b/aoc/2024/10/rust/src/main.rs
new file mode 100644
index 0000000..b5b3825
--- /dev/null
+++ b/aoc/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
+}