-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_01.rs
65 lines (53 loc) · 1.25 KB
/
day_01.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use common::{solution, Answer};
solution!("Historian Hysteria", 1);
fn part_a(input: &str) -> Answer {
let (mut list_a, mut list_b) = parse(input);
list_a.sort();
list_b.sort();
list_a
.into_iter()
.zip(list_b)
.map(|(a, b)| a.abs_diff(b))
.sum::<u32>()
.into()
}
fn part_b(input: &str) -> Answer {
let (list_a, list_b) = parse(input);
list_a
.into_iter()
.map(|x| {
let count = list_b.iter().filter(|&&y| y == x).count();
x * count as u32
})
.sum::<u32>()
.into()
}
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
let (mut a, mut b) = (Vec::new(), Vec::new());
for x in input.lines() {
let mut parts = x.split_whitespace();
a.push(parts.next().unwrap().parse::<u32>().unwrap());
b.push(parts.next().unwrap().parse::<u32>().unwrap());
}
(a, b)
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
3 4
4 3
2 5
1 3
3 9
3 3
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 11.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 31.into());
}
}