day7, part2

This commit is contained in:
Joe Ardent 2024-12-28 13:11:42 -08:00
parent b1110f2b6c
commit 342dd2982a

View file

@ -3,7 +3,7 @@ use std::collections::HashSet;
use winnow::{ use winnow::{
PResult, Parser, PResult, Parser,
ascii::{dec_uint, newline, space1}, ascii::{dec_uint, newline, space1},
combinator::{eof, opt, separated, separated_pair, seq}, combinator::{opt, separated, separated_pair, seq},
}; };
fn main() { fn main() {
@ -86,7 +86,7 @@ fn check(equation: &Equation, pt2: bool) -> bool {
} }
if process_top { if process_top {
let _ = q.pop(); let _ = q.pop();
if cur_val == target { if cur_val == target && cur_idx == (equation.factors.len() - 1) {
return true; return true;
} }
processed.insert(current); processed.insert(current);
@ -112,12 +112,10 @@ fn parse_equation(input: &mut &str) -> PResult<Equation> {
} }
fn parse(input: &str) -> Vec<Equation> { fn parse(input: &str) -> Vec<Equation> {
let (equations, _): (Vec<Equation>, _) = seq!( let (equations, _): (Vec<Equation>, _) =
separated(1.., parse_equation, newline), seq!(separated(1.., parse_equation, newline), opt(newline))
seq!(opt(newline), opt(eof)) .parse(input)
) .unwrap();
.parse(input)
.unwrap();
equations equations
} }