-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rs
56 lines (44 loc) · 1.3 KB
/
day17.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
//! [Day 17: No Such Thing as Too Much](https://adventofcode.com/2015/day/17)
use itertools::Itertools;
use rustc_hash::FxHashMap;
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i32, usize) {
solve_eggnot(data, 150)
}
fn solve_eggnot(data: &str, eggnot: u32) -> (i32, usize) {
// read data into an array
let values = data
.lines()
.filter_map(|line| line.parse().ok())
.collect::<Vec<u32>>();
let mut part1 = 0;
let mut part2 = FxHashMap::default();
for k in 1..=values.len() {
// try all the combinations with i containers
for combination in values.iter().combinations(k) {
let sum: u32 = combination.iter().copied().sum();
if sum == eggnot {
// part 1: count solutions
part1 += 1;
// part 2: count solutions by number of container
*part2.entry(k).or_insert(0) += 1;
}
}
}
let min_container = part2.keys().min().unwrap();
(part1, part2[min_container])
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test_solve() {
assert_eq!(solve_eggnot(TEST_INPUT, 25), (4, 3));
}
}