-
Notifications
You must be signed in to change notification settings - Fork 1
/
day7.rs
54 lines (43 loc) · 1.17 KB
/
day7.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
use aoc_2021::shared::{Day, read_input_lines};
fn main() {
Day7::new(read_input_lines(7)).run()
}
struct Day7 {
numbers: Vec<usize>,
}
impl Day for Day7 {
fn part1(&self) -> usize {
self.resolve(gap)
}
fn part2(&self) -> usize {
self.resolve(|from, dest| (0..=gap(from, dest)).sum())
}
}
impl Day7 {
fn new(input: Vec<String>) -> Day7 {
Day7 { numbers: input.join("").split(',').map(|s| s.parse::<usize>().unwrap()).collect() }
}
fn resolve(&self, fuel: fn(usize, usize) -> usize) -> usize {
let min = *self.numbers.iter().min().unwrap();
let max = *self.numbers.iter().max().unwrap();
(min..max).map(|dest| self.numbers.iter().map(|from| fuel(*from, dest)).sum()).min().unwrap()
}
}
fn gap(from: usize, dest: usize) -> usize {
(from as isize - dest as isize).abs() as usize
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_sample() {
assert_eq!(sample_day().part1(), 37)
}
#[test]
fn part2_sample() {
assert_eq!(sample_day().part2(), 168)
}
fn sample_day() -> Day7 {
Day7::new(vec![String::from("16,1,2,0,4,2,7,1,2,14")])
}
}