Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? What is the total of all the name scores in the file?
Two ways of doing this: Come up with a rule that calculates numbers on the diagonal OR generate this spiral and calculate the diagonal.
I chose the latter. I initially create a grid (a square matrix) that I want to fill my numbers with. My idea was to create a function that will allow a person to traverse from the center to the last number. We have 4 different functions (Down, Left, Up, Right). For example, as soon as we hit 2 if we start from 1. We have to go up. Hence, we will use the Up() function that intakes number of spaces away from center (which is basically telling us the next size of the "square" we are making or how many numbers we have to make in this particular direction. In this case, it's 2 since we need to make 2 and 9. It also in takes the grid and the starting position. We do this until we hit 1001. Summing up the diagonal is pretty trivial.
Answer: 669171001
Runtime: 713 ms