Day 18 add merge (aka +) capability.
This commit is contained in:
parent
247e4a89a5
commit
a31a47b4b5
@ -35,6 +35,13 @@ fn read_byte<R: Read>(reader: &mut R) -> std::io::Result<Option<u8>> {
|
||||
}
|
||||
|
||||
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 = [
|
||||
|
||||
@ -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},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user