From 928218226b4643b5e0b6cb491ed8af84bc0bd666 Mon Sep 17 00:00:00 2001 From: Siphalor Date: Tue, 3 Dec 2024 23:31:21 +0100 Subject: [PATCH] Day one --- .gitignore | 2 ++ day-01-rust/.gitignore | 1 + day-01-rust/Cargo.lock | 7 +++++++ day-01-rust/Cargo.toml | 6 ++++++ day-01-rust/src/main.rs | 24 ++++++++++++++++++++++++ day-01-rust/src/task1.rs | 27 +++++++++++++++++++++++++++ day-01-rust/src/task2.rs | 31 +++++++++++++++++++++++++++++++ 7 files changed, 98 insertions(+) create mode 100644 .gitignore create mode 100644 day-01-rust/.gitignore create mode 100644 day-01-rust/Cargo.lock create mode 100644 day-01-rust/Cargo.toml create mode 100644 day-01-rust/src/main.rs create mode 100644 day-01-rust/src/task1.rs create mode 100644 day-01-rust/src/task2.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..607250b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +input/ +.idea diff --git a/day-01-rust/.gitignore b/day-01-rust/.gitignore new file mode 100644 index 0000000..9f97022 --- /dev/null +++ b/day-01-rust/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/day-01-rust/Cargo.lock b/day-01-rust/Cargo.lock new file mode 100644 index 0000000..5756c5d --- /dev/null +++ b/day-01-rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day-01-rust" +version = "0.1.0" diff --git a/day-01-rust/Cargo.toml b/day-01-rust/Cargo.toml new file mode 100644 index 0000000..a7230cc --- /dev/null +++ b/day-01-rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day-01-rust" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day-01-rust/src/main.rs b/day-01-rust/src/main.rs new file mode 100644 index 0000000..0c91737 --- /dev/null +++ b/day-01-rust/src/main.rs @@ -0,0 +1,24 @@ +mod task1; +mod task2; + +fn main() { + let args = std::env::args().collect::>(); + if args.len() != 3 { + println!("Usage: {} <1|2> ", args[0]); + return; + } + let task = args.get(1).unwrap(); + match task.as_str() { + "1" => { + task1::run(&*read_input(args.get(2).unwrap())); + } + "2" => { + task2::run(&*read_input(args.get(2).unwrap())); + } + _ => { eprintln!("First argument has to be 1 or 2") } + } +} + +fn read_input(file: &str) -> String { + std::fs::read_to_string(file).expect("Could not read file") +} diff --git a/day-01-rust/src/task1.rs b/day-01-rust/src/task1.rs new file mode 100644 index 0000000..ac00bf7 --- /dev/null +++ b/day-01-rust/src/task1.rs @@ -0,0 +1,27 @@ +use std::str::FromStr; + +pub fn run(input: &str) { + let mut first = Vec::::new(); + let mut second = Vec::::new(); + for line in input.split('\n') { + if let Some((first_part, second_part)) = line.split_once(" ") { + match i64::from_str(first_part) { + Ok(n) => first.push(n), + Err(err) => { + eprintln!("Failed to parse first number from {}: {}", first_part, err); + } + } + match i64::from_str(second_part) { + Ok(n) => second.push(n), + Err(err) => { + eprintln!("Failed to parse second number from {}: {}", second_part, err); + } + } + } + } + + first.sort(); + second.sort(); + let sum = first.iter().zip(second.iter()).map(|(a, b)| a.abs_diff(*b)).sum::(); + println!("Total difference: {}", sum); +} \ No newline at end of file diff --git a/day-01-rust/src/task2.rs b/day-01-rust/src/task2.rs new file mode 100644 index 0000000..1dfc7d2 --- /dev/null +++ b/day-01-rust/src/task2.rs @@ -0,0 +1,31 @@ +use std::collections::HashMap; +use std::str::FromStr; + +pub fn run(input: &str) { + let mut first = Vec::::new(); + let mut second = Vec::::new(); + for line in input.split('\n') { + if let Some((first_part, second_part)) = line.split_once(" ") { + match i64::from_str(first_part) { + Ok(n) => first.push(n), + Err(err) => { + eprintln!("Failed to parse first number from {}: {}", first_part, err); + } + } + match i64::from_str(second_part) { + Ok(n) => second.push(n), + Err(err) => { + eprintln!("Failed to parse second number from {}: {}", second_part, err); + } + } + } + } + + let mut second_counts = HashMap::::new(); + for element in second { + *second_counts.entry(element).or_insert(0) += 1; + } + + let result = first.iter().map(|num| num * second_counts.get(num).unwrap_or(&0)).sum::(); + println!("Similarity: {}", result); +}