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:
Fabian Schmidt 2024-12-03 15:28:12 +01:00
parent 0b4e2449a1
commit eae352413b
7 changed files with 177 additions and 2 deletions

17
Cargo.lock generated
View File

@ -11,10 +11,25 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "genaoc" name = "genaoc"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]] [[package]]
name = "md5" name = "md5"
version = "0.7.0" version = "0.7.0"
@ -59,12 +74,12 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "utils" name = "utils"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.plobos.xyz/projects/PuzzleUtils.git#49b0f24c1bdc2c04df237634607df15f19fb3ead"
[[package]] [[package]]
name = "y2015" name = "y2015"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"itertools",
"md5", "md5",
"utils", "utils",
] ]

View File

@ -19,4 +19,6 @@ members = [
] ]
[workspace.dependencies] [workspace.dependencies]
utils = { git = "https://git.plobos.xyz/projects/PuzzleUtils.git" } utils = { path = "../utils" }
itertools = "0.13.0"
regex = "1.11.1"

View File

@ -6,3 +6,4 @@ edition = "2021"
[dependencies] [dependencies]
md5 = "0.7.0" md5 = "0.7.0"
utils = { workspace = true } 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 d22;
pub mod d23; pub mod d23;
pub mod d24;