-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
95 lines (87 loc) · 3.19 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::cmp::min;
fn count_combinations(containers: Vec<usize>, target: usize) -> usize {
let mut sorted = containers.clone();
sorted.sort();
sorted.reverse();
fn combos(sorted_containers: &[usize], remaining: usize) -> usize {
match (0..sorted_containers.len()).find(|i| sorted_containers[*i] <= remaining) {
None => 0,
Some(i) => {
let v = sorted_containers[i];
if v == remaining {
1 + combos(&sorted_containers[(i + 1)..], remaining)
} else {
combos(&sorted_containers[(i + 1)..], remaining - v)
+ combos(&sorted_containers[(i + 1)..], remaining)
}
}
}
}
combos(&sorted[..], target)
}
fn min_container_count(containers: Vec<usize>, target: usize) -> usize {
let mut sorted = containers.clone();
sorted.sort();
sorted.reverse();
fn min_combos(sorted_containers: &[usize], remaining: usize, count: usize) -> usize {
match (0..sorted_containers.len()).find(|i| sorted_containers[*i] <= remaining) {
None => usize::MAX,
Some(i) => {
let v = sorted_containers[i];
if v == remaining {
min(
count + 1,
min_combos(&sorted_containers[(i + 1)..], remaining, count),
)
} else {
min(
min_combos(&sorted_containers[(i + 1)..], remaining - v, count + 1),
min_combos(&sorted_containers[(i + 1)..], remaining, count),
)
}
}
}
}
min_combos(&sorted[..], target, 0)
}
fn count_combinations_with_count(containers: Vec<usize>, target: usize, count: usize) -> usize {
let mut sorted = containers.clone();
sorted.sort();
sorted.reverse();
fn combos(sorted_containers: &[usize], remaining: usize, count: usize) -> usize {
match (0..sorted_containers.len()).find(|i| sorted_containers[*i] <= remaining) {
None => 0,
Some(i) => {
let v = sorted_containers[i];
if v == remaining && count == 1 {
1 + combos(&sorted_containers[(i + 1)..], remaining, count)
} else {
combos(&sorted_containers[(i + 1)..], remaining - v, count - 1)
+ combos(&sorted_containers[(i + 1)..], remaining, count)
}
}
}
}
combos(&sorted[..], target, count)
}
fn main() {
let containers = include_str!("in.txt")
.lines()
.map(|l| l.parse::<usize>().unwrap())
.collect::<Vec<usize>>();
println!("Part 1: {}", count_combinations(containers.clone(), 150));
let min_count = min_container_count(containers.clone(), 150);
dbg!(min_count);
println!(
"Part 2: {}",
count_combinations_with_count(containers.clone(), 150, min_count)
);
}
#[test]
fn test_part1() {
assert_eq!(count_combinations(vec![20, 15, 10, 5, 5], 25), 4);
}
#[test]
fn test_part2() {
assert_eq!(min_container_count(vec![20, 15, 10, 5, 5], 25), 2);
}