day 1: part 2: rust

This commit is contained in:
Samuel Ortion 2024-12-01 10:40:27 +01:00
parent d348f1ad22
commit 8ae35afa9f
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[package]
name = "part2"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,36 @@
fn similarity(nums1: &Vec<u64>, nums2: &Vec<u64>) -> 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<u64> = Vec::new();
let mut nums2: Vec<u64> = 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(())
}