Day 22: Monkey Market
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
21 Comments
Gobbel2000@programming.dev · 5 pts · 1y
Rust
Nice breather today (still traumatized from the robots). At some point I thought you had to do some magic for predicting special properties of the pseudorandom function, but no, just collect all values, have a big table for all sequences and in the end take the maximum value in that table. Part 1 takes 6.7ms, part 2 19.2ms.
::: spoiler Solution
:::
Also on github
Deebster@programming.dev · 3 pts · 1y
How have I never noticed that
scan()exists? Very handy.I liked the zipping of the offset prices, neater than my helper method.
mykl@lemmy.world · 5 pts · 1y
Uiua
It's been a while since I posted one of these, but I thought this would be straightforward in Uiua. Turns out that bitwise operations are a bit (haha) of a pain, so the
Rngoperation is very slow at 4sec for live data.I took this as an opportunity to play with the
β§(stencil)operator which probably slowed things down too.VegOwOtenks@lemmy.world · 3 pts · 1y
Haskell
I have no Idea how to optimize this and am looking forward to the other solutions that probably run in sub-single-second times. I like my solution because it was simple to write which I hadn't managed in the previous days, runs in 17 seconds with no less than 100MB of RAM.
lwhjp@lemmy.sdf.org · 3 pts · 1y
Haha, same! Mine runs in a bit under 4s compiled, but uses a similar 100M-ish peak. Looks like we used the same method.
Maybe iterate all the secrets in parallel, and keep a running note of the best sequences so far? I'm not sure how you'd decide when to throw away old candidates, though. Sequences might match one buyer early and another really late.
Deebster@programming.dev · 3 pts · 1y
Rust
Not too hard today, apart from yesterday's visit to a cocktail bar leaving me a little hazy in the mind.
::: spoiler code
:::
CameronDev@programming.dev · 2 pts · 1y
Rust
Part 2 is crazy slow, but it works, so thats cool :D
Edit: Gonna fix this, because pt2 is stupid.Much better, 2.4s. Still slow, but not 6 minutes slow.
Deebster@programming.dev · 2 pts · 1y
Six minutes? π I was feeling crappy about my 30 seconds (my naive big O cubed(?) logic means my code spends most of its time testing array equalities - 72 billion samples in the flamegraph!)
CameronDev@programming.dev · 1 pts · 1y
Most of my time is wasted on hashmap stuff. And the processing into the string, which really isnt needed anymore. :/
Deebster@programming.dev · 1 pts · 1y
Have you tried gxhash or one of the other non-cryptographic hashers?
CameronDev@programming.dev · 1 pts · 1y
I probably should give that a try. Looks like it can just drop in, so might try it later. I see FxHash is pretty popular here as well.
Zikeji@programming.dev · 2 pts · 1y
Go
Re-familiarizing myself with Go. The solution to Part 2 is fairly simply, the whole packing of the sequence into a single integer to save on memory was an optimization I did afterwards based on looking at other solutions. I thought it was cool.
LeixB@lemmy.world · 2 pts · 1y
Haskell
::: spoiler solution
:::
mykl@lemmy.world · 2 pts · 1y
Dart
Well, that was certainly a bit easier than yesterday...
I know running a window over each full list of 2000 prices rather than looking for cycles etc means I'm doing a lot of unnecessary work, but it only takes a couple of seconds, so that'll do.
GiantTree@feddit.org · 2 pts · 1y
Kotlin
I experimented a lot to improve the runtime and now I am happy with my solution. The JVM doesn't optimize code that quickly :)
I have implemented a few optimizations in regards to transformations so that they use arrays directly (The file with the implementations is here)
::: spoiler Code
:::
Acters@lemmy.world · 1 pts · 1y
Python3
Hey there lemmy, I recently transitioned from using notepad to Visual Studio Code along with running a local LLM for autocomplete(faster than copilot, big plus but hot af room)
I was able to make this python script with a bunch of fancy comments and typing silliness. I ended up spamming so many comments. yay documentation! lol
Solve time: ~3 seconds (can swing up to 5 seconds)
:::spoiler Code
:::
CameronDev@programming.dev · 1 pts · 1y
Bit odd having
main()returning an actual value, probably would have named it something else, otherwise, nicely documented solution.I bet VSC is a lot nicer to work in than notepad :D
Acters@lemmy.world · 1 pts · 1y
you have a point to call name it something else, but lazy to do that. should I simply call it
solve()maybe, that would work fine.I do want to note that having it return a value is not unheard of, it is just part of being lazy with the naming of the functions.
I definitely would not have the code outside of
main()be included in the main function as it is just something to grab the input pass it to the solver function( main in this case, but as you noted should be called something else ) and print out the results. if you imported it as a module, then you can callmain()with any input and get back the results to do whatever you want with. Just something I think makes the code better to look at and use.While doing this is highly unnecessary for these challenge, I wish to keep a little bit of proper syntax than just writing the script with everything at the top level. It feels dirty.
Coding in notepad was a bit brutal, but I stuck with notepad for years and never really cared because I copy pasta quite a bit from documentation or what not.(now a days, gpt helps me fix my shit code, now that hallucinations are reduced decently) even with VSCode, I don't pay attention to many of its features. I still kinda treat it as a text editor, but extra nagging on top.(nagging is a positive I guess, but I am an ape who gives little fucks) I do like VSCode having a workspace explorer on the side. I dislike needing to alt-tab to various open file explorer windows. Having tabs for open files is really nice, too.
VSCode is nice, and running my Qwen-coder-1.5B locally is neat for helping somethings out. Not like I rely on it for helping with coding, but rather use it for comments or sometimes I stop to think or sometimes the autocomplete is updated in realtime while I am typing. really neat stuff, I think running locally is better than copilot because of it just being more real-time than the latency with interacting with MS servers. though I do think about all the random power it is using and extra waste heat from me constantly typing and it having to constantly keep up with the new context.
The quality took a little hit with the smaller model than just copilot, but so far it is not bad at all for things I expect it to help with. It definitely is capable of helping out. I do get annoyed when the autocomplete tries too hard to help by generating a lot more stuff that I don't want.(even if the first part of what it generated is what I wanted but the rest is not) thankfully, that is not too often.
I give the local llm is helping with 70% of the comments and 15% of the code on average but it is not too consistent for the code.
For python, there is not enough syntax overhead to worry about and the autocomplete isn't needed as much.
CameronDev@programming.dev · 1 pts · 1y
Its normal for main to return a value, its just usually a status thing, rather than actual data. But given python doesn't really treat main as anything special, it hardly matters
lwhjp@lemmy.sdf.org · 1 pts · 1y
Haskell
A nice easy one today; shame I couldn't start on time. I had a go at refactoring to reduce the peak memory usage, but it just ended up a mess. Here's a tidy version.
sjmulder@lemmy.sdf.org · 1 pts · 1y
C
Really proud of this one! Started with with an O(n^atoms in the universe) scan which took 44s even after adding a dedup check.
But iterating on a trick to encode the deltas for the dedup check, using it to build a mapping table here, a lookup there etc brought it down to a very fast, fairly low memory, linear complexity solution!
::: spoiler Code
:::
day22 0m00.04s realhttps://codeberg.org/sjmulder/aoc/src/branch/master/2024/c/day22.c