blob: c1648cf6c6e1e192b88a19aaa3f42ad58e99f482 (
plain)
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
|
use std::collections::HashMap;
use std::io;
fn main() -> io::Result<()> {
let (mut ls, mut rs): (Vec<u64>, Vec<u64>) = io::stdin()
.lines()
.map(|line| {
line.unwrap()
.split_whitespace()
.map(|s| s.parse().unwrap())
.collect::<Vec<u64>>()
})
.map(|xs| (xs[0], xs[1]))
.unzip();
ls.sort_unstable();
rs.sort_unstable();
let mut counts = HashMap::new();
rs.iter().for_each(|&x| {
*counts.entry(x).or_insert(0) += 1;
});
let silver: u64 = ls.iter().zip(rs).map(|(x, y)| x.abs_diff(y)).sum();
let gold: u64 = ls.iter().map(|x| x * counts.get(x).unwrap_or(&0)).sum();
println!("silver: {silver}");
println!("gold: {gold}");
return Ok(());
}
|