this post was submitted on 18 Dec 2024
17 points (94.7% liked)
Advent Of Code
1011 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
Probably the easiest optimization (which admittedly I didn't think of myself) is to work backwards: you can eliminate multiplication and concatenation early if you start with the answer and check terms from the right.
Call it optimization and people will assume it is magic. Instead I call this a simple algebra challenge.(With part two having that quirky concatenation operation)
It is like solving for x. Let's take an example:
3267: 81 40 27
If you change it to equations:
81 (+ or *) 40 (+ or *) 27 = 3267
Which effectively is:
81 (+ or *) 40 = x (+ or *) 27 = 3267
So what is the x that either add or multiply would need to create 3267? Or solve for x for only two equations.
x + 27 = 3267
->x = 3267 - 27 = 3240
x * 27 = 3267
->x = 3267 / 27 = 121
(Special note since multiplying and adding will never make a float then a division that will have a remainder(or float) is impossible for x, we can easily remove multiplying as a possible choice)
Now with our example we go plug in x:
81 (+ or *) 40 = (3240 or 121) (+ or *) 27 = 3267
So now we see if either 40 or 81 can divide or subtract from x to get the other number. Since it is easier to just program to use the next number in the list, we will use 40.
81 + 40 = 3240
->x = 3240 - 40 = 3200 != 81
81 * 40 = 3240
->x = 3240 / 40 = 81
81 + 40 = 121
->x = 121 - 40 = 81
81 * 40 = 121
->x = 121 / 40 = 3.025 != 81
This particular example from the website has two solutions.
For the concatenation operation, you simply check if the number ends with the number in the list. If not then a concatenation cannot occur, and if true remove the number from the end and continue in the list.
Call it pruning tree paths but it is simply algebraic.