We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1aa85cc commit a96bc7aCopy full SHA for a96bc7a
src/aoc2024/day1.rs
@@ -1,3 +1,5 @@
1
+use std::collections::HashMap;
2
+
3
use crate::aoc2024::Aoc2024;
4
use crate::traits::days::Day1;
5
use crate::traits::ParseInput;
@@ -45,9 +47,13 @@ impl Solution<Day1> for Aoc2024 {
45
47
46
48
fn part2((left, right): &(Vec<u32>, Vec<u32>)) -> u32 {
49
let mut score = 0;
50
+ let mut counts = HashMap::new();
51
52
+ for &l in left {
53
+ let count = *counts.entry(l).or_insert_with(|| {
54
+ right.iter().filter(|&&r| r == l).count() as u32
55
+ });
56
- for l in left {
- let count = right.iter().filter(|&r| r == l).count() as u32;
57
score += l * count;
58
}
59
0 commit comments