bjorng

bjorng

Erlang Core Team

Advent of Code - Day 22

Note: This topic is to talk about Day 21 of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.

Most Liked

bjorng

bjorng

Erlang Core Team

Here is my solution for Day 22.

I finished part 1 one quickly.

Embarrassingly, I then spent a lot of time on part 2, only to discover that I had a bug in the erosion level calculation. Despite the bug, I got the correct result for both the example and my input for part 1.

When I had fixed the erosion calculation bug, my code for finding the fastest path worked the first time I tried running on the example.

Then, of course, it didn’t finish running on the real input data. I then added a heuristic to prune paths that couldn’t possibly be faster than the already found path.

With that final fix, my program finished in about one and a half minutes.

Last Post!

stefanchrobot

stefanchrobot

On the flipside I went with Dijkstra straight away, but it took me a considerable amount of time to realize that it cannot be applied as-is. The reason is that the relaxation rule (dist[u] + length(u, v) < dist[v]) breaks because going along the “longer” edge (that is, switching gear) may pay off better in the long run. To solve that, one needs to do a “3D” version of the algorithm, that is, keep the minimum (tentative) distance for each combination of location and gear.

Once I got that down and the example worked, the key thing was to use priority queue in the implementation. Since there is none in the standard lib, I used a plain MapSet+Enum.min_by with the tip from Wikipedia:

Instead of filling the priority queue with all nodes in the initialization phase, it is also possible to initialize it to contain only source ; then, inside the if alt < dist[ v ] block, the node must be inserted if not already in the queue (instead of performing a decrease_priority operation)

With that, I arrived at the solution after a couple of seconds even if I enlarged the cave by plain experimentation so that I could find the globally shortest path.

Where Next?

Popular in Challenges Top

Aetherus
Finished Day 1 with Elixir :tada: Here’s my code: #!/usr/bin/env elixir defmodule Combination do @doc "Yields each combination of 2...
New
Aetherus
This topic is about Day 4 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
Aetherus
This topic is about Day 7 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
ehayun
I have 2 arrays: a1 can be any combination of value or nil like that a1 = [1,nil,3] and array 2 the same a2 = [4,2, nil] How do I com...
New
bjorng
This topic is about Day 2 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
New
rugyoga
Not the prettiest but it works https://github.com/rugyoga/aoc2023/blob/main/lib/2024/9.ex
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement