blob: d0c9bae00d5c9056c6f0ae0e810d32433b2849c3 (
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
32
33
34
35
36
37
38
39
40
41
42
|
static DAY: u8 = 1;
fn main() {
let input = advent::read_lines(DAY);
println!("{DAY}a: {}", list_distance(&input));
println!("{DAY}b: {}", 0);
}
fn list_distance(numbers: &[String]) -> u32 {
let mut list1 = Vec::new();
let mut list2 = Vec::new();
for pair in numbers {
let (number1, number2) = pair.split_once(" ").unwrap();
list1.push(number1.parse::<u32>().unwrap());
list2.push(number2.parse::<u32>().unwrap());
}
list1.sort();
list2.sort();
list1.iter()
.enumerate()
.map(|(i, number1)| number1.abs_diff(list2[i]))
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = [
"3 4",
"4 3",
"2 5",
"1 3",
"3 9",
"3 3",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(list_distance(&input), 11);
}
}
|