this post was submitted on 02 Dec 2024
44 points (97.8% liked)
Advent Of Code
955 readers
55 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 | 18 | 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 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
TypeScript
Solution
God, I really wish my solutions weren't so convoluted. Also, this is an O(N^3) solution....
I don't think your solution is O(N^3). Can you explain your reasoning?
3 nested for loops
It's not as simple as that. You can have 20 nested for loops with complexity of O(1) if all of them only ever finish one iteration.
Or you can have one for loop that iterates 2^N times.
What do you think my complexity is?
I think it could be maybe O(n^2) because the other for loop which tries elements around the first error will only execute a constant of 5 times in the worst case? I'm unsure.
It's O(n).
If you look at each of the levels of all reports, you will access it a constant number of times: at most twice in each call to
EvaluateLineSafe
, and you will callEvaluateLineSafe
at most six times for each report.It really depends on what your parameter n is. If the only relevant size is the number of records (let's say that is n), then this solution takes time in O(n), because it loops over records only once at a time. This ignores the length of records by considering it constant.
If we also consider the maximum length of records (let's call it m), then your solution, and most others I've seen in this thread, has a time complexity in O(n * m^2) for part 2.