Advent of Code 2019 - Day 10

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

There is a private leaderboard for elixirforum members. You can join it by following this link and entering the following code:

39276-eeb74f9a

2 Likes

My solution is here. I actually documented the approach in pt1 / pt2 at the top. I won’t paste it here to avoid spoilers, it’s still early on the day.

3 Likes

Part 1 and Part 2 (most days, I clone my Part 1 solution before starting on Part 2, to preserve it as it was).

I kinda like how this one turned out. Less brute-force-y than most of my other solutions, but boy did it take a long time to get there.

1 Like

I have no idea about trigonometry. Oh well. Didn’t implement the part with looping, because the task actually didn’t ask for it (look, I can tell that won’t be necessary when my first part solution was bigger than 200).

1 Like

My solution.

1 Like

Day 10 of elixir! My solution

It looks more messy than what I originally expected. Also my solution for part 2 only works on small inputs, I might refactor it a bit

1 Like

My solution. Today’s challenge was finicky, but fun! Used a bit of trigonometry (atan2) and gb_trees for the second part.

1 Like

Here’s my solution.

My hero functions today are Enum.group_by and :math.atan2.

There’s a trick in part 2. I was about to write an algorithm to find the result. But before start I inspected my data and found 200 is not enough to finish a single round. So the process is super easy.

I’m not sure if this works for others, because not everyone’s input is the same. I guess not because one time after I provided a wrong answer, it told me that my wrong answer is someone else’s right answer.

1 Like

Not my day today…

My solution, slightly cleaned up after I got both parts working.

I figured out how to solve part 1 without using any floating point arithmetic.

After some false starts trying to solve part 2, I searched some hints on the Internet, and found out how to use atan2(). I also saw that it didn’t actually seem necessary to go round more than once, since the 200th asteroid was reached before the first round ended, so I didn’t bother implementing going round more than once.

Happy enough with my solution. I fiddled around a lot before realizing I could just calculate angles between points…

1 Like

Hi. Just joined the forum now, although I have been helped by people here for some time now. Joined the leaderboard, so figured I should be a member :slight_smile:

Anyway, here is my solution for day 10

Since there were more visible asteroids than 200, simply sorting by angle and taking the closest one works.

1 Like

High school math is needed, but I decided to avoid it for as long as I can. It takes about 2 hours to get the whole thing going (and I’ve had to spend my morning, lunch, and breaks on this!) but I end up with a fully functional solution for arbitrary-sized maps and positions. It ought to be fast enough, finding both the solution to part 1 and part 2 in under 100ms combined by doing some fancy filtering.

Source at https://gist.github.com/ferd/fa1618fbdbbfa0b4c7fb01a74d35463f

2 Likes

My solution : https://github.com/cblavier/advent/tree/master/lib/2019/day10

Wasted at least 1 hour this morning going totally wrong (I was only dealing with 8 different angles) :exploding_head:

And then I used the high school maths mentioned by @ferd :wink:

2 Likes

Finally made it… I’ve got stuck in part 2 and wasted too much time to understand what I was doing: terrible code had a toll on my brain… (like vague names and hardcoding the starting angle to :math.pi/-2 ).

For few times I went back saying “There should be an easier way…”, re-thinking the problem… but at the end the way to start (like I think many of you did) was to transform the cartesian coordinates to polar coordinates, grouping the asteroids by polar angle (calculated with math:atan2/2).

The second part takes ~70ms (macbook pro i9), it’s absolutely unoptimized, all coded to get the result and go to sleep :sleeping_bed:

1 Like

The power of polar coordinates!

Interestingly, this can be solved without atan or any other float arithmetic. Here is a StackOverflow answer with an example algorithm for sorting points (the key is cross product of vectors).

2 Likes

I’m very happy I found this forum! I just finished day 10 here the code, and the tests but it’s almost the first time I use Elixir and the Phoenix framework so my code can probably be much better