Check this out - fun coding challenges from December 1 to 25. I’ve done it for the past two years and it’s a cool way to learn new languages and solve puzzles.
Just wanted to share this website I found, https://adventofcode.com/ . It has programming challenges for each day up until the 25th of December (a challenge is locked until we reach that day on the calendar). For anyone interested, good luck .
For Part 1, I found in some website some math formulas that from a given number would give me the coordinates of that point, considering the number 1 as the origin. This made it super easy to calculate the distance to the center.
However in the second part, the numbers will be filled up based on the numbers that are already there, so it seems to me that brute forcing the creation of the spiral until the number we are interested in is the only way to go. What are your thoughts?
I was thinking there could be a maths solution for this, but what I did is I listed the numbers on paper and their corresponding distances from the center. Patterns emerged, and I made my solution based on those. In the end I don’t fully know why my solution works
That’s what I did. I made a function that generates the spiral with the spiral’s “radius” as input, until the number of interest was in there.
I have not even attempted part 2 yet, but here’s what I can report from part 1 of day 3:
It’s a centered octagonal number, which thankfully Wikipedia had the formula for calculating sides provided: (2n-1)^2
So, I used that formula to push these values into an array, each index representing how many levels of the square there are. So arr[0] = 1; arr[1] = 9; arr[2] = 25; arr[3] = 49 etc…
I used a while loop that kept incrementing the index by one, and just kept running the formula. I breaked out of the loop once the val was higher than the term we were searching for.
Now, since I had an array with values, and the index that represented what row we were on, I was able to use that information to calculate the size of the square (arr[index] - arr[index -1]), the size of the sides (square / 4), and the index of the middle of a given side (side / 2).
Storing the find number minus arr[index - 1] gets the total offset relative to the start of the square (FindIndex), but I need to isolate this offset relative to a side (any side). So I simply subtracted the size of a side from it until that offset was smaller than a side of the square at our level.
At this point all the data I have calculated results in the following: