The Advent of Code is a global programming challenge to solve 25 puzzles each year in December. Alteryx Community members solve these puzzles in Designer and share solutions to earn badges. This article is part of a blog series that shares advanced Alteryx problem-solving techniques for these challenges and more!
The Advent of Code for Day 14, 2024 is a puzzle to calculate the position of 500 robots after a certain period when they move at the same speed from different initial positions. The field in which the robots move has a fixed width of 101 and a height of 103. And if a robot tries to move further out from the edge of the field, it will appear on the opposite side of the field, like in a computer role-playing game.
Part 1 is to calculate their positions after 100 seconds, which is a very simple physics problem about linear uniform motion. In this article, I don’t refer to this puzzle.
In Part 2, it must be solved using very limited clues. They are “Easter egg” and “picture of a Christmas tree.” So, you need to find the elapsed time for a picture of a Christmas tree to appear.
However, do you think Alteryx Designer can understand a Christmas tree? If you use image recognition AI, it could be done. Generally, the Advent of Code doesn't require such a complex solution. Actually, the author of the Advent of Code said the following:
You don't need a computer science background to participate - just a little programming knowledge and some problem-solving skills will get you pretty far. Nor do you need a fancy computer; every problem has a solution that completes in at most 15 seconds on ten-year-old hardware.
So, it should be possible to solve this puzzle with simple logic.
In this case, it is very useful to visualize the data and confirm it. The robots are in integer space, so you can only handle the x and y axes as integers. So, the Christmas tree should be like pixel art. The screenshot below visualizes the initial position of the robots.
It seems like it is randomized. I calculated all the positions up to 500 seconds later, and when I checked them, I noticed that the picture after 28 seconds was quite distinctive. Note that the data of the Advent of Code is different for each person. If you try this puzzle, that period is not the same as mine. The screenshot below was taken 28 seconds after the robot started moving.
It looks like a line object. I checked more pictures, and I found a distinctive picture 86 seconds later.
There seems to be a periodical pattern due to these screenshots. If I keep looking at more of the screenshots, I might be able to find the Christmas tree, but it would take too long, and my finger might be broken from clicking the mouse too much. So, I stopped looking at screenshots and considered some hypotheses. Here, I would think about some abstract things instead of the specific picture of the “Christmas tree.”
Based on these hypotheses, I would like to solve the puzzle in Alteryx Designer. However, how much time should I calculate? In other words, I don’t know the answer, so I don’t know how much time I should calculate. This is quite a difficult question. It will be case by case. At least, let’s create the first 100 or 1,000 seconds to confirm the pattern. If you can’t find the answer, you should create 5,000 seconds or 10,000 seconds. As for this puzzle, after 101 x 103 = 10,403 seconds, the robots will be in the first position, so you can find the answer between 1 and 10,403 seconds. In this article, I decided to do 10,000 seconds.
The concept of the workflow is to check whether all robots are adjacent to each other and to identify the time when the number of adjacent robots is the highest. The logic to create the workflow is not difficult, but there are many rows to calculate whether robots are adjacent to each other, so it takes longer to run the workflow. After running the workflow, you can see this answer. Certainly, there is a time when the number of adjacent robots is overwhelmingly high.
From this screenshot, the answer is after 7,502 seconds. The outline of the workflow is as follows.
The amount of calculation rows is 500 robots by 500 robots to check the adjacency, which is equal to 250,000 rows. Furthermore, you need to calculate for 10,000 seconds, so the final amount of calculation rows is 250,000 by 10,000, which is equal to 2.5 billion rows. After I removed unnecessary fields with the Select tool, I did that calculation by running the workflow.
However, even though there are too many rows, it finishes running after 3.5 minutes on my laptop.
When many robots are gathered in a certain area, statistically speaking, the variance or standard deviation is smaller than widely scattered. So, you can calculate the variance of the x and y values to get the answer. The results of the calculations are as follows.
Obviously, the variance at 7,502 seconds is smaller than the other variances. This workflow is very easy to create. It just summarizes variance.
It takes 12 seconds on my laptop.
So far, we know there appears to be a periodical pattern. Let’s think about it more deeply. I noted the time when the distinctive picture appeared until 500 seconds as follows.
28, 86, 129, 189, 230, 292, 331, 395, 432, 498
Calculating the difference between before and after is as follows:
I color the cells above to understand the pattern easily.
So, we can find the odd pattern is different from the even pattern, and both are distinctive. This means that the odd pattern colored by green is incremented by 2, and the even pattern colored by orange is decremented by 2. Now, you can narrow down the time when you should see the picture. Actually, you can narrow it down to around 200 pictures. But let’s move forward. Here, I add the odd pattern and even pattern as follows, which is “Sum-diff.”
Please remember the screenshots at the beginning of this article. At 28 seconds there was a vertical pattern, and at 86 seconds, there was a horizontal pattern. In this article, we will refer to the vertical pattern as Pattern B and the horizontal pattern as Pattern A.
In other words, the vertical pattern and horizontal pattern alternately appear. So, you should find the time when the 2 patterns appear at the same time to find the Christmas tree.
If you calculate these patterns more and more, you can create the table as follows, and you can find the same time that both patterns appear.
The time with green cells is when the Christmas tree appears.
Actually, this is an Excel task. Is it strictly an Alteryx solution? If you can find the patterns, you can create the workflow easily for the final calculations. You need to create the workflow to find the patterns, so I think that it is based on Alteryx.
I used a similar method to solve other puzzles of the Advent of Code. So, you can use this method for other puzzles.
What is the “Easter Egg”? It has another meaning for software. Let’s see the entry "Easter Egg" on Wikipedia.
"An Easter egg is a message, image, or feature hidden in software, a video game, a film, or another—usually electronic—medium."
In other words, an Easter Egg is a hidden feature. In this case, I think it’s an easy way to solve the puzzle. Or, the picture of the Christmas Tree is the Easter Egg itself.
Now, let’s consider the former definition (a hidden feature) of the Easter Egg. If you count the locations of the robots, you can see that every robot is in a different location only at the time when the Christmas tree is displayed. In other words, if you search for the time which every robot is in a different location, you can easily get the time when the Christmas tree is displayed. I think it would be difficult to hypothesize this feature from the picture of a Christmas tree. So, this is clearly a hidden feature that the author has put into the data; it is the Easter Egg in itself.
The workflow is as follows.
This is a very simple workflow, so it only takes 13 seconds to complete.
Please see the Christmas Tree that I could find in several ways: it appears whether I stop when the number of adjacent pixels is highest, when the variance between x and y positions is lowest, when we identify the occurrence of the overlapping patterns, or by the easter egg method.
This puzzle is to find the answer with very few hints. There are some ways to find the answer: finding the pattern in data, statistical way, and so on. In any case, you need to make hypotheses and create the workflow.
If you know what specific picture appears, you don’t need to make hypotheses and can obviously plan how to create the workflow. However, in real data analysis, such hypothesis testing is necessary. This is a very important thing: having statistical thinking and making a hypothesis. Please try the Advent of Code and develop your hypothesis-testing skills!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.