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 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.