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
[–] ImplyingImplications 1 points 2 months ago (5 children)

Are you getting any error or is there an input that isn't working as expected? I don't know Rust but I copied the logic of "calculate" into Python and it worked for my puzzle input, so I don't think the logic is incorrect. Are you sure the file is being parsed correctly?

[–] kionite231 2 points 2 months ago (4 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)

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.

load more comments (2 replies)
load more comments (2 replies)