# Advent of Code: Day 3

It’s day 3 of the Advent of Code, so day 3 of my set-based approach to solving the challenges.

The Challenge

The challenge today was the Spiral Memory challenge.

The first challenge was, given a number, to determine the number of steps from the centre of the grid to that number. It’s pretty clear that this problem is not really suited to a set-based approach, but I did try to use it anyway.

The first thing I noticed was that the diagonal starting in the middle of the grid, and going down and right, was the squares of the odd numbers (1…9…25…). I decided to use this fact to determine which “spiral” my puzzle input could be found in. In my case, the answer was 515. This meant that my square would extend 257 steps in each direction from the starting block. I decided to work with the co-ordinates of each square, starting at (0,0).

I created a table to store the co-ordinates of the number in the square. I then built only the outside spiral, by using my tally table again (see Day 1 for more info). The right hand side of the grid had a y-coordinate of 257, and starts with an x-coordinate of 257 and moves to -257. The other sides all follow a similar pattern, having one static coordinate, and one which moves. By starting with the square of the previous odd number (513), I could easily work out what position my puzzle input was found at. Calculating the distance to (0,0) was the pretty simple, it was just the absolute value of my x-coordinate plus the absolute value of the y-coordinate.

So that worked pretty well, and it unlocked part 2 of the question.

And… the question needed a totally different solution. Where the previous part only required 1 value, this part needed to know all the cells around it. So I would have to change my method entirely.

I used a similar idea, but I couldn’t insert entire rows at once, I had to load each value individually so I could calculate the value of the cells around it. The only benefit that set-based thinking was that it was super easy to calculate the value of the blocks that were already “filled”.

I haven’t thought of a better method to solve the problem, but this still feels very clunky. If you have a better solution, please feel free to point it out to me in the comments.

Hopefully tomorrow is better suited to set based thinking. (Spoiler – it is)