General Discussions

Discuss a wide range of topics! Questions about the Alteryx Platform should be directed to the appropriate Product discussion forum.
It's the most wonderful time of the year - Santalytics 2020 is here! This year, Santa's workshop needs the help of the Alteryx Community to help get back on track, so head over to the Group Hub for all the info to get started!

Advent of Code - Base A Style! (10 of 26)

14 - Magnetar
14 - Magnetar

Day 10 - Monitoring Station


1.0 seconds


Yay!! A day without Intcode!! Double-yay!!! Looks like this one can be solved without a macro, as it seems pretty straightforward. Let's see......

Seeing the word map makes me think spatial tools, so decided to see where that would take me. Need to find the best location for a monitoring station in a field of asteroids, by finding the one position that can detect the most asteroids. OK, so we're looking at central points, and which have the most unique angles with asteroids in sight.

Take the data, assign a record ID to each row (which will represent the y axis, spatially), parse into more rows, and assign unique identifiers to each position in each row. If you've followed my solutions, you know I love the Tile tool for the unique ID within a group. The Tile Sequence number field will be our x for spatial purposes:


First time through, Filter for just "#", which represents the asteroids. Determine the location of each with a Create Points tool, and then Append each field onto itself (Filtering out for same x/source x & y/source y) so that we can get the angle from each point to each other point. Distance tool to calculate that angle (and distance too), and then a Unique tool to eliminate those asteroids along the same lines. Add a Summarize tool to count the asteroids along each x and y position, and then a Sort.


Nope, wrong answer. So my x & y fields must be off if the spatial comparison isn't working. First problem. My X values are starting with 1, and my Y values are starting with 0. Perhaps I should have used a Multi-Row Formula to assign the X values, but I already have the Tile tool in. So, drop in a Formula tool to change X to X-1.


Still not working. These numbers aren't really representative of Latitude and Longitude, so let me try some division. Now x is (x-1)/1000 and y is y/1000. Solution is still too high.


Next go-round I try 10000 as the divisor, and BAM, it works. This was a bit more brute force than expected, but hey, it's working now. On to part 2.

For Part 2, we have to vaporize the asteroids, starting at the asteroid monitoring station, and eliminating the closest asteroid to the station at 0 degrees, and moving around the circle as the degrees increase. For each revolution, we eliminate the closest asteroid at that angle. The goal is to find which asteroid will be eliminated at the 200th round.

Again, seems simple. Let's take that point from Part 1, use Create Points to bring back a centroid field, and append each original asteroid onto that monitoring station (eliminating the monitoring station itself). Another Distance tool, and this time we'll use both angle and distance. 

Based on the instructions, I need the X and Y to be in whole numbers again, so a Multifield Formula tool helps me there:


IF (Contains([_CurrentFieldName_],"X") OR contains([_CurrentFieldName_],"Y")) THEN [_CurrentField_]*10000 ELSE [_CurrentField_] ENDIF


Now sort on degrees and distance, ascending on both. Another Tile tool to add in that internal numbering within each Degrees value, and another Sort to reorder on the Tile Sequence number and Tile number, so with each revolution, the closest asteroid at each angle will be eliminated. Record ID tool to find the 200th record, and then a filter tool to isolate it. 




Voila!! And it fails.

Now I have to figure out why. Easiest way, with spatial, is to add browses so you can see what's going on. And the Browse tool, after the second sort, is showing me that this isn't eliminating the closest to each monitoring station first, but rather the farthest.

Easy fix. Adjust the formula tool before part 1 of my solution to change Y to -y/10000. Run to check that my first solution still works. It does.

Adjust the MultiField formula tool to use the Absolute Value of X or Y fields instead of the actual value, and then multiply by 10000.

Goal achieved. Everything works.

Again, brute force/trial and error here really paid off.


Would love to hear how you solved it. Anyone have a non-spatial solution??




Alteryx Community Team
Alteryx Community Team

Wow @EstherB47 !!! 


Your use of the Spatial Tools was the way to go!  I only used them to debug Part II.


Part I:

By only focusing on common slopes, I had to take into account the visible asteroids both in front of the current asteroid as well as behind.


Part II:

It was cool to research and find the ATAN2() function that allowed me to perform the vaporize clockwise operation!


Cheers, and Happy New Year!


Terry T