d7p2: passes test, fails run
This commit is contained in:
parent
cf586d1715
commit
b1110f2b6c
1 changed files with 21 additions and 5 deletions
|
@ -16,16 +16,21 @@ fn pt1(input: &str) -> u64 {
|
|||
let mut total = 0;
|
||||
let eqs = parse(input);
|
||||
for eq in &eqs {
|
||||
if check(eq) {
|
||||
if check(eq, false) {
|
||||
total += eq.total;
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
fn pt2(_input: &str) -> u64 {
|
||||
fn pt2(input: &str) -> u64 {
|
||||
let mut total = 0;
|
||||
|
||||
let eqs = parse(input);
|
||||
for eq in &eqs {
|
||||
if check(eq, true) {
|
||||
total += eq.total;
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
|
@ -46,7 +51,7 @@ impl Equation {
|
|||
}
|
||||
}
|
||||
|
||||
fn check(equation: &Equation) -> bool {
|
||||
fn check(equation: &Equation, pt2: bool) -> bool {
|
||||
let mut processed = HashSet::new();
|
||||
let mut q = Vec::new();
|
||||
let target = equation.total;
|
||||
|
@ -71,6 +76,13 @@ fn check(equation: &Equation) -> bool {
|
|||
process_top = false;
|
||||
q.push(plus);
|
||||
}
|
||||
if pt2 {
|
||||
let concat = (next_idx, cat(cur_val, next_val));
|
||||
if concat.1 <= target && !processed.contains(&concat) {
|
||||
process_top = false;
|
||||
q.push(concat);
|
||||
}
|
||||
}
|
||||
}
|
||||
if process_top {
|
||||
let _ = q.pop();
|
||||
|
@ -84,6 +96,10 @@ fn check(equation: &Equation) -> bool {
|
|||
false
|
||||
}
|
||||
|
||||
fn cat(a: u64, b: u64) -> u64 {
|
||||
format!("{a}{b}").parse().unwrap()
|
||||
}
|
||||
|
||||
fn parse_equation(input: &mut &str) -> PResult<Equation> {
|
||||
let (total, factors) = separated_pair(
|
||||
dec_uint,
|
||||
|
@ -127,6 +143,6 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn p2() {
|
||||
assert_eq!(0, pt2(TEST_INPUT));
|
||||
assert_eq!(11387, pt2(TEST_INPUT));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue