From 8ae35afa9f49ea4cf447854e3f55e78c3130d86f Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Sun, 1 Dec 2024 10:40:27 +0100 Subject: [PATCH] day 1: part 2: rust --- 2024/days/01/part2/Cargo.toml | 6 ++++++ 2024/days/01/part2/src/main.rs | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 2024/days/01/part2/Cargo.toml create mode 100644 2024/days/01/part2/src/main.rs diff --git a/2024/days/01/part2/Cargo.toml b/2024/days/01/part2/Cargo.toml new file mode 100644 index 0000000..2001cb3 --- /dev/null +++ b/2024/days/01/part2/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "part2" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/days/01/part2/src/main.rs b/2024/days/01/part2/src/main.rs new file mode 100644 index 0000000..f4066f4 --- /dev/null +++ b/2024/days/01/part2/src/main.rs @@ -0,0 +1,36 @@ + +fn similarity(nums1: &Vec, nums2: &Vec) -> u64 { + let mut s = 0; + for num1 in nums1 { + let mut count: u64 = 0; + for num2 in nums2 { + if *num1 == *num2 { + count += 1; + } + } + s += num1 * count; + } + s +} + + +fn main() -> std::io::Result<()> { + let mut nums1: Vec = Vec::new(); + let mut nums2: Vec = Vec::new(); + for line in std::io::stdin().lines() { + let line = line.unwrap(); + if line == "" { + continue; + } + let mut parts = line.split(" "); + let num1 = parts.next().unwrap(); + let num1: u64 = num1.parse().unwrap(); + let num2 = parts.next().unwrap(); + let num2: u64 = num2.parse().unwrap(); + nums1.push(num1); + nums2.push(num2); + } + let s = similarity(&nums1, &nums2); + println!("{}", s); + Ok(()) +}