From a31a47b4b5d82adf74e37481ff8a996bfa06ebbc Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Tue, 21 Dec 2021 14:07:45 -0800 Subject: [PATCH] Day 18 add merge (aka +) capability. --- 2021/src/day18.rs | 16 ++++++++++++++++ src/lib.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/2021/src/day18.rs b/2021/src/day18.rs index f248f1e..0e51b70 100644 --- a/2021/src/day18.rs +++ b/2021/src/day18.rs @@ -35,6 +35,13 @@ fn read_byte(reader: &mut R) -> std::io::Result> { } impl Tree { + fn merge(left: &Tree, right: &Tree) -> Tree { + // This is lazy but works for simple any obvious reasons (if FromStr and Display work + // correctly). + format!("[{},{}]", left, right) + .parse() + .expect("failed to parse merge tree") + } fn find_root(&self, node: &Node) -> Idx { match node.parent { Some(parent_idx) => self.find_root(&self[parent_idx]), @@ -195,6 +202,15 @@ mod tests { Ok(()) } + #[test] + fn test_merge() -> Result<()> { + assert_eq!( + Tree::merge(&"[1,2]".parse().unwrap(), &"[[3,4],5]".parse().unwrap()), + "[[1,2],[[3,4],5]]".parse().unwrap() + ); + Ok(()) + } + //#[test] fn test_part1() -> Result<()> { let sum = [ diff --git a/src/lib.rs b/src/lib.rs index e4f4209..878b20e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ pub mod prelude { pub use std::{ convert::Infallible, - fmt::{Debug, Error, Formatter}, + fmt::{Debug, Display, Error, Formatter}, io::Read, num::ParseIntError, ops::{Index, IndexMut},