adventofcode

Maravedis 2024-12-21T08:14:27.483579Z

Holy puzzle batman.

Maravedis 2024-12-21T10:27:02.916409Z

For day 21 part 2*, does anyone have the number for the sample input please ?

Maravedis 2024-12-21T10:54:47.332029Z

For sample input (from reddit):

'029A': 82050061710 * 29
'980A': 72242026390 * 980
'179A': 81251039228 * 179
'456A': 80786362258 * 456
'379A': 77985628636 * 379

result: 154115708116294

tildedave 2024-12-21T21:43:48.529699Z

matches my solution too

Maravedis 2024-12-21T13:05:18.464229Z

Day 21 - Solutions

Maravedis 2024-12-21T13:06:19.647479Z

So this was a real mind bender... I have pages and pages of papers covered in symbols x) Runtime with cold caches: 3ms part1, 45ms part2. EDIT: DRY-ing

๐Ÿ‘ 1
๐Ÿ‘๐Ÿป 1
Aleks 2024-12-21T14:28:13.007229Z

After a huge mess I made it readable. It runs in 5ms (cold). https://github.com/zelark/AoC/blob/master/src/zelark/aoc_2024/day_21.clj

2024-12-21T15:44:36.187629Z

I'm just getting started. I took one look last night and realized I'd much rather do it on a saturday morning than a friday night ๐Ÿ™‚

2024-12-21T15:45:32.734279Z

I can see by the lack of responses here I made the right call ๐Ÿ™‚

๐Ÿ’ฏ 1
Maravedis 2024-12-21T16:28:33.926869Z

@erdos if you're willing and have some time, i would love some commentary on your code for today. I literally solved today and I still don't get what you're doing x) And it looks so clean and concise, too...

erdos 2024-12-21T19:04:50.351739Z

hey @malaingreclement I am happy to share here. Today took the longest for me as well, as I missed a lot of details early on. Code is on github: https://github.com/erdos/advent-of-code/blob/master/2024/day21.clj I like writing these small solutions without dependencies. I defined a bunch of helpers for this task: โ€ข code-numeric function just calculates numeric score of a code to be used later. โ€ข directional-mapping and numeric-mapping are mappings of button code to coordinates of buttons. I figured that shifting the grids so that button A gets to [0 0] would simplify things later. These could have been written programmatically but sometimes hard-coding mappings is quicker/shorter. โ€ข sequences-step function calculates the direction between two coordinates. For example, if the source is left to target, then direction is [>], if above, then [v], if both then [> v] โ€ข Function pos-step is used to move a point by a button press. โ€ข loc-sequences function calculates all the possible shortest sequences of button presses needed to get from one coordinate to another. For example, to get from [0 0] to [1 1] , it gives us ([> v] [v >]) The solution is just using dynamic programming with memoize : โ€ข If there are no robots involved and we are pressing the buttons directly, then the number of button presses equals to the length of the code. โ€ข Otherwise, the code is just a list of consecutive button presses. We have to calculate (recursively) the robot's path from A to a desired button and then press A on the level above to activate the robot. All these paths have a cost, and we are looking for the minimum of the cost. The worst thing I missed was that there are empty gaps on the keypads which are illegal positions. For the example cases, there actually exist paths that are shorter, but those start with << which would move the robot to the gap. I am filtering for these in the condition in loc-sequences . I had another implementation that actually enumerated all the paths and then selected the shortest one. It was really slow for the example cases and then I lost patience when running it for the actual input. So I rewrote it to select the shortest one with (min-key count %) but then the one it selected was not always optimal for deeper levels of recursion - this approach was good with all but the last test case, which led me down the rabbit hole for another hour of debugging.

๐Ÿ‘๐Ÿป 1
1
Maravedis 2024-12-21T20:27:31.975659Z

Thank you for this very comprehensive breakdown :) I'm really in awe at how you always seem to pick the correct data structure to attack the second part of the day.

jvuillermet 2024-12-21T21:30:10.469599Z

interesting (defn map-pairs [f s] (map f s (next s)))! Never thought about it and always reach for partition

๐Ÿ‘ 1
tildedave 2024-12-21T21:38:07.390129Z

gosh i loved this problem so much

tildedave 2024-12-21T21:42:11.534939Z

I assume I got it the intended way. reduced the key sequences to a CFG, each "step" or indirection expands the string, certain button transitions have optimal routes (e.g. going from 0 to 7 you go up up up left not up left up up), then split the string apart at the "A"s, and use the stone/lanternfish counting mechanism. my code is pretty ugly, I haven't had a chance to clean it up.

tildedave 2024-12-21T21:43:00.434419Z

https://github.com/tildedave/advent-of-code/blob/main/src/advent2024/day21.clj - I'll have to do a pass to try to clean it up later, lots of duplicated logic now.

tildedave 2024-12-21T21:51:19.479179Z

I don't really understand why in cases where there's ambiguity in terms of a "best" path (for example going from A to v you can go ^>A or >^A and they both expand to the same size at the next robot) it doesn't matter which one you choose.

Aleks 2024-12-22T04:55:30.099959Z

@tildedave because they are located at the same distance from A.

๐Ÿ‘ 1
roklenarcic 2024-12-22T06:00:01.774249Z

I thought that โ€œexplodingโ€ commands through the levels wouldnโ€™t be a viable approach but it was. I assumed that there would be an error there caused by the remaining existing state after finishing one command to prevent the next one from being so simply derived. Initially I went into a totally different direction where I wanted to translate moves from [\x \y \A \A \A \A] => [\adj-to-x \adj-dir \A \A \A \A] = cost([\y \A \A \A] => [\adj-dir \A \A \A]) + 1 and then search that space, but that had so much complexity I couldnโ€™t get it to work.

erdos 2024-12-22T06:24:34.744639Z

@jvuillermet yeah (partition 2 1 xs) is definitely better than (map f xs (next xs), I tend to forget about it and when I remember, I screw up the parameters ๐Ÿ™‚

bhauman 2024-12-22T14:32:03.733189Z

Oh my goodness Day 21 https://github.com/bhauman/adv2024/blob/main/src/adv2024/day21/sol.clj Part 2 13ms

bhauman 2024-12-22T14:34:53.984049Z

Used recursion memoization. Itโ€™s too bad bc I started going in that direction at the start but I didnโ€™t grok one certain thing about it. I did try frequencies and got it to work for the example but it ran way to long. but in the process learned more about the structure of the problem. Anyway that was an adventure.

๐Ÿ‘ 2
๐Ÿ‘๐Ÿป 1
bhauman 2024-12-22T14:35:00.771609Z

now for day 22

2024-12-23T15:29:21.927929Z

https://gitlab.com/maximoburrito/advent2024/-/blob/main/src/day21/main.clj Taking these a bit more casually... Finally catching up.

๐Ÿ™Œ๐Ÿป 1
bhauman 2024-12-21T15:14:52.380019Z

Yeah this is a PITA

Maravedis 2024-12-21T15:24:48.158489Z

Yeah. Good luck, it's definitely a Week-end Puzzle โ„ข๏ธ

bhauman 2024-12-21T15:25:41.317869Z

Hereโ€™s hoping

rjray 2024-12-21T21:03:16.231929Z

Only started on this at about 10:45 this morning (which is 13:45 past the unlocking). I'm trying to figure out why my last transform (for "029A") is always too long. The first two get the right length of strings, but the last is 2 higher.

rjray 2024-12-21T21:14:52.663959Z

AND I just figured out the "why"...

jvuillermet 2024-12-21T21:16:10.243329Z

you found the entrance to the rabbit hole, welcome

jvuillermet 2024-12-21T21:19:24.897509Z

I waste quite a bit of time thinking that In particular, if a robot arm is ever aimed at a gap where no button is present on the keypad, even for an instant, the robot will panic unrecoverably. So, don't do that didn't matter for part 1. Once I understood that, it made more sense where the complexity was lying. My first approach was a bit too naive ๐Ÿ˜