summaryrefslogtreecommitdiff
path: root/aoc/2024/01/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/2024/01/rust
parent62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff)
downloadpuzzles-86bac31392a76da84817eec020d2b84d099b3cc1.tar.gz
puzzles-86bac31392a76da84817eec020d2b84d099b3cc1.zip
add other challenges supportHEADmaster
Diffstat (limited to 'aoc/2024/01/rust')
-rw-r--r--aoc/2024/01/rust/Cargo.lock7
-rw-r--r--aoc/2024/01/rust/Cargo.toml6
-rw-r--r--aoc/2024/01/rust/src/main.rs31
3 files changed, 44 insertions, 0 deletions
diff --git a/aoc/2024/01/rust/Cargo.lock b/aoc/2024/01/rust/Cargo.lock
new file mode 100644
index 0000000..ac79b17
--- /dev/null
+++ b/aoc/2024/01/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/01/rust/Cargo.toml b/aoc/2024/01/rust/Cargo.toml
new file mode 100644
index 0000000..88e7b42
--- /dev/null
+++ b/aoc/2024/01/rust/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "puzzle"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/aoc/2024/01/rust/src/main.rs b/aoc/2024/01/rust/src/main.rs
new file mode 100644
index 0000000..c1648cf
--- /dev/null
+++ b/aoc/2024/01/rust/src/main.rs
@@ -0,0 +1,31 @@
+use std::collections::HashMap;
+use std::io;
+
+fn main() -> io::Result<()> {
+ let (mut ls, mut rs): (Vec<u64>, Vec<u64>) = io::stdin()
+ .lines()
+ .map(|line| {
+ line.unwrap()
+ .split_whitespace()
+ .map(|s| s.parse().unwrap())
+ .collect::<Vec<u64>>()
+ })
+ .map(|xs| (xs[0], xs[1]))
+ .unzip();
+
+ ls.sort_unstable();
+ rs.sort_unstable();
+
+ let mut counts = HashMap::new();
+ rs.iter().for_each(|&x| {
+ *counts.entry(x).or_insert(0) += 1;
+ });
+
+ let silver: u64 = ls.iter().zip(rs).map(|(x, y)| x.abs_diff(y)).sum();
+ let gold: u64 = ls.iter().map(|x| x * counts.get(x).unwrap_or(&0)).sum();
+
+ println!("silver: {silver}");
+ println!("gold: {gold}");
+
+ return Ok(());
+}