🌉 - 2024 DAY 7 SOLUTIONS - 🌉
Day 7: Bridge Repair
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
Uiua
This turned out to be reasonably easy in Uiua, though this solution relies on macros which maybe slow it down.
(edit: removing one macro sped it up quite a bit)
(edit2: Letting Uiua build up an n-dimensional array turned out to be the solution, though sadly my mind only works in 3 dimensions. Now runs against the live data in around 0.3 seconds.)
Try it here
I feel like I need a Rosetta stone to read this code
Here you go
thanks that indeed is a Rosetta stone
Thanks to your solution I learned more about how to use
reduce:DMy solution did work for the example input but not for the actual one. When I went here and saw this tiny code block and you saying
I was quite taken aback. And it's so much better performance-wise too :D (well, until part 2 comes along in my case. Whatever this black magic is you used there is too high for my fried brain atm)
Haha, sorry about that, it does seem quite smug :-) I went into it expecting it to be a nightmare of boxes and dimensions, but finding it something I could deal with was a massive relief. Of course once I had a working solution I reversed it back into a multi-dimensional nightmare. That's where the performance gains came from: about 10x speedup from letting Uiua build up as many dimensions as it needed before doing a final deshaping.
I enjoyed reading a different approach to this, and thanks for reminding me that
⋕$"__"exists, that's a great idiom to have up your sleeve.Let me know if there's any bits of my solution that you'd like me to talk you through.
No worries, it does seem a lot less difficult in hindsight now, my mind just blanked at what I expected to be a lot more code :))
That performance improvement is amazing, I'll definitely take a look at how that works in detail later. Just gotta recover from the mental stretch gymnastics trying to remember the state of the stack at different code positions
Python
It is a tree search
I asked ChatGPT to explain your code and mentioned you said it was a binary search. idk why, but it output a matter of fact response that claims you were wrong. lmao, I still don't understand how your code works
I understand it now! I took your solution and made it faster. it is now like 36 milliseconds faster for me. which is interesting because if you look at the code. I dont process the entire list of integers. I sometimes stop prematurely before the next level, clear it, and add the root. I don't know why but it just works for my input and the test input.
::: spoiler code
:::
however what I notice is that the parse_input causes it to be the reason why it is slower by 20+ milliseconds. I find that even if I edited your solution like so to be slightly faster, it is still 10 milliseconds slower than mine: :::spoiler code
:::
Wow I got thrashed by chatgpt. Strictly speaking that is correct, it is more akin to Tree Search. But even then not strictly because in tree search you are searching through a list of objects that is known, you build a tree out of it and based on some conditions eliminate half of the remaining tree each time. Here I have some state space (as chatgpt claims!) and again based on applying certain conditions, I eliminate some portion of the search space successively (so I dont have to evaluate that part of the tree anymore). To me both are very similar in spirit as both methods avoid evaluating some function on all the possible inputs and successively chops off a fraction of the search space. To be more correct I will atleast replace it with tree search though, thanks. And thanks for taking a close look at my solution and improving it.
idk why my gpt decided to be like that. I expected a long winded response with a little bit of ai hallucinations. I was flabbergasted, and just had to post it.
I seemingly realized that working forward through the list of integers was inefficient for me to do, and I was using multiprocessing to do it too! which my old solution took less than 15 seconds for my input. your solution to reverse the operations and eliminate paths was brilliant and made it solve it in milliseconds without spinning up my fans, lol
Haskell
Oooh, some nice number theory going on there!
C
Big integers and overflow checking, what else is there to say 😅
::: spoiler Code
:::
https://github.com/sjmulder/aoc/blob/master/2024/c/day07.c
Uiua
Credits to @[email protected] for the approach of using
reduceand also how to split the input by multiple characters.I can happily say that I learned quite a bit today, even though the first part made me frustrated enough that I went searching for other approaches ^^
Part two just needed a simple modification. Changing how the input is parsed and passed to the adapted function took longer than changing the function itself actually.
Run with example input here
Team Uiua in the house!
(╯°□°)╯︵ ┻━┻
Hell yeah!
┻━┻︵ (°□°)/ ︵ ┻━┻
Haskell
I'm not very proud, I copied my code for part two.
If you get the right answer, it's ok 👍
Haskell
A surprisingly gentle one for the weekend! Avoiding string operations for
concatenategot the runtime down below one second on my machine.Love the fold on the list monad to apply the operations.
Since all operations increase the accumulator, I tried putting a
guard (a <= x)inapply, but it doesn't actually help all that much (0.65s -> 0.43s).0.65 -> 0.43 sounds pretty strong, isn't that a one-fourth speedup?
Edit: I was able to achieve a 30% speed improvement using this on my solution
It's not insignificant, sure, but I'd prefer 10x faster :D
Plus I'm not sure it's worth the loss of generality and readability. It is tempting to spend hours chasing this kind of optimization though!
I wanted to this the way yo did, by repeatedly applying functions, but I didn't dare to because I like to mess up and spend some minutes debugging signatures, may I ask what your IDE setup is for the LSP-Hints with Haskell?
Setting up on my PC was a little bit of a pain because it needed matching
ghcandghcideversions, so I hadn't bothered doing it on my Laptop yet.I use neovim with
haskell-tools.nvimplugin. Forghc,haskell-language-serverand others I usenixwhich, among other benefits makes my development environment reproducible and all haskellPackages are built on the same version so there are no missmatches.But, as much as I love
nix, there are probably easier ways to setup your environment.I just checked and I have haskell-tools.nvim on my PC but it somehow crashes the default config of the autocompletion for me, which I am too inexperienced to debug. I'll try it nonetheless, since I don't have autocompletion on the laptop anyways, thank you for the suggestion!
Ah, well, I have a bit of a weird setup. GHC is 9.8.4, built from git. I'm using HLS version 2.9.0.1 (again built from git) under Emacs with the LSP and flycheck packages. There are probably much easier ways of getting it to work :)
I envy emacs for all of its modes, but I don't think I'm relearning the little I know about vi. Thank you for the answer on the versions and building!
Python
Takes ~5.3s on my machine to get both outputs. Not sure how to optimize it any further other than running the math in threads? Took me longer than it should have to realize a lot of unnecessary math could be cut if the running total becomes greater than the target while doing the math. Also very happy to see that none of the inputs caused the recursive function to hit Python's max stack depth.
::: spoiler Code
:::
https://github.com/stevenviola/advent-of-code-2024
If you havent already done so, doing it in the form of "tree search", the code completes in the blink of an eye (though on a high end cpu 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz). posted the code below
Thanks! yup, I figured there would be a way. You're right, much faster, on my machine with your code, this is the speed:
I'll have to take a look to understand how that works to be better.
I posted my solution here and found my way to finish 30 milliseconds faster.(~100ms for his, and ~66 ms for mine) However, as I noted I stop prematurely sometimes. Which seems to work with my given input. but here is the one that makes sure it gets to the end of the list of integers: :::spoiler code
:::
Nim
Bruteforce, my beloved.
Wasted too much time on part 2 trying to make combinations iterator (it was very slow). In the end solved both parts with
3^nandtoTernary.Runtime: 1.5s
Codeberg repo
Java
Today was pretty easy one but for some reason I spent like 20 minutes overthinking part 2 when all it needed was one new
else if. I initially through the concatenation operator would take precedence even tho it clearly says "All operators are still evaluated left-to-right" in the instructions..I'm sure there are optimizations to do but using parallelStreams it only takes around 300ms total on my machine so there's no point really
::: spoiler The code
:::
Dart
Suspiciously easy, so let's see how tomorrow goes.... (edit: forgot to put the language! Dart for now, thinking about Uiua later)
Factor
::: spoiler spoiler
:::
Slow and dumb gets it done! I may revisit this when I give up on future days.
Lisp
Could probably go much faster if I kept track of calculations to not repeat, but 4 seconds for part 2 on my old laptop is good enough for me. Also, not really a big change from part 1 to part 2.
::: spoiler Part 1 and 2
:::
python
45s on my machine for first shot, trying to break my will to brute force 😅. I'll try improving on it in a bit after I smoke another bowl and grab another drink.
::: spoiler solution
:::
What a horrible way to name variables
It's not a long lived project, it's a puzzle, and once solved never needs to run again. My objective here is to get the correct answer, not win a style contest.
Can you provide a link to your solution? I'd like to check it out.
My initial comment was a bit harsh, I'm sorry for that. It was meant to be a bit of a joke. Anyway here's my code. Do note that I don't do the challenges timed so I have a bit more time to name my variables accordingly. Takes 35 seconds to run on a pc with a AMD Ryzen 5 5600
aeo, Killer Tofu. That's all I can think of reading this code.Made a couple of attempts to munge the input data into some kind of binary search tree, lost some time to that, then threw my hands into the air and did a more naïve sort-of breadth-first search instead. Which turned out to be better for part 2 anyway.
Also, maths. Runs in just over a hundred milliseconds when using
AsParallel, around half a second without.::: spoiler C#
That is true, I've evidently not had enough coffee yet this morning.
I'm way behind, but I'm trying to learn F#.
I'm using the library Combinatorics in dotnet, which I've used in the past, generate in this case every duplicating possibility of the operations. I the only optimization that I did was to use a function to concatenate numbers without converting to strings, but that didn't actually help much.
I have parser helpers that use ReadOnlySpans over strings to prevent unnecessary allocations. However, here I'm adding to a C# mutable list and then converting to an FSharp (linked) list, which this language is more familiar with. Not optimal, but runtime was pretty good.
I'm not terribly good with F#, but I think I did ok for this challenge.
F#
The Gen0 garbage collection looks absurd, but Gen0 is generally considered "free".
V2 - concatenate numbers did little for the runtime, but did help with Gen1 garbage, but not the overall allocation.
Julia
Took quite some time to debug but in the end I think it's a nice solution using base 2 and 3 numbers counting up to check all operator combinations.
::: spoiler Code
:::
Kotlin
I finally got around to doing day 7. I try the brute force method (takes several seconds), but I'm particularly proud of my sequence generator for operation permutations.
The
Collection#rotatemethod is in the fileUtils.kt, which can be found in my repo.::: spoiler Solution
:::