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 4b4edb2 commit 656bd3c
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 @@ -31,17 +31,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 @@ -59,6 +68,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 656bd3c

Please sign in to comment.