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
- Follow the programming.dev instance rules
- Keep all content related to advent of code in some way
- If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
- When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts
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
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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
If you're not getting that output then somethings wrong.
yeah you are right, I am passing the vector of numbers by reference, that's why my solution doesn't work.