cacheson avatar

cacheson

@cacheson@kbin.social
cacheson,
cacheson avatar

Nim

Hey, waitaminute, this isn't a programming puzzle. This is algebra homework!

Part 2 only required a trivial change to the parsing, the rest of the code still worked. I kept the data as singleton arrays to keep it compatible.

cacheson,
cacheson avatar

Nim

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.

cacheson,
cacheson avatar

Ended up solving it by working backwards by trying different location values and seeing if that can become a valid seed.

Huh, that's clever.

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar

Nim

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.

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar

Nim

I hope y'all like nested loops:

cacheson,
cacheson avatar

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

cacheson,
cacheson avatar

::: 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.
:::

cacheson,
cacheson avatar

Another nim person! Have you joined the community? There are dozens of us!

Here's mine (no code blocks because kbin):

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar

Oh hey, a fellow nim person. Have you joined the community?

Here's mine. Kbin doesn't even support code blocks, so using topaz:

cacheson,
cacheson avatar

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.

cacheson,
cacheson avatar
cacheson,
cacheson avatar

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • JUstTest
  • everett
  • InstantRegret
  • osvaldo12
  • magazineikmin
  • thenastyranch
  • rosin
  • Youngstown
  • mdbf
  • slotface
  • ngwrru68w68
  • khanakhh
  • kavyap
  • DreamBathrooms
  • provamag3
  • Durango
  • ethstaker
  • GTA5RPClips
  • cisconetworking
  • cubers
  • Leos
  • modclub
  • tacticalgear
  • tester
  • anitta
  • megavids
  • normalnudes
  • lostlight
  • All magazines