-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha.rs
46 lines (37 loc) · 901 Bytes
/
a.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
pub fn run() -> usize {
let input = include_str!("./input.txt");
return solve(input)
}
fn solve(input: &str) -> usize {
let mut hpos = input
.split(',')
.map(|s| s.parse::<i32>().unwrap())
.collect::<Vec<_>>();
// Determine the median from the set of horizontal positions
let midpoint = hpos.len() / 2;
let median = *hpos.select_nth_unstable(midpoint).1;
return hpos.iter()
.map(|h| {
(h - median).abs()
})
.sum::<i32>() as usize;
}
#[cfg(test)]
mod tests {
extern crate test;
use test::Bencher;
use super::*;
#[test]
fn test_example() {
let input = "16,1,2,0,4,2,7,1,2,14";
assert_eq!(solve(input), 37);
}
#[test]
fn test_puzzle() {
assert_eq!(run(), 329389);
}
#[bench]
fn bench_run(b: &mut Bencher) {
b.iter(|| run())
}
}