Hello,

I am trying to solve the day 7 using Rust and this is what I came up with so far:

use std::fs;

fn calculate(answer: &i32, numbers: &mut Vec<i32>) -> bool {
    if numbers.len() >= 2 {
	let tmp1 = numbers[0];
	let tmp2 = numbers[1];
	numbers.remove(0);
	numbers.remove(0);

	numbers.insert(0, tmp1 * tmp2);

	if calculate(answer, numbers) == true {
	    return true;
	} else {
	    numbers.remove(0);
	    numbers.insert(0, tmp1 + tmp2);
	    if calculate(answer, numbers) == true {
		return true;
	    } else {
		return false;
	    }
	}
    } else {
	if *answer == numbers[0] {
	    println!("> {} true", numbers[0]);
	    return true;
	} else {
	    println!("> {} false", numbers[0]);
	    return false;
	}
    }
}

fn main() {
    let contents = fs::read_to_string("sample.txt")
        .expect("Should have been able to read the file");

    for line in contents.lines() {
	let tmp = line.split(":").collect::<Vec<&str>>();
	let answer = tmp[0].to_string().parse::<i32>().unwrap();
	println!("{:?}", answer);
	let numbers_str = tmp[1].split(" ");
	let mut numbers: Vec<i32> = Vec::new();
	for num in numbers_str {
	    if num.len() == 0 {
		continue;
	    }
	    numbers.push(num.parse::<i32>().unwrap());
	}
	println!("{:?}", numbers);
	if calculate(&answer, &mut numbers) == true {
	    println!("it's true");
	}
    }
}

I don’t know why the recursion is not working. any help would be appreciated.

  • ImplyingImplications
    link
    fedilink
    arrow-up
    2
    ·
    5 days ago

    82 = 11 × 6 + 16
    Your algorithm should then multiply 82 by 20 to get 1640, but it doesn’t. It stops one number short.

    17 = 11 + 6
    Your algorithm is now stopping two numbers short.

    Looking above at the output for 292, your algorithm gives the correct answer but only checks 4 numbers. There are 8 possible combinations (2 choices between each number = 2×2×2 possible combinations), it’s not checking all of them. In fact it’s stopping short just like above.

    81 = 9 × 7 + 16
    Stopping short of multiplying 81 by 13.

    The first two numbers in each case are equivalent to what my Python version checks, but after that your algorithm no longer checks all the numbers. The array is shorter than it should be, and becomes shorter as the function is called recursively.

    I’m going to assume that Python keeps multiple versions of the array in memory and uses a different one each time calculate is called, while your Rust implementation isn’t and every call to calculate is using the same array and thats why it’s getting shorter. I don’t normally use languages where I have to manage memory so that’s just a guess!

    If it helps, the output for 292: 11 6 16 20 in Python is

    14742
    1147
    1053
    94
    3744
    301
    442
    47
    

    If you’re not getting that output then somethings wrong.

    • whoareuOP
      link
      fedilink
      arrow-up
      2
      ·
      4 days ago

      I’m going to assume that Python keeps multiple versions of the array in memory

      yeah you are right, I am passing the vector of numbers by reference, that’s why my solution doesn’t work.