day 1 part 2
This commit is contained in:
parent
d72a5b20fc
commit
4e8ad5a56d
2 changed files with 19 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::Iter;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn solve_day_1_2() {
|
||||
// --snip--
|
||||
|
@ -18,5 +19,20 @@ pub fn solve_day_1_2() {
|
|||
)
|
||||
}).unzip();
|
||||
|
||||
l_list.sort();
|
||||
// let mut similarity = 0;
|
||||
let mut r_count_map : HashMap<i32, i32> = r_list.iter()
|
||||
.fold(HashMap::new(), |mut acc, x| {
|
||||
*acc.entry(*x).or_insert(0) += 1;
|
||||
acc
|
||||
});
|
||||
|
||||
let similarity = l_list.iter().fold(0, |mut acc, x| {
|
||||
acc += x * *r_count_map.entry(*x).or_insert(0);
|
||||
acc
|
||||
});
|
||||
|
||||
println!("{similarity}");
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -2,8 +2,10 @@ mod day_1_1;
|
|||
mod day_1_2;
|
||||
|
||||
use crate::day_1_1::solve_day_1_1;
|
||||
use crate::day_1_2::solve_day_1_2;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// solve_day_1_1();
|
||||
solve_day_1_2();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue