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
I am not sure what is the real bug in here but I do know one thing.
you should not work forwards in the list of numbers to see if it equates.
Instead you should work from the last number back to the first one. This is because if the each number tested cannot divide without a remainder or be subtracted to 0 or above then the list of numbers is entirely invalid.
This is a state solver type problem. You are basically bruteforcing each possible state and seeing if one is valid. This is computationally expensive.
take a look at the example:
3267: 81 40 27
81 + 40 * 27
and81 * 40 + 27
both equal3267
when evaluated left-to-rightSo working backwards from the final state
3267
to the first state in the list81
:3267 / 27 = 121 - 40 = 81
and so it is valid!3267 - 27 = 3240 / 40 = 81
Also valid!but here are also examples that show being invalid:
192: 17 8 14
192 / 14 = 13.7
Invalid, skip checking the rest to save time!192 - 14 = 178 / 8 = 22.25
invalid! needs to be the same as the first number in the list and cannot have a remainder!192 - 14 = 178 - 8 = 170 != 17
invalid! needs to be the same as the first number in the list!this exhausts all paths that can be created!
wow, that's a nice idea. I will try to solve this problem with this method if mine doesn't work.