-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
68 lines (59 loc) · 1.82 KB
/
main.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
66
67
68
#![warn(clippy::all, clippy::pedantic)]
use itertools::Itertools;
use std::cmp::Ordering;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one::<20>(data, 150));
println!("Part 2: {}", part_two::<20>(data, 150));
}
fn part_one<const N: usize>(data: &str, target: u8) -> u32 {
let mut values = [0_u8; N];
for (val, inp) in values.iter_mut().zip(data.lines()) {
*val = inp.parse().unwrap();
}
count_combs(&values, target)
}
fn part_two<const N: usize>(data: &str, target: u8) -> u32 {
let mut values = [0_u8; N];
for (val, inp) in values.iter_mut().zip(data.lines()) {
*val = inp.parse().unwrap();
}
let (count, _) = count_least_depth_combs(&values, target, 0);
count
}
fn count_combs(data: &[u8], target: u8) -> u32 {
data.iter()
.enumerate()
.map(|(i, elem)| match elem.cmp(&target) {
Ordering::Greater => 0,
Ordering::Equal => 1,
Ordering::Less => count_combs(&data[i + 1..], target - elem),
})
.sum()
}
fn count_least_depth_combs(data: &[u8], target: u8, depth: u8) -> (u32, u8) {
data.iter()
.enumerate()
.map(|(i, elem)| match elem.cmp(&target) {
Ordering::Greater => (0, u8::MAX),
Ordering::Equal => (1, depth + 1),
Ordering::Less => count_least_depth_combs(&data[i + 1..], target - elem, depth + 1),
})
.min_set_by_key(|&(_, d)| d)
.iter()
.fold((0, u8::MAX), |(acc, _), &(c, d)| (acc + c, d))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(4, part_one::<5>(data, 25));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(3, part_two::<5>(data, 25));
}
}