🐮 - 2024 DAY 10 SOLUTIONS - 🐮
Day 10: Hoof It
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
C
Tried a dynamic programming kind of thing first but recursion suited the problem much better.
Part 2 seemed incompatible with my visited list representation. Then at the office I suddenly realised I just had to skip a single if(). Funny how that works when you let things brew in the back of your mind.
::: spoiler Code
::::
https://github.com/sjmulder/aoc/blob/master/2024/c/day10.c
That's a lovely minimalist solution. I couldn't even see where the solution was on my first read-through.
I bet that search would look cool visualized.
Here it is! https://sjmulder.nl/2024/aoc-day10.mp4
Oooh! Pretty!
Rust
Definitely a nice and easy one, I accidentally solved part 2 first, because I skimmed the challenge and missed the unique part.
Haskell
A nice easy one today: didn't even have to hit this with the optimization hammer.
Nim
As many others today, I've solved part 2 first and then fixed a 'bug' to solve part 1. =)
Codeberg Repo
Python
Not surprisingly, trees
yay trees! my solution was really fast too! 😀
edit: you can find it here, or look at my lemmy post
should take only 1.5 milliseconds!
Uiua
Run it here!
How to read this
Uiua has a very helpful
pathfunction built in which returns all valid paths that match your criteria (using diijkstra/a* depending on whether third function is provided), making a lot of path-finding stuff almost painfully simple, as you just need to provide a starting node and three functions: return next nodes, return confirmation if we've reached a suitable target node (here testing if it's = 9), (optional) return heuristic cost to destination (here set to constant 1), .Julia
Quite happy that today went a lot smoother than yesterday even though I am not really familiar with recursion. Normally I never use recursion but I felt like today could be solved by it (or using trees, but I'm even less familiar with them). Surprisingly my solution actually worked and for part 2 only small modifications were needed to count peaks reached by each trail.
::: spoiler Code
:::
Nice to have a really simple one for a change, both my day 1 and 2 solutions worked on their very first attempts.
I rewrote the code to combine the two though, since the implementations were almost identical for both solutions, and also to replace the recursion with a search list instead.
::: spoiler C#
:::
C#
Straightforward depth first search. I found that the only difference for part 2 was to remove the current location from the HashSet of visited locations when the recurive call finished so that it could be visited again in other unique paths.
Haskell
Cool task, nothing to optimize
Raku
Pretty straight-forward problem today.
Haskell
Rust
This was a nice one. Basically 9 rounds of Breadth-First-Search, which could be neatly expressed using
fold. The only difference between part 1 and part 2 turned out to be the datastructure for the search frontier: TheHashSetin part 1 unifies paths as they join back to the same node, theVecin part 2 keeps all paths separate.::: spoiler Solution
:::
Also on github
Python
Sets of tuples and iteration for both first and second parts. A list of tuples used as a stack for the conversion of recursion to iteration. Dictionary of legal trail moves for traversal. Type hints for antibugging in VSCode. Couple of seconds runtime for each part.
https://github.com/jdnewmil/aocpy/blob/master/aocpy%2Faoc2024%2Fday10.py
are type hints only for debugging? I never really used them.
your code was interesting, where do you think your script was taking longer than usual to solve? Does VSCode help with this?
my python script only takes 1.5 milliseconds to solve both parts.
Not "debugging" ... the value comes before I even try to run the code. The background syntax checker highlights when the types don't agree into and out of each function call and I don't get errors like trying to index into an integer.
As for time... I guessed... I did not measure. I have limited time to play with this and don't optimize unless I find myself waiting excessively for an answer.
Dart
I dug out the hill-climbing trail-walking code from 2022 Day 12, put it up on blocks, and stripped all the weirdness out of it.
Ended up with just a simple flood-fill from each trailhead, so it turns out I only actually used the Graph and Node classes which I then also stripped out...
Uiua
After finally deciding to put aside Day 9 Part 2 for now, this was really easy actually. The longest was figuring out how many extra dimensions I had to give some arrays and where to remove those again (and how). Then part 2 came along and all I had to do was remove a single character (not removing duplicates when landing on the same field by going different ways from the same starting point). Basically, everything in the parentheses of the
Trails!macro was my solution for part 1, just that the^0was◴(deduplicate). Once that was removed, the solution for part 2 was there as well.Run with example input here
Note: in order to use the code here for the actual input, you have to replace
=₈with=₅₀because I was too lazy to make it work with variable array sizes this time.Kotlin
::: spoiler Code:
:::
Very optimized python script. takes ~1.5 milliseconds for it to do both parts. [ link ]