Day 13: Claw Contraption
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
29 Comments
mykl@lemmy.world · 7 pts · 1y
I have nothing. I hate Diophantine equations. That is all I have to say today.
(edit) I came back to it after a 24 hour break. All that nonsense about scoring multiple results really took me in yesterday.
hades@lemm.ee · 3 pts · 1y
mykl@lemmy.world · 3 pts · 1y
I thought that would be easier but then ended up with that monstrous function, but that's all that's left of yesterday's terrible mess, so it stays :-)
VegOwOtenks@lemmy.world · 4 pts · 1y
Haskell
Pen and Paper solved these equations for me.
(Edit: coding style)
Acters@lemmy.world · 3 pts · 1y
Python
Execution time: ~<1 millisecond (800 microseconds on my machine)
Good old school linear algebra from middle school. we can solve this really really fast. With minimal changes from part 1!
:::spoiler FastCode [ paste ]
:::
Sparrow_1029@programming.dev · 2 pts · 1y
This is a really excellent, clean solution! Would you mind breaking down how the piece of linear algebra works (for a shmo like me who doesn't remember that stuff frum school heh π )
Acters@lemmy.world · 4 pts · 1y
https://lemmy.world/comment/13950499
take the two equations, solve for y, and make sure y is fully divisible.
hades@lemm.ee · 2 pts · 1y
Acters@lemmy.world · 3 pts · 1y
we are solving for y first. If there is a y then x is found easily.
(Ax)*x + (Bx)*y = Pxand(Ay)*x + (By)*y = PyBecause of Ax or Ay and Bx or By, lets pretend that they are not
(A*x)*xand(A*y)*yfor both. they are just names. could be rewritten as:(Aleft)*x + (Bleft)*y = Pleftand(Aright)*x + (Bright)*y = Prightbut I will keep them short. solving for y turns into this:
y = (Ay*Px - Ax*Py) / (Ay*Bx - Ax*By)if mod of 1 is equal to 0 then there is a solution. We can be confident that x is also a solution, too. Could there be an edge case? I didn't proof it, but it works flawlessly for my solution.
Thankfully, divmod does both division and mod of 1 at the same time.
Sparrow_1029@programming.dev · 2 pts · 1y
Thank you so much for your explanation! I kind of found some clues looking up perp dot products & other vector math things, but this breaks it down very nicely.
I implemented your solution in rust, and part 2 failed by +447,761,194,259 (this was using signed 64-bit integers,
i64). When I changed it to use signed 64 bit floating-pointf64and checked that the solution forxproduces a whole number it worked.Acters@lemmy.world · 3 pts · 1y
Did you run my python code as is? I would hope it works for everyone. though, I am unsure what the edge cases are, then.
Sparrow_1029@programming.dev · 1 pts · 1y
I did run your code as-is in an ipython REPL to check. These were the results:
::: spoiler REPL session
:::
If you're curious to check against my puzzle input, it's here
Thank you again for the back & forth, and for sharing your solution!
Acters@lemmy.world · 2 pts · 1y
there is exactly ONE "machine" that causes your result to be incorrect. ONLY for part 2.
I see now what your corner case causes. so when my script solves for y first. it will be exact. BUT when you solve for x, it will be not divisible... makes sense now. I didn't expect this. This only occurs because of part 2! so dastardly. well, that was interesting. I guess I am forced to add that extra check... rip those microsecond gains.
Sparrow_1029@programming.dev · 1 pts · 1y
Ooh that is tricky of them. Good catch!
VegOwOtenks@lemmy.world · 1 pts · 1y
They do, if the remainder returned by divmod(...) wasn't zero then it wouldn't be divisble
Acters@lemmy.world · 2 pts · 1y
you are right, we solve for y, but I am confident that solving for x after y would yield the correct result as long as y is fully divisible.
lwhjp@lemmy.sdf.org · 3 pts · 1y
Haskell
Whee, linear algebra! Converting between numeric types is a bit annoying in Haskell, but I'm reasonably happy with this solution.
Gobbel2000@programming.dev · 2 pts · 1y
Rust
This problem is basically a linear system, which can be solved by inverting the 2x2 matrix of button distances. I put some more detail in the comments.
::: spoiler Solution
:::
Also on github
iAvicenna@lemmy.world · 2 pts · 1y
Python
I just threw linear algebra and float64 on this question and it stuck. Initially in order to decrease the numbers a bit (to save precision) I tried to find greatest common divisors for the coordinates of the target but in many cases it was 1, so that was that went down the drain. Luckily float64 was able to achieve precisions up to 1e-4 and that was enough to separate wheat from chaff. So in the end I did not have to use exact formulas for the inverse of the matrix though probably would be a more satisfying solution if I did.
mykl@lemmy.world · 2 pts · 1y
Uiua
Pretty much just a transcription of my Dart solution.
Quant@programming.dev · 2 pts · 1y
Welp, got frustrated again with part one because there kept being something wrong with my totally-not-ugly loop and so came here again. I did have to change
IsInt(and thus alsoCostto account for different handling) for part two though because I kept getting wrong results for my input.I'm guessing it's because uiua didn't see the difference between rounded and non-rounded number anymore.
Here's the updated, slightly messier version of the two functions that worked out for me in the end :D
CouldhavebeendonebetterbutI'mlackingthepatienceforthatnowmykl@lemmy.world · 2 pts · 1y
Yeah, I had to fiddle with that limit before it actually worked for me, so it's clearly quite sensitive to the data :-)
hades@lemm.ee · 2 pts · 1y
CameronDev@programming.dev · 2 pts · 1y
Rust
Hardest part was parsing the input, i somehow forgot how regexes work and wasted hours.
Learning how to do matrix stuff in rust was a nice detour as well.
gentooer@programming.dev · 2 pts · 1y
Haskell, 14 ms. The hardest part was the parser today. I somehow thought that the buttons could have negative values in X or Y too, so it's a bit overcomplicated.
CameronDev@programming.dev · 3 pts · 1y
I wasted hours on the parsing, because my regex
([0-9]*)was giving me empty strings. Made me feel very dumb when I worked it outlwhjp@lemmy.sdf.org · 2 pts · 1y
Oops! Took me a while to spot it from your comment, too, so don't feel too bad :)
sjmulder@lemmy.sdf.org · 2 pts · 1y
C
"The cheapest way" "the fewest tokens", that evil chap!
I'm on a weekend trip and thought to do the puzzle in the 3h train ride but I got silly stumped on 2D line intersection*, was too stubborn to look it up, and fell asleep π€‘
When I woke up, so did the little nugget of elementary algebra somewhere far in the back of my mind. Tonight I finally got to implementing, which was smooth sailing except for this lesson I learnt:
*) on two parts:
::: spoiler Code
:::
https://github.com/sjmulder/aoc/blob/master/2024/c/day13.c
lwhjp@lemmy.sdf.org · 1 pts · 1y
Line intersection is a nice way of looking at it. My immediate thought was "change of basis".
SteveDinn@lemmy.ca · 2 pts · 1y
C#
Thank goodness for high school algebra!
janAkali@lemmy.one · 1 pts · 1y
Nim
I'm embarrasingly bad with math. Couldn't have solved this one without looking up the solution. =C
RagingHungryPanda@lemm.ee · 1 pts · 1y
I had to borrow some of y'alls code to finish part 2. My approach and all the attempts I did at trying to get the slopes of lines and such couldn't get it high enough.
I thought to move from the prize along it's furthest away axis until I got to the intersection of the slope of the two buttons.
I thought that maybe that wasn't the cheapest way, so I fiddled around with the order of button A and B, but still couldn't get it high enough.
It looks like this group was mostly doing the cross product of the prize to the two buttons. I'm too dumb to realize how that would work. I thought you'd want to move along the furthest away axis first, but maybe it doesn't matter.
If anyone can see if I did anything obviously wrong, I'd appreciate it. I normally don't like to take code without fixing whatever it is I was doing since it always bites me in the butt later, but here I am.
F#
::: spoiler expand for source
:::