π - 2023 DAY 15 SOLUTIONS -π
Day 15: Lens Library
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)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
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
π Thread is locked until there's at least 100 2 star entries on the global leaderboard
Edit: π Unlocked
Rust
Part 1 was super simple with
wrapping_addandwrapping_mulon au8. Building an actual hash map in Part 2 was nice.That array initialisation is pure poetry! π
I'm not fluent in Rust, but is this something like the C++ placement new? Presumably just declaring a table of Vecs won't automatically call the default constructor? (Sorry for my total ignorance -- pointers to appropriate reading material appreciated)
You can create an array filled with all the same values in Rust, but only if all values have the same memory representation because they will be copied. That just doesn't work with Vec's, because they must all have their own unique pointer. And to have uninitialized values at first (think NULL-pointers for every Vec) while creating each Vec, something like this is apparently needed.
The appropriate way would certainly have been to store the map as a
Vec>instead of an array, but I just wanted to see if could.Ah, I see! Thank you.
Scala3
C
Yes, it's a hash table. Did I pick a language with built in hash tables? Of course I didn't. Could I have used one of the many libraries implementing one? Sure. But the real question is, can we make do with stuffing things into a few static arrays at nearly zero memory and runtime cost? Yes!
In the spirit of Fred Brooks, itβll suffice here to show my data structures:
https://github.com/sjmulder/aoc/blob/master/2023/c/day15.c
Haskell
Took a while to figure out what part 2 was all about. Didn't have the energy to golf this one further today, so looking forward to seeing the other solutions!
::: spoiler Solution 0.3 line-seconds
:::
Had to take a couple days off, but this was a nice one to come back to. Will have to find some time today to go back and do one or two of the 3 that I missed. I don't have much to say about this one - I had an idea almost immediately and it worked out without much struggle. There's probably some cleaner ways to write parts of this, but I'm not too disappointed with how it turned out.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day15.rs
Dart
Just written as specced. If there's any underlying trick, I missed it totally.
9ms * 35 LOC ~= 0.35, so it'll do.
Thatβs why I normally let computers do my sums for me. Corrected now.
This felt ... too simple. I think the hardest part of part two for me was reading comprehension. My errors were typically me not reading exactly was there.
::: spoiler Python import re import math import argparse import itertools
:::
Nim
Almost caught up. Not much to say about this one. Part 1 was a freebie. Part 2 had a convoluted description, but was still pretty easy.
Nim
My whole solution can be expressed in just two words:
Ordered HashTableTotal runtime: 0.068 line-seconds (40 LOC * 1.7 ms)
Puzzle rating: exceptionally confusing description 4/10
Code: cleaned up solution with types
Snippet:
Haskell
Nice use of
foldMap!