Part of Advent of Code Golf 2021 event. See the linked meta post for details.
Related to AoC2017 Day 3, Part 2.
You come across an experimental new kind of memory stored on an infinite two-dimensional grid.
Each square on the grid is allocated in a spiral pattern starting at a location marked 1 and then counting up while spiraling outward. For example, the first few squares are allocated like this:
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23---> ...
As a stress test on the system, the programs here clear the grid and then store the value 1 in square 1. Then, in the same allocation order as shown above, they store the sum of the values in all adjacent squares, not including diagonals.
So, the first few squares' values are chosen as follows:
- Square 1 starts with the value 1.
- Square 2 has only one adjacent filled square (with value 1), so it also stores 1.
- Square 3 is the same (diagonal neighbors don't count), so it also stores 1.
- Square 4 has squares 1 and 3 as neighbors and stores the sum of their values, 2.
- Square 5 has square 4 as its only neighbor, so it gets the value 2.
Once a square is written, its value does not change. Therefore, the first few squares would receive the following values:
12 12 10 8 7
14 2 2 1 7
17 3 1 1 6
20 3 4 5 5
20 23 27---> ...
What is the first value written that is at least as large as the input (a positive integer)?
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
1 -> 1
2 -> 2
9 -> 10
18 -> 20
50 -> 55
100 -> 111
200 -> 214
500 -> 552
1000 -> 1070
1070 -> 1070