From df1ea7405a6e2ce2b4b39f06e23b318e157a0699 Mon Sep 17 00:00:00 2001 From: Codinget Date: Tue, 3 Dec 2024 08:04:53 +0000 Subject: [PATCH] day 1, rust --- .gitignore | 2 ++ 01/Cargo.lock | 7 +++++++ 01/Cargo.toml | 6 ++++++ 01/src/main.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 .gitignore create mode 100644 01/Cargo.lock create mode 100644 01/Cargo.toml create mode 100644 01/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a172e65 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +input.txt diff --git a/01/Cargo.lock b/01/Cargo.lock new file mode 100644 index 0000000..861e054 --- /dev/null +++ b/01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc2024-01" +version = "0.1.0" diff --git a/01/Cargo.toml b/01/Cargo.toml new file mode 100644 index 0000000..c179218 --- /dev/null +++ b/01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc2024-01" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/01/src/main.rs b/01/src/main.rs new file mode 100644 index 0000000..c88e0f9 --- /dev/null +++ b/01/src/main.rs @@ -0,0 +1,30 @@ +fn main() { + let raw = std::fs::read_to_string("input.txt").expect("Couldn't read input"); + let mut left = Vec::new(); + let mut right = Vec::new(); + for line in raw.split("\n") { + if line.is_empty() { + continue; + } + let split = line + .split(" ") + .map(|x| x.parse()) + .collect::, _>>() + .unwrap(); + left.push(split[0]); + right.push(split[1]); + } + left.sort(); + right.sort(); + let first = left + .iter() + .zip(right.iter()) + .map(|(a, b)| a.max(b) - a.min(b)) + .fold(0, |a, b| a + b); + println!("{}", first); + let second = left + .iter() + .map(|a| *a * right.iter().filter(|b| *a == **b).count() as u32) + .fold(0, |a, b| a + b); + println!("{}", second); +}