this post was submitted on 31 Dec 2024
4 points (100.0% liked)

Advent Of Code

997 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] kionite231 2 points 2 months ago (2 children)

last input isn't working, for the last input answer should be true but I am getting false. here is the output:

rustc t1.rs && ./t1
190
[10, 19]
> 190 true
it's true
3267
[81, 40, 27]
> 87480 false
> 3267 true
it's true
83
[17, 5]
> 85 false
> 22 false
156
[15, 6]
> 90 false
> 21 false
7290
[6, 8, 6, 15]
> 4320 false
> 303 false
> 54 false
> 14 false
161011
[16, 10, 13]
> 2080 false
> 173 false
> 26 false
192
[17, 8, 14]
> 1904 false
> 150 false
> 25 false
21037
[9, 7, 18, 13]
> 14742 false
> 1147 false
> 81 false
> 16 false
292
[11, 6, 16, 20]
> 21120 false
> 1076 false
> 82 false
> 17 false

[–] ImplyingImplications 2 points 2 months ago (1 children)

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.

[–] kionite231 2 points 2 months 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.

[–] ImplyingImplications 2 points 2 months ago (1 children)

In case this helps too, here's the Python version of your code (with print statements removed):

def calculate(answer: int, numbers: list[int]) -> bool:
    if len(numbers) >= 2:
        tmp1 = numbers[0]
        tmp2 = numbers[1]
        numbers = numbers[2:]

        numbers.insert(0, tmp1*tmp2)

        if calculate(answer, numbers):
            return True
        else:
            numbers = numbers[1:]
            numbers.insert(0, tmp1+tmp2)
            return calculate(answer, numbers)
    else:
        return answer == numbers[0]
[–] kionite231 1 points 2 months ago

Thanks I will try to run the python version and see if it works. I think the problem is that I am passing list of numbers by reference and in python it's passed by value, this is the reason the python version works while mine doesn't.