summaryrefslogtreecommitdiff
path: root/aoc/2025/03/rust
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/2025/03/rust
parent62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff)
downloadpuzzles-86bac31392a76da84817eec020d2b84d099b3cc1.tar.gz
puzzles-86bac31392a76da84817eec020d2b84d099b3cc1.zip
add other challenges supportHEADmaster
Diffstat (limited to 'aoc/2025/03/rust')
-rw-r--r--aoc/2025/03/rust/Cargo.lock7
-rw-r--r--aoc/2025/03/rust/Cargo.toml6
-rw-r--r--aoc/2025/03/rust/src/main.rs42
3 files changed, 55 insertions, 0 deletions
diff --git a/aoc/2025/03/rust/Cargo.lock b/aoc/2025/03/rust/Cargo.lock
new file mode 100644
index 0000000..ac79b17
--- /dev/null
+++ b/aoc/2025/03/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/2025/03/rust/Cargo.toml b/aoc/2025/03/rust/Cargo.toml
new file mode 100644
index 0000000..26e4e77
--- /dev/null
+++ b/aoc/2025/03/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "puzzle"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/aoc/2025/03/rust/src/main.rs b/aoc/2025/03/rust/src/main.rs
new file mode 100644
index 0000000..be0b3e6
--- /dev/null
+++ b/aoc/2025/03/rust/src/main.rs
@@ -0,0 +1,42 @@
+use std::io;
+
+fn max(bs: &Vec<u8>, start: usize, end: usize) -> (u8, usize) {
+ let mut bests = [None; 10];
+ for idx in start..=end {
+ if bs[idx] == 9 {
+ return (9, idx);
+ }
+ bests[bs[idx] as usize].get_or_insert((bs[idx], idx));
+ }
+ bests.into_iter().flatten().last().unwrap()
+}
+
+fn jolt(bs: &Vec<u8>, n: usize) -> u64 {
+ let mut j: u64 = 0;
+ let mut start = 0;
+ for n in (1..=n).rev() {
+ j *= 10;
+ let (b, idx) = max(bs, start, bs.len() - n);
+ j += b as u64;
+ start = idx + 1;
+ }
+ j
+}
+
+fn main() {
+ let (silver, gold) = io::stdin()
+ .lines()
+ .flatten()
+ .map(|line| {
+ line.into_bytes()
+ .iter()
+ .map(|&x| x - b'0')
+ .collect::<Vec<u8>>()
+ })
+ .map(|bs| (jolt(&bs, 2), jolt(&bs, 12)))
+ .reduce(|(a0, a1), (x0, x1)| (a0 + x0, a1 + x1))
+ .unwrap();
+
+ println!("silver: {silver}");
+ println!("gold: {gold}");
+}