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 {
|
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 {
|
fn find_root(&self, node: &Node) -> Idx {
|
||||||
match node.parent {
|
match node.parent {
|
||||||
Some(parent_idx) => self.find_root(&self[parent_idx]),
|
Some(parent_idx) => self.find_root(&self[parent_idx]),
|
||||||
@ -195,6 +202,15 @@ mod tests {
|
|||||||
Ok(())
|
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]
|
//#[test]
|
||||||
fn test_part1() -> Result<()> {
|
fn test_part1() -> Result<()> {
|
||||||
let sum = [
|
let sum = [
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use std::{
|
pub use std::{
|
||||||
convert::Infallible,
|
convert::Infallible,
|
||||||
fmt::{Debug, Error, Formatter},
|
fmt::{Debug, Display, Error, Formatter},
|
||||||
io::Read,
|
io::Read,
|
||||||
num::ParseIntError,
|
num::ParseIntError,
|
||||||
ops::{Index, IndexMut},
|
ops::{Index, IndexMut},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user