-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
37 lines (31 loc) · 1.06 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::collections::HashMap;
fn main() {
let data: Vec<(i64, i64)> = include_str!("./input.txt")
.trim()
.lines()
.map(|line| {
let mut iter = line
.split_whitespace()
.take(2)
.map(|x| x.parse().unwrap());
let left = iter.next().unwrap();
let right = iter.next().unwrap();
(left, right)
}
)
.collect();
let mut left: Vec<i64> = data.iter().map(|(first, _)| *first).collect();
let mut right: Vec<i64> = data.iter().map(|(_, second)| *second).collect();
left.sort();
right.sort();
let part1: u64 = left.iter().zip(right.iter()).map(|(a, b)| (a-b).abs() as u64).sum();
println!("part 1: {part1}");
let occurrences_right = right
.iter()
.fold(HashMap::new(), |mut acc, x| {
*acc.entry(x).or_insert(0) += 1;
acc
});
let part2: u64 = left.iter().map(|x| (occurrences_right.get(x).unwrap_or(&0) * x) as u64).sum();
println!("part 2: {part2}");
}