diff options
author | mhsn <mail@mhsn.net> | 2025-03-19 22:48:52 +0000 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2025-03-19 22:48:52 +0000 |
commit | 57e3b1f2d31c7f7d3ea81be3beebe8b47358374a (patch) | |
tree | 9eecc35f4371b885d1be173e7f936e6dad6b4ee6 | |
parent | 8b8a804994dd61c8c61c1452c57551b1ca6ded88 (diff) | |
download | aoc-57e3b1f2d31c7f7d3ea81be3beebe8b47358374a.tar.gz aoc-57e3b1f2d31c7f7d3ea81be3beebe8b47358374a.zip |
2024-01 rust p1,p2
-rw-r--r-- | 2024/01/rust/Cargo.lock | 7 | ||||
-rw-r--r-- | 2024/01/rust/Cargo.toml | 6 | ||||
-rw-r--r-- | 2024/01/rust/src/main.rs | 31 |
3 files changed, 44 insertions, 0 deletions
diff --git a/2024/01/rust/Cargo.lock b/2024/01/rust/Cargo.lock new file mode 100644 index 0000000..7010c56 --- /dev/null +++ b/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 = "aoc_2024-01" +version = "0.1.0" diff --git a/2024/01/rust/Cargo.toml b/2024/01/rust/Cargo.toml new file mode 100644 index 0000000..0e9bfaa --- /dev/null +++ b/2024/01/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc_2024-01" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/01/rust/src/main.rs b/2024/01/rust/src/main.rs new file mode 100644 index 0000000..c1648cf --- /dev/null +++ b/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(()); +} |