Skip to content

Commit 2a18cb2

Browse files
committed
Add day 1 part 2
1 parent a16d35f commit 2a18cb2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/days/day01.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,26 @@ impl Day01 {
3333
let mut sorted_second_list: Vec<i64> = self.second_list.clone();
3434
sorted_first_list.sort();
3535
sorted_second_list.sort();
36+
3637
let mut result: i64 = 0;
3738
let len = sorted_first_list.len();
39+
3840
for i in 0..len {
3941
let distance: i64 = sorted_first_list[i] - sorted_second_list[i];
4042
result += distance.abs();
4143
}
44+
4245
result
4346
}
4447

4548
pub fn part2(&self) -> i64 {
46-
23
49+
let mut result = 0;
50+
51+
for item in self.first_list.iter() {
52+
let count = self.second_list.iter().filter(|&n| *n == *item).count();
53+
result += (count as i64) * item;
54+
}
55+
result
4756
}
4857
}
4958

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

0 commit comments

Comments
 (0)