bjorng

bjorng

Erlang Core Team

Advent of Code - Day 23

Note: This topic is to talk about Day 23 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 13.

The hardest one so far. I implemented a divide and conquer algorithm. Basically, I divided the 3D space into cubes and found the cubes with the most number of bots in range. I then divided that cube into smaller cubes and so on. (Actually, I don’t explicitly sub-divide the space. Instead, I scale down the coordinates and radii for the bots. When I have found a solution for the scaled down problem, I transform the found point back to the original coordinate system.)

When I finally got it working and producing a reasonable answer, the answer was wrong.

I got the right answer when I extended the search. When I have found one cube, I added the adjacent cubes to it and then continue to sub-divide that cube.

My solution runs in about a second.

sasajuric

sasajuric

Author of Elixir In Action

I saw on reddit that most people are using the same approach. I wonder is it a general solution though? Once you pick an optimal region and even if you take adjacent regions, I still think it’s possible that the solution may lie in an intersection of discarded regions.

Since I’m not convinced that this will solve all the cases, I decided not to take that path. After some thinking, I started thinking how would I solve this problem in 1D, and then see how could this be extended to 3D. This brought me to the idea of using some sort of a line sweep (or plane sweep) to quickly find the largest set of overlapping regions. Once we have that, it takes another single pass to intersect those regions, and then I expect the resulting region would be small enough that it can be linearly scanned for the global optimum.

Unfortunately I got stuck at the very first step (line sweep). I still think the idea is sound, but I just don’t get the proper results :smiley: I think I’ll postpone part 2 for the moment and get back to it later, but I definitely want to take another stab at this.

Once I’m past the line sweep, there is still a remaining challenge of intersecting octahedrons. I feel that this could be simplified if I extrapolated to cubes instead. In fact, perhaps I should extrapolate to cubes before doing the line sweep too. The result would be a somewhat larger region, but I still hope it would be small enough to do a quick linear scan.

sasajuric

sasajuric

Author of Elixir In Action

After waking up, I thought some more about this, and I think that line sweeping won’t work for 3D. The reason is that these nanobots are octahedrons, while line sweeping would assume that the regions are cuboids. So while this approach would possibly give the correct solution for the input, I can think of cases where this approach would mistakenly pick a wrong region.

So I decided to browse the reddit comments again and see if someone came up with a more sound approach, and I think I finally found it. The idea is similar to yours (divide-and-conquer), but the important difference is that we don’t discard other choices.

So let’s say that we split the entire area into four large cubes, and then compute the amount of nanobots each cube intersects with. We’ll store all the cubes into a priority queue. Then, from the queue we pull the cube which intersects with the most nanobots. If there are multiple cubes with the same best score, we pick the one closest to the origin (0, 0, 0). We then divide that cube, compute the intersection scores for each subcube, and put the subcubes into the queue. Then rinse and repeat. The first cube of size 1 that we pull is the solution.

I currently can’t see anything wrong with this algorithm. Of course, there are some cases where this optimization won’t help (e.g. if we only have pair intersections), but I think that this algorithm would always return the correct result, no matter what the input is. I’ll turn my attention to day 24 now, and keep this in the back of my mind, but this is currently my best candidate :slight_smile:

Where Next?

Popular in Challenges Top

adamu
I said I was on a break, but I took a sneak peak and it looked fun so… Part 1 completes in half a millisecond with a single pass of the ...
New
bjorng
This topic is about Day 18 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
bjorng
Here is my solution: https://github.com/bjorng/advent-of-code-2021/blob/77bdef7a51b428b58ffb4ab76f540901e636f841/day12/lib/day12.ex
New
christhekeele
Continuation of Advent of Code 2022​:christmas_tree:, Day 1: Day 2! Leaderboard:
New
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
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
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy: Here’s my code: https://github.com/Aetherus/advent-of-...
New
New
cblavier
Hi, there :wave: Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
Aetherus
This topic is about Day 16 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement