-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.rs
74 lines (59 loc) · 1.48 KB
/
day24.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
//! [Day 24: It Hangs in the Balance](https://adventofcode.com/2015/day/24)
use itertools::Itertools;
struct Puzzle {
packages: Vec<u64>,
}
impl Puzzle {
fn new(data: &str) -> Self {
Self {
packages: data.lines().filter_map(|s| s.parse().ok()).collect(),
}
}
fn solve(&self, ngroups: u64) -> u64 {
let weight = self.packages.iter().sum::<u64>() / ngroups;
for k in 0..self.packages.len() {
let g = self
.packages
.iter()
.combinations(k)
.filter(|x| x.iter().copied().sum::<u64>() == weight);
if let Some(m) = g.map(|p| p.iter().copied().product::<u64>()).min() {
return m;
}
}
0
}
/// Solve part one.
fn part1(&self) -> u64 {
self.solve(3)
}
/// Solve part two.
fn part2(&self) -> u64 {
self.solve(4)
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u64, u64) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test01() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part1(), 99);
}
#[test]
fn test02() {
let puzzle = Puzzle::new(TEST_INPUT);
assert_eq!(puzzle.part2(), 44);
}
}