cacheson

joined 2 years ago
[–] cacheson@kbin.social 2 points 2 years ago (1 children)

Huh, I'm going to have to try that at some point. It's even got nim support.

Link for the lazy

[–] cacheson@kbin.social 6 points 2 years ago (1 children)

Nim

Pretty easy one today. Made a Pyramid type to hold the values and their layers of diffs, and an extend function to predict the next value. For part 2 I just had to make an extendLeft version of it that inserts and subtracts instead of appending and adding.

[–] cacheson@kbin.social 4 points 2 years ago (1 children)

Nim

I like how if you have an error in your calculations, you end up wandering the haunted desert for eternity. Very flavorful.

My solution for part 2 is pretty much the same as for part 1, just modified to work on a sequence of nodes instead of a single node. However, it doesn't find an answer in the time that I was willing to wait for it. I thought about trying to optimize it to run faster, but figured that if it was taking this long on Nim, then interpreted languages would have no chance, so that couldn't be the right approach.

I suspected that maybe the ghosts arrived at the Z locations at predictable intervals. I added some code to output the step count each time each ghost reached a Z (see commented code), and my suspicion was correct. Just needed to calculate the least common multiple of the 6 cycle lengths.

[–] cacheson@kbin.social 6 points 2 years ago* (last edited 2 years ago) (1 children)

Nim

I wrote some nice code for sorting poker hands, just defining the < and == operations for my CardSet and Hand types, and letting the standard library's sort function handle the rest.

It was quite frustrating to be told that my answer was wrong, though. I dumped the full sorted hand list and checked it manually to make sure everything was working properly, and it was. Wasted a few hours trying to figure out what was wrong. Ended up grabbing someone else's code and running it in order to compare the resulting hand list. Theirs was clearly ordered wrong, but somehow ended up with the correct answer?

Turns out that Camel Cards isn't Poker. -_-

Rather than rewrite my code entirely, I settled on some slightly ugly hacks to make it work for Camel Cards, and to handle the wildcards in part 2.

[–] cacheson@kbin.social 6 points 2 years ago (1 children)

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@kbin.social 2 points 2 years ago (1 children)

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@kbin.social 2 points 2 years ago (3 children)

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@kbin.social 2 points 2 years ago

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@kbin.social 1 points 2 years ago (1 children)

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@kbin.social 3 points 2 years ago (6 children)

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@kbin.social 3 points 2 years ago (2 children)

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@kbin.social 5 points 2 years ago (5 children)

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.

1
anime_irl (media.kbin.social)
 
2
anime_irl (media.kbin.social)
 
4
anime_irl (media.kbin.social)
 
4
anime_irl (media.kbin.social)
 
17
Big Shot! (media.kbin.social)
 
1
anime_irl (media.kbin.social)
 
217
Rule but unironically (media.kbin.social)
submitted 2 years ago* (last edited 2 years ago) by cacheson@kbin.social to c/196@lemmy.blahaj.zone
 

Liberated from some sarcastic tankie

2
anime_irl (media.kbin.social)
 
5
anime_irl (media.kbin.social)
 
2
anime_irl (media.kbin.social)
 
2
submitted 2 years ago* (last edited 2 years ago) by cacheson@kbin.social to c/Animemes@kbin.social
 
1
anime_irl (media.kbin.social)
 
view more: β€Ή prev next β€Ί