y2015d24 pretty easy also but had to use itertools instead of my own combinations/permutations impl because of an overflow

This commit is contained in:
2024-12-03 15:28:12 +01:00
parent 900f01913f
commit dafe576fc9
7 changed files with 177 additions and 2 deletions

View File

@@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
md5 = "0.7.0"
utils = { workspace = true }
itertools = { workspace = true }

View File

@@ -0,0 +1,29 @@
1
2
3
5
7
13
17
19
23
29
31
37
41
43
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113

20
y2015/src/bin/d24.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::fs;
use y2015::days::d24;
fn main() {
part1();
part2();
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/24_input.txt")).unwrap();
println!("{}", d24::process_part1(&content));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/24_input.txt")).unwrap();
println!("{}", d24::process_part2(&content));
}

106
y2015/src/days/d24.rs Normal file
View File

@@ -0,0 +1,106 @@
use itertools::Itertools;
#[derive(Clone, Debug, Default)]
struct Group {
packages: Vec<usize>,
weight: usize,
}
impl Group {
fn new(packages: Vec<usize>) -> Self {
let mut group = Self::default();
for package in packages {
group.add(package);
}
group
}
fn add(&mut self, package: usize) {
self.packages.push(package);
self.weight += package;
}
fn qe(&self) -> usize {
self.packages.iter().product()
}
fn len(&self) -> usize {
self.packages.len()
}
}
pub fn process_part1(input: &str) -> usize {
let packages = input
.lines()
.map(|line| line.parse::<usize>().unwrap())
.collect::<Vec<usize>>();
let group_weight = packages.iter().sum::<usize>() / 3;
let mut best_group = (usize::MAX, usize::MAX);
for k in 1..packages.len() {
let combinations = packages.clone().into_iter().combinations(k);
for combination in combinations {
let group = Group::new(combination.clone());
if group.weight == group_weight {
if group.len() < best_group.0 {
best_group = (group.len(), group.qe());
}
if group.len() == best_group.0 && group.qe() < best_group.1 {
best_group = (group.len(), group.qe());
}
}
}
}
best_group.1
}
pub fn process_part2(input: &str) -> usize {
let packages = input
.lines()
.map(|line| line.parse::<usize>().unwrap())
.collect::<Vec<usize>>();
let group_weight = packages.iter().sum::<usize>() / 4;
let mut best_group = (usize::MAX, usize::MAX);
for k in 1..packages.len() {
let combinations = packages.clone().into_iter().combinations(k);
for combination in combinations {
let group = Group::new(combination.clone());
if group.weight == group_weight {
if group.len() < best_group.0 {
best_group = (group.len(), group.qe());
}
if group.len() == best_group.0 && group.qe() < best_group.1 {
best_group = (group.len(), group.qe());
}
}
}
}
best_group.1
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "1
2
3
4
5
7
8
9
10
11";
#[test]
fn part1() {
let result = process_part1(INPUT);
assert_eq!(result, 99);
}
#[test]
fn part2() {
let result = process_part2(INPUT);
assert_eq!(result, 44);
}
}

View File

@@ -26,3 +26,5 @@ pub mod d21;
pub mod d22;
pub mod d23;
pub mod d24;