Skip to content

Commit

Permalink
Add day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Ker committed Dec 1, 2024
1 parent a16d35f commit 2a18cb2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/days/day01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,26 @@ impl Day01 {
let mut sorted_second_list: Vec<i64> = self.second_list.clone();
sorted_first_list.sort();
sorted_second_list.sort();

let mut result: i64 = 0;
let len = sorted_first_list.len();

for i in 0..len {
let distance: i64 = sorted_first_list[i] - sorted_second_list[i];
result += distance.abs();
}

result
}

pub fn part2(&self) -> i64 {
23
let mut result = 0;

for item in self.first_list.iter() {
let count = self.second_list.iter().filter(|&n| *n == *item).count();
result += (count as i64) * item;
}
result
}
}

Expand All @@ -60,6 +69,6 @@ mod tests {
#[test]
fn result_part2() {
let day01: Day01 = Day01::new(String::from("input_examples"));
assert_eq!(day01.part2(), 23);
assert_eq!(day01.part2(), 31);
}
}

0 comments on commit 2a18cb2

Please sign in to comment.