Add (still messy) part 2 solution for day 18

master
Nick Krichevsky 2021-12-27 15:55:19 -05:00
parent 361a0004b4
commit bd71e6463c
3 changed files with 42 additions and 1 deletions

16
day18/Cargo.lock generated
View File

@ -12,10 +12,17 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
name = "day18"
version = "0.1.0"
dependencies = [
"itertools",
"nom",
"petgraph",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "fixedbitset"
version = "0.4.0"
@ -38,6 +45,15 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]]
name = "memchr"
version = "2.4.1"

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
nom = "7.1"
petgraph = "0.6"
itertools = "0.10"

View File

@ -1,5 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
use std::collections::HashSet;
use itertools::Itertools;
use std::env;
use std::fmt;
use std::fmt::Debug;
@ -494,6 +494,29 @@ fn part1(input_pairs: &[InputPair]) -> u32 {
problem_tree.magnitude()
}
fn part2(input_pairs: &[InputPair]) -> u32 {
input_pairs
.into_iter()
.permutations(2)
.map(|pairs| {
let pair1 = pairs[0];
let pair2 = pairs[1];
let mut tree1 = if let InputPair::Pair(left, right) = pair1 {
ProblemTree::build(&*left, &*right)
} else {
panic!("input pair should be a pair");
};
tree1.insert_root_sibling_input_pair(pair2, Direction::Right);
tree1.reduce();
tree1.magnitude()
})
.max()
.expect("should be at least one input pair")
}
fn print_tree(problem_tree: &ProblemTree) {
println!("{:?}", Dot::with_config(&problem_tree.graph, &[]));
// let mut to_visit = vec![(0, problem_tree.root_idx)];
@ -538,4 +561,5 @@ fn main() {
// .expect("did not find input problem");
println!("Part 1: {}", part1(&all_pairs));
println!("Part 2: {}", part2(&all_pairs));
}