diff --git a/Cargo.lock b/Cargo.lock index 193872a..4fc2597 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,10 +11,25 @@ dependencies = [ "memchr", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "genaoc" 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]] name = "md5" version = "0.7.0" @@ -59,12 +74,12 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "utils" version = "0.1.0" -source = "git+https://git.plobos.xyz/projects/PuzzleUtils.git#49b0f24c1bdc2c04df237634607df15f19fb3ead" [[package]] name = "y2015" version = "0.1.0" dependencies = [ + "itertools", "md5", "utils", ] diff --git a/Cargo.toml b/Cargo.toml index ab40e13..5b777a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,6 @@ members = [ ] [workspace.dependencies] -utils = { git = "https://git.plobos.xyz/projects/PuzzleUtils.git" } +utils = { path = "../utils" } +itertools = "0.13.0" +regex = "1.11.1" diff --git a/y2015/Cargo.toml b/y2015/Cargo.toml index 303c651..7d7cee6 100644 --- a/y2015/Cargo.toml +++ b/y2015/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] md5 = "0.7.0" utils = { workspace = true } +itertools = { workspace = true } diff --git a/y2015/resources/24_input.txt b/y2015/resources/24_input.txt new file mode 100644 index 0000000..502fad7 --- /dev/null +++ b/y2015/resources/24_input.txt @@ -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 diff --git a/y2015/src/bin/d24.rs b/y2015/src/bin/d24.rs new file mode 100644 index 0000000..af4878c --- /dev/null +++ b/y2015/src/bin/d24.rs @@ -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)); +} diff --git a/y2015/src/days/d24.rs b/y2015/src/days/d24.rs new file mode 100644 index 0000000..2298fba --- /dev/null +++ b/y2015/src/days/d24.rs @@ -0,0 +1,106 @@ +use itertools::Itertools; + +#[derive(Clone, Debug, Default)] +struct Group { + packages: Vec, + weight: usize, +} + +impl Group { + fn new(packages: Vec) -> 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::().unwrap()) + .collect::>(); + let group_weight = packages.iter().sum::() / 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::().unwrap()) + .collect::>(); + let group_weight = packages.iter().sum::() / 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); + } +} diff --git a/y2015/src/days/mod.rs b/y2015/src/days/mod.rs index 5ef7755..164706c 100644 --- a/y2015/src/days/mod.rs +++ b/y2015/src/days/mod.rs @@ -26,3 +26,5 @@ pub mod d21; pub mod d22; pub mod d23; + +pub mod d24;