Woof. Part 1 was simple enough. I thought I could adapt my solution to part 2 pretty easily, just add all the values in the ranges to the starting set. Worked fine for the example, but the ranges for the actual input are too large. Ended up taking 16gb of RAM and crunching forever.
I finally abandoned my quick and dirty approach when rewriting part 2, and made some proper types and functions. Treated each range as an object, and used set operations on them. The difference operation tends to fragment the range that it's used on, so I meant to write some code to defragment the ranges after each round of mappings. Forgot to do so, but the code ran quick enough this time anyway.
Treated each range as an object, and used set operations on them
That’s smart. Honestly, I don’t understand how it works. 😅
"Set operations" should probably be in quotes. I just mean that I implemented the * (intersection) and - (difference) operators for my ValueRange type. The intersection operator works like it does for sets, just returning the overlap. The difference operator has to work a little differently, because ranges have to be contiguous, whereas sets don't, so it returns a sequence of ValueRange objects.
My ValueMapping type uses a ValueRange for it's source, so applying it to a range just involves using the intersection operator to determine what part of the range needs to move, and the difference operator to determine which parts are left.
Yeah, roughly the same idea. I guess I could have just used HSlice for my range type, I thought maybe there was some special magic to it.
It looks like your if-else ladder misses a corner case, where one range only intersects with the first or last element of the other. Switching to <= and >= for those should take care of it though.
This one was pretty simple, just parse the numbers into sets and check the size of the intersection. Part 2 just made the scoring mechanism a little more complicated.
I'm rather spoiled by python, so I feel like it could be more elegant. xD
But yeah, I do like how this one turned out, and nim runs a whole lot faster than python does. I really like nim's "method call syntax". Instead of having methods associated with an individual type, you can just call any procedure as x.f(remaining_args) to call f with x as its first argument. Makes it easy to chain procedures. Since nim is strongly typed, it'll know which procedure you mean to use by the signature.
Thank you, I'll keep those in mind. Functional programming seems interesting to me, but I don't have any practical experience with it. At some point I want to learn one of the languages that are dedicated to it. Nim does have some features for enabling a functional style, but the overall flexibility of the language probably makes it harder to learn said style.
My solution for day 1 part 1 was simple and to the point. The other ones are getting increasingly less so. You're right that sometimes it's best not to get too fancy, but I think soon I may have to break out such advanced programming techniques as "functions" and maybe "objects", instead of writing increasingly convoluted piles of nested loops. xD
Today, I continue preparations for refactoring. Szymon updated the environment and added a spoiler tag in the markdown editor. Additionally, there were several fixes and improvements related to collections and the People page....
::: spoiler Like this?
Do you mean just displaying something other than "spoiler" in the header? Or not using the word "spoiler" when writing the tag at all? If the latter, I believe it's done that way to match how it works on lemmy.
:::
I actually just learned about scanf while writing this. Only ended up using it in the one spot, since split worked well enough for the other bits. I really wanted to be able to use python-style unpacking, but in nim it only works for tuples. At least without writing macros, which I still haven't been able to wrap my head around.
Welcome everyone to the 2023 advent of code! Thank you all for stopping by and participating in it in programming.dev whether youre new to the event or doing it again....
I'm not doing anything too fancy here, just the first stuff that comes to mind and gets the job done. The filterIt template was pretty handy for part 1, though. I assume at some point in these puzzles I'll have to actually write some types and procedures instead of just using nested loops for everything.
I think it's a pretty cool language overall. I've only used it for one project so far, so there's a bunch that I still don't know. Haven't been able to wrap my head around how macros work, for example, though I've sort of figured out how to write really basic templates.
Oh wow, I guess it doesn't take too much. I copied your survey post over to r/nim with a "cross-posted from nim@programming.dev" link, and also invited the author of Enu to post here. I'll keep at it.
🌟 - 2023 DAY 6 SOLUTIONS -🌟
# Day 6: Wait for It...
anime_irl
🎁 - 2023 DAY 5 SOLUTIONS -🎁
# Day 5: If You Give a Seed a Fertilizer...
Roadmap for the technology?
I used YouTube to find out what changes are planned for the bitcoin network in 2024....
it's giving succubus
☃️ - 2023 DAY 4 SOLUTIONS -☃️
# Day 4: Scratchcards...
❄️ - 2023 DAY 3 SOLUTIONS -❄️
# Day 3: Gear Ratios...
RTR#31 Further refactoring, update Symfony to 6.4 and other deps
Today, I continue preparations for refactoring. Szymon updated the environment and added a spoiler tag in the markdown editor. Additionally, there were several fixes and improvements related to collections and the People page....
🦌 - 2023 DAY 2 SOLUTIONS -🦌
Day 2: Cube Conundrum...
🎄 - 2023 DAY 1 SOLUTIONS -🎄
Welcome everyone to the 2023 advent of code! Thank you all for stopping by and participating in it in programming.dev whether youre new to the event or doing it again....
Community health check-in | November 2023
Starting up a new monthly post where I look at the communities in the instance to see how well all of them are doing...