igorb
Advent of Code 2024 - Day 18
This puzzle was a nice break after the difficult Day 17! For part 2, I implemented a binary search to find the necessary coordinate faster, but considering the input size this was definitely not necessary. advent-of-code-2024/lib/advent_of_code2024/day18.ex at main · ibarakaiev/advent-of-code-2024 · GitHub
Most Liked
bjorng
Yes!
Having quickly solved both parts, I had time to experiment with different ways of managing the queue. My initial implementation used gb_sets; I ended up using queue.
The combined runtime for both parts is 4.5 seconds.
https://github.com/bjorng/advent-of-code/blob/main/2024/day18/lib/day18.ex
lud
Same here, solved easily, the brute force took something like 6 seconds.
Then I used binary search:
- First drop 1024 items from the walls points
- Create a base grid (with grid module) with those 1024 walls
- Then do a binary search for
nbetween1andlength(rest_of_walls). For each try:- take with
Enum.take(rest_of_walls, n)and add those walls to the grid. - return “search reasult is greater” if the path is possible
- return “…is lower” if the path is not possible
- take with
Of course there is no answer for “search result is equal” so I had to modify my binary search algorithm to detect ties and return them.
Part two is 13ms on my machine.
https://github.com/lud/adventofcode/blob/main/lib/solutions/2024/day18.ex
liamcmitchell
I thought that was long until I saw you are brute forcing part 2, no binary search.
I timed my first implementation without binary search and got 213 seconds (2013 MBP). Switched the pathfinding to use :queue like yours and got 42 seconds so much faster. Re-added binary search and it’s now 3 seconds including compilation.
https://github.com/liamcmitchell/advent-of-code/blob/main/2024/18/1.exs
Popular in Challenges
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








