Skip to content

Commit

Permalink
fix: optimize day01
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal committed Dec 2, 2024
1 parent 24dd1d8 commit 579535c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions c/day01.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);

Expand Down
12 changes: 5 additions & 7 deletions cpp/day01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int> distances;
unsigned int sum = 0;

if (left_column.size() != right_column.size()) {
throw std::runtime_error("Column sizes do not match");
Expand All @@ -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<unsigned int>(std::abs(left_column[i] - right_column[i])));
sum +=
(static_cast<unsigned int>(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<unsigned int> appear;
for (const auto &e : left_column) {
unsigned int count =
std::count(right_column.begin(), right_column.end(), e);
appear.push_back(static_cast<unsigned int>(e) * count);
sum += static_cast<unsigned int>(e) * count;
}

sum = std::accumulate(appear.begin(), appear.end(), 0);
std::cout << "Part Two solution: sum is " << sum << std::endl;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion crates/day01/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "day01"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[lints]
Expand Down
10 changes: 4 additions & 6 deletions crates/day01/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = Vec::new();

assert!(left_column.len() == right_column.len());
#[cfg(feature = "unstable")]
Expand All @@ -20,24 +19,23 @@ 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<u32> = Vec::new();
sum = 0;
for e in &left_column {
let count: i32 = right_column
.iter()
.filter(|&n| *n == *e)
.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}");
}

Expand Down

0 comments on commit 579535c

Please sign in to comment.