From 579535c553176e475ec31b1d060e73c98edb584c Mon Sep 17 00:00:00 2001 From: martabal <74269598+martabal@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:26:44 +0100 Subject: [PATCH] fix: optimize day01 --- Cargo.lock | 2 +- c/day01.c | 10 ++-------- cpp/day01.cc | 12 +++++------- crates/day01/Cargo.toml | 2 +- crates/day01/src/main.rs | 10 ++++------ 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0f5232..0d10fd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "day01" -version = "0.1.0" +version = "0.1.1" [[package]] name = "day02" diff --git a/c/day01.c b/c/day01.c index dbfe14b..79192bb 100644 --- a/c/day01.c +++ b/c/day01.c @@ -56,18 +56,14 @@ int main() { qsort(left_column, len, sizeof(int), compare_ints); qsort(right_column, len, sizeof(int), compare_ints); - unsigned int *distances = malloc(len * sizeof(unsigned int)); unsigned int sum = 0; for (size_t i = 0; i < len; i++) { - distances[i] = abs(left_column[i] - right_column[i]); - sum += distances[i]; + sum += abs(left_column[i] - right_column[i]); } printf("Part One solution: sum is %u\n", sum); - free(distances); // --- Part Two --- - unsigned int *appear = malloc(len * sizeof(unsigned int)); sum = 0; for (size_t i = 0; i < len; i++) { @@ -77,12 +73,10 @@ int main() { count++; } } - appear[i] = left_column[i] * count; - sum += appear[i]; + sum += left_column[i] * count; } printf("Part Two solution: sum is %u\n", sum); - free(appear); free(left_column); free(right_column); diff --git a/cpp/day01.cc b/cpp/day01.cc index 3b67595..9aa3af8 100644 --- a/cpp/day01.cc +++ b/cpp/day01.cc @@ -33,7 +33,7 @@ read_file(const std::string &input) { int main() { // --- Part One --- auto [left_column, right_column] = read_file("crates/day01/input.txt"); - std::vector distances; + unsigned int sum = 0; if (left_column.size() != right_column.size()) { throw std::runtime_error("Column sizes do not match"); @@ -43,22 +43,20 @@ int main() { std::sort(right_column.begin(), right_column.end()); for (size_t i = 0; i < left_column.size(); ++i) { - distances.push_back( - static_cast(std::abs(left_column[i] - right_column[i]))); + sum += + (static_cast(std::abs(left_column[i] - right_column[i]))); } - unsigned int sum = std::accumulate(distances.begin(), distances.end(), 0u); std::cout << "Part One solution: sum is " << sum << std::endl; // --- Part Two --- + sum = 0; std::vector appear; for (const auto &e : left_column) { unsigned int count = std::count(right_column.begin(), right_column.end(), e); - appear.push_back(static_cast(e) * count); + sum += static_cast(e) * count; } - - sum = std::accumulate(appear.begin(), appear.end(), 0); std::cout << "Part Two solution: sum is " << sum << std::endl; return 0; diff --git a/crates/day01/Cargo.toml b/crates/day01/Cargo.toml index f02b6c7..3888ec6 100644 --- a/crates/day01/Cargo.toml +++ b/crates/day01/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "day01" -version = "0.1.0" +version = "0.1.1" edition = "2021" [lints] diff --git a/crates/day01/src/main.rs b/crates/day01/src/main.rs index 99c5c76..408d362 100644 --- a/crates/day01/src/main.rs +++ b/crates/day01/src/main.rs @@ -4,7 +4,6 @@ use std::io::{self, BufRead}; fn main() { // --- Part One --- let (mut left_column, mut right_column) = read_file("crates/day01/input.txt").unwrap(); - let mut distances: Vec = Vec::new(); assert!(left_column.len() == right_column.len()); #[cfg(feature = "unstable")] @@ -20,14 +19,14 @@ fn main() { right_column.sort(); } + let mut sum: i32 = 0; for (i, _) in left_column.iter().enumerate() { - distances.push((left_column[i] - right_column[i]).unsigned_abs()); + sum += (left_column[i] - right_column[i]).abs(); } - let mut sum: u32 = distances.iter().copied().sum(); println!("Part One solution: sum is {sum}"); // --- Part Two --- - let mut appear: Vec = Vec::new(); + sum = 0; for e in &left_column { let count: i32 = right_column .iter() @@ -35,9 +34,8 @@ fn main() { .count() .try_into() .unwrap(); - appear.push((*e * count).try_into().unwrap()); + sum += *e * count; } - sum = appear.iter().copied().sum(); println!("Part Two solution: sum is {sum}"); }