This repository was archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.rs
More file actions
98 lines (84 loc) · 2.87 KB
/
Copy pathday24.rs
File metadata and controls
98 lines (84 loc) · 2.87 KB
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
96
97
98
use itertools::Itertools;
#[allow(dead_code)]
const PACKAGES_SIMPLE: [i64; 10] = [11, 10, 9, 8, 7, 5, 4, 3, 2, 1];
#[allow(dead_code)]
const GROUP_WEIGHT_SIMPLE: i64 = 20;
#[allow(dead_code)]
fn get_group_set(packages: &[i64], group_weight: i64, group_size: usize) -> Vec<Vec<i64>> {
packages
.iter()
.copied()
.combinations(group_size)
.filter(|group| group.iter().sum::<i64>() == group_weight)
.collect()
}
#[cfg(test)]
mod solution {
use super::*;
use crate::input::get_input::get_input;
#[test]
fn balance_sleigh_simple() {
let min_group_size = 2;
let group_set = get_group_set(&PACKAGES_SIMPLE, GROUP_WEIGHT_SIMPLE, min_group_size);
let smallest_group_len = group_set.iter().min_by_key(|g| g.len()).unwrap().len();
assert_eq!(smallest_group_len, min_group_size);
// Get lowest quantum entanglement
assert_eq!(
group_set
.into_iter()
.filter(|g| g.len() == smallest_group_len)
.map(|g| g.iter().product::<i64>())
.min()
.unwrap(),
99
);
}
#[test]
fn balance_sleigh() {
let packages: Vec<i64> = get_input("packages")
.unwrap()
.lines()
.map(|p| p.parse().unwrap())
.collect();
let total_weight: i64 = packages.iter().sum();
let group_weight = total_weight / 3;
let min_group_size = 6;
let group_set = get_group_set(&packages, group_weight, min_group_size);
let smallest_group_len = group_set.iter().min_by_key(|g| g.len()).unwrap().len();
assert_eq!(smallest_group_len, min_group_size);
// Get lowest quantum entanglement
assert_eq!(
group_set
.into_iter()
.filter(|g| g.len() == smallest_group_len)
.map(|g| g.iter().product::<i64>())
.min()
.unwrap(),
11266889531
);
}
#[test]
fn balance_sleigh_4_groups() {
let packages: Vec<i64> = get_input("packages")
.unwrap()
.lines()
.map(|p| p.parse().unwrap())
.collect();
let total_weight: i64 = packages.iter().sum();
let group_weight = total_weight / 4;
let min_group_size = 5;
let group_set = get_group_set(&packages, group_weight, min_group_size);
let smallest_group_len = group_set.iter().min_by_key(|g| g.len()).unwrap().len();
assert_eq!(smallest_group_len, min_group_size);
// Get lowest quantum entanglement
assert_eq!(
group_set
.into_iter()
.filter(|g| g.len() == smallest_group_len)
.map(|g| g.iter().product::<i64>())
.min()
.unwrap(),
77387711
);
}
}