work on exploding and splitting
This commit is contained in:
parent
63818ee2ee
commit
54053779aa
@ -11,12 +11,27 @@ enum Item {
|
||||
SubPair(Box<Pair>),
|
||||
}
|
||||
|
||||
impl Item {
|
||||
fn magnitude(&self) -> usize {
|
||||
match self {
|
||||
Item::Value(v) => *v as usize,
|
||||
Item::SubPair(pair) => pair.magnitude(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Pair {
|
||||
left: Item,
|
||||
right: Item,
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
fn magnitude(&self) -> usize {
|
||||
3 * self.left.magnitude() + 2 * self.right.magnitude()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Parser<'a> {
|
||||
chars: Chars<'a>,
|
||||
@ -25,7 +40,7 @@ struct Parser<'a> {
|
||||
impl<'a> Parser<'a> {
|
||||
fn from(src: &'a str) -> Parser<'a> {
|
||||
let mut chars = src.chars();
|
||||
chars.next(); // throw away the opening bracket
|
||||
chars.next(); // throw away the opening bracket so we don't nest too deeply
|
||||
Parser {chars}
|
||||
}
|
||||
|
||||
@ -52,21 +67,24 @@ fn load(data: &str) -> eyre::Result<Vec<Pair>>{
|
||||
let mut pairs = Vec::new();
|
||||
for line in data.lines() {
|
||||
let mut parser = Parser::from(line);
|
||||
match parser.parse() {
|
||||
Ok(pair) => pairs.push(pair),
|
||||
Err(e) => return Err(eyre!("Invalid input: {}", line)),
|
||||
}
|
||||
let pair = parser.parse()
|
||||
.map_err(|_| eyre!("Invalid input: {}", line))?;
|
||||
pairs.push(pair);
|
||||
}
|
||||
Ok(pairs)
|
||||
}
|
||||
|
||||
|
||||
const _SAMPLE: &str = "[[1,9],[8,5]]";
|
||||
const _SAMPLE: &str = "[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]";
|
||||
|
||||
pub fn run(data: &str) -> eyre::Result<(usize, usize)> {
|
||||
let pairs = load(data)?;
|
||||
dbg!(pairs.len());
|
||||
dbg!(&pairs[0]);
|
||||
// let pairs = load(data)?;
|
||||
// dbg!(pairs.len());
|
||||
// dbg!(&pairs[0]);
|
||||
|
||||
let mut parser = Parser::from(_SAMPLE);
|
||||
let root = parser.parse()?;
|
||||
dbg!(root.magnitude());
|
||||
|
||||
Ok((0, 0))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user