From 2688d7d93d41fdd6105fc2cbdf1bebf1ad0e90c9 Mon Sep 17 00:00:00 2001 From: d2weber <29163905+d2weber@users.noreply.github.com> Date: Thu, 13 Apr 2023 19:22:41 +0200 Subject: [PATCH] Complete d16.2 --- Cargo.lock | 1 + Cargo.toml | 1 + src/d16/mod.rs | 34 ++++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b78fe43..ce036f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,7 @@ version = "0.1.0" dependencies = [ "criterion", "either", + "itertools", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6d0317e..4b7b7df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ harness = false [dependencies] either = "1.8.1" +itertools = "0.10.5" [dev-dependencies] criterion = "0.4.0" diff --git a/src/d16/mod.rs b/src/d16/mod.rs index caf3011..aec0351 100644 --- a/src/d16/mod.rs +++ b/src/d16/mod.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::{ collections::HashMap, fmt::Debug, @@ -244,15 +245,36 @@ pub mod part2 { pub fn solution(s: &str) -> u32 { let valves = parse(s); - // valves - todo!() + let unvisited: Vec = valves + .clone() + .into_iter() + .map(|(k, _)| k) + .filter(|k| *k != START_ID) + .collect(); + let mut max = 0; + let ceil_half = (unvisited.len() + 1) / 2; + for n_elephant in 0..=ceil_half { + unvisited + .clone() + .into_iter() + .combinations(n_elephant) + .for_each(|x| { + let mut my_unvisited = unvisited.clone(); + my_unvisited.retain(|k| !x.contains(k)); + let my_max = start_iteration(&valves, my_unvisited, 26); + let eleph_max = start_iteration(&valves, x, 26); + max = std::cmp::max(my_max + eleph_max, max); + }) + } + max } #[test] fn sample() { assert_eq!(solution(SAMPLE), 1707); } - // #[test] - // fn actual() { - // assert_eq!(solution(INPUT), 0); - // } + #[test] + #[ignore = "slow"] + fn actual() { + assert_eq!(solution(INPUT), 2520); + } }