5 You come across an experimental new kind of memory stored on an infinite two-dimensional grid.
7 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:
15 While this is very space-efficient (no squares are skipped), requested data must be carried back to square 1 (the location of the only access port for this memory system) by programs that can only move up, down, left, or right. They always take the shortest path: the Manhattan Distance between the location of the data and square 1.
19 * Data from square 1 is carried 0 steps, since it's at the access port.
20 * Data from square 12 is carried 3 steps, such as: down, left, left.
21 * Data from square 23 is carried only 2 steps: up twice.
22 * Data from square 1024 must be carried 31 steps.
24 How many steps are required to carry the data from the square identified in your puzzle input all the way to the access port?
28 I freely admit that I worked out the program I wanted to write using graph paper and some Python doodles. There's no point in trying to write a Joy program until I'm sure I understand the problem well enough.
30 The first thing I did was to write a column of numbers from 1 to n (32 as it happens) and next to them the desired output number, to look for patterns directly:
65 There are four groups repeating for a given "rank", then the pattern enlarges and four groups repeat again, etc.
73 Four of this pyramid interlock to tile the plane extending from the initial "1" square.
77 10 11 12 13|14 15 16 17|18 19 20 21|22 23 24 25
81 We can figure out the pattern for a row of the pyramid at a given "rank" $k$:
83 $2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k$
87 $k + (k - 1), k + (k - 2), ..., k, k + 1, k + 2, ..., k + k$
89 This shows that the series consists at each place of $k$ plus some number that begins at $k - 1$, decreases to zero, then increases to $k$. Each row has $2k$ members.
91 Let's figure out how, given an index into a row, we can calculate the value there. The index will be from 0 to $k - 1$.
93 Let's look at an example, with $k = 4$:
103 Subtract $k$ from the index and take the absolute value:
107 for n in range(2 * k):
121 Not quite. Subtract $k - 1$ from the index and take the absolute value:
125 for n in range(2 * k):
126 print(abs(n - (k - 1)), end=' ')
131 Great, now add $k$...
135 for n in range(2 * k):
136 print(abs(n - (k - 1)) + k, end=' ')
141 So to write a function that can give us the value of a row at a given index:
146 i %= (2 * k) # wrap the index at the row boundary.
147 return abs(i - (k - 1)) + k
153 for i in range(2 * k):
154 print(row_value(k, i), end=' ')
159 (I'm leaving out details of how I figured this all out and just giving the relevent bits. It took a little while to zero in of the aspects of the pattern that were important for the task.)
161 ### Finding the rank and offset of a number.
162 Now that we can compute the desired output value for a given rank and the offset (index) into that rank, we need to determine how to find the rank and offset of a number.
164 The rank is easy to find by iteratively stripping off the amount already covered by previous ranks until you find the one that brackets the target number. Because each row is $2k$ places and there are $4$ per rank each rank contains $8k$ places. Counting the initial square we have:
166 $corner_k = 1 + \sum_{n=1}^k 8n$
168 I'm not mathematically sophisticated enough to turn this directly into a formula (but Sympy is, see below.) I'm going to write a simple Python function to iterate and search:
172 def rank_and_offset(n):
173 assert n >= 2 # Guard the domain.
174 n -= 2 # Subtract two,
175 # one for the initial square,
176 # and one because we are counting from 1 instead of 0.
179 m = 8 * k # The number of places total in this rank, 4(2k).
181 return k, n % (2 * k)
182 n -= m # Remove this rank's worth.
188 for n in range(2, 51):
189 print(n, rank_and_offset(n))
245 for n in range(2, 51):
246 k, i = rank_and_offset(n)
247 print(n, row_value(k, i))
301 ### Putting it all together
306 return abs(i - (k - 1)) + k
309 def rank_and_offset(n):
310 n -= 2 # Subtract two,
311 # one for the initial square,
312 # and one because we are counting from 1 instead of 0.
315 m = 8 * k # The number of places total in this rank, 4(2k).
317 return k, n % (2 * k)
318 n -= m # Remove this rank's worth.
325 k, i = rank_and_offset(n)
326 return row_value(k, i)
355 aoc20173(23000000000000)
365 # Sympy to the Rescue
366 ### Find the rank for large numbers
367 Using e.g. Sympy we can find the rank directly by solving for the roots of an equation. For large numbers this will (eventually) be faster than iterating as `rank_and_offset()` does.
371 from sympy import floor, lambdify, solve, symbols
372 from sympy import init_printing
383 $1 + 2 + 3 + ... + N = \frac{N(N + 1)}{2}$
387 $\sum_{n=1}^k 8n = 8(\sum_{n=1}^k n) = 8\frac{k(k + 1)}{2}$
393 E = 2 + 8 * k * (k + 1) / 2 # For the reason for adding 2 see above.
401 $\displaystyle 4 k \left(k + 1\right) + 2$
405 We can write a function to solve for $k$ given some $n$...
410 return floor(max(solve(E - n, k))) + 1
413 First `solve()` for $E - n = 0$ which has two solutions (because the equation is quadratic so it has two roots) and since we only care about the larger one we use `max()` to select it. It will generally not be a nice integer (unless $n$ is the number of an end-corner of a rank) so we take the `floor()` and add 1 to get the integer rank of $n$. (Taking the `ceiling()` gives off-by-one errors on the rank boundaries. I don't know why. I'm basically like a monkey doing math here.) =-D
415 It gives correct answers:
419 for n in (9, 10, 25, 26, 49, 50):
431 And it runs much faster (at least for large numbers):
435 %time rank_of(23000000000000) # Compare runtime with rank_and_offset()!
438 CPU times: user 27.8 ms, sys: 5 µs, total: 27.8 ms
445 $\displaystyle 2397916$
451 %time rank_and_offset(23000000000000)
454 CPU times: user 216 ms, sys: 89 µs, total: 216 ms
461 $\displaystyle \left( 2397916, \ 223606\right)$
465 After finding the rank you would still have to find the actual value of the rank's first corner and subtract it (plus 2) from the number and compute the offset as above and then the final output, but this overhead is partially shared by the other method, and overshadowed by the time it (the other iterative method) would take for really big inputs.
467 The fun thing to do here would be to graph the actual runtime of both methods against each other to find the trade-off point.
469 ### It took me a second to realize I could do this...
470 Sympy is a *symbolic* math library, and it supports symbolic manipulation of equations. I can put in $y$ (instead of a value) and ask it to solve for $k$.
479 g, f = solve(E - y, k)
482 The equation is quadratic so there are two roots, we are interested in the greater one...
492 $\displaystyle - \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
504 $\displaystyle \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
508 Now we can take the `floor()`, add 1, and `lambdify()` the equation to get a Python function that calculates the rank directly.
518 $\displaystyle \left\lfloor{\frac{\sqrt{y - 1}}{2} - \frac{1}{2}}\right\rfloor + 1$
524 F = lambdify(y, floor(f) + 1)
529 for n in (9, 10, 25, 26, 49, 50):
545 %time int(F(23000000000000)) # The clear winner.
548 CPU times: user 60 µs, sys: 4 µs, total: 64 µs
555 $\displaystyle 2397916$
559 Knowing the equation we could write our own function manually, but the speed is no better.
563 from math import floor as mfloor, sqrt
566 return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
571 %time mrank_of(23000000000000)
574 CPU times: user 7 µs, sys: 1 µs, total: 8 µs
581 $\displaystyle 2397916$
585 ### Given $n$ and a rank, compute the offset.
587 Now that we have a fast way to get the rank, we still need to use it to compute the offset into a pyramid row.
592 return (n - 2 + 4 * k * (k - 1)) % (2 * k)
595 (Note the sneaky way the sign changes from $k(k + 1)$ to $k(k - 1)$. This is because we want to subract the $(k - 1)$th rank's total places (its own and those of lesser rank) from our $n$ of rank $k$. Substituting $k - 1$ for $k$ in $k(k + 1)$ gives $(k - 1)(k - 1 + 1)$, which of course simplifies to $k(k - 1)$.)
599 offset_of(23000000000000, 2397916)
605 $\displaystyle 223606$
609 So, we can compute the rank, then the offset, then the row value.
614 return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
618 return (n - 2 + 4 * k * (k - 1)) % (2 * k)
622 return abs(i - (k - 1)) + k
628 return row_value(k, i)
657 aoc20173(23000000000000)
663 $\displaystyle 4572225$
669 %time aoc20173(23000000000000000000000000) # Fast for large values.
672 CPU times: user 22 µs, sys: 2 µs, total: 24 µs
679 $\displaystyle 2690062495969$
684 At this point I feel confident that I can implement a concise version of this code in Joy. ;-)
688 from notebook_preamble import J, V, define
697 The translation is straightforward.
699 int(floor(sqrt(n - 1) / 2 - 0.5) + 1)
701 rank_of == -- sqrt 2 / 0.5 - floor ++
705 define('rank_of -- sqrt 2 / 0.5 - floor ++')
714 (n - 2 + 4 * k * (k - 1)) % (2 * k)
735 offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %
739 define('offset_of dup 2 * [dup -- 4 * * 2 + -] dip %')
759 define('row_value over -- - abs +')
769 n k [offset_of] dupdip
777 define('aoc2017.3 dup rank_of [offset_of] dupdip swap row_value')
798 V('23000000000000 aoc2017.3')
801 • 23000000000000 aoc2017.3
802 23000000000000 • aoc2017.3
803 23000000000000 • dup rank_of [offset_of] dupdip swap row_value
804 23000000000000 23000000000000 • rank_of [offset_of] dupdip swap row_value
805 23000000000000 23000000000000 • -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
806 23000000000000 22999999999999 • sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
807 23000000000000 4795831.523312615 • 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
808 23000000000000 4795831.523312615 2 • / 0.5 - floor ++ [offset_of] dupdip swap row_value
809 23000000000000 2397915.7616563076 • 0.5 - floor ++ [offset_of] dupdip swap row_value
810 23000000000000 2397915.7616563076 0.5 • - floor ++ [offset_of] dupdip swap row_value
811 23000000000000 2397915.2616563076 • floor ++ [offset_of] dupdip swap row_value
812 23000000000000 2397915 • ++ [offset_of] dupdip swap row_value
813 23000000000000 2397916 • [offset_of] dupdip swap row_value
814 23000000000000 2397916 [offset_of] • dupdip swap row_value
815 23000000000000 2397916 • offset_of 2397916 swap row_value
816 23000000000000 2397916 • dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
817 23000000000000 2397916 2397916 • 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
818 23000000000000 2397916 2397916 2 • * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
819 23000000000000 2397916 4795832 • [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
820 23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] • dip % 2397916 swap row_value
821 23000000000000 2397916 • dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
822 23000000000000 2397916 2397916 • -- 4 * * 2 + - 4795832 % 2397916 swap row_value
823 23000000000000 2397916 2397915 • 4 * * 2 + - 4795832 % 2397916 swap row_value
824 23000000000000 2397916 2397915 4 • * * 2 + - 4795832 % 2397916 swap row_value
825 23000000000000 2397916 9591660 • * 2 + - 4795832 % 2397916 swap row_value
826 23000000000000 22999994980560 • 2 + - 4795832 % 2397916 swap row_value
827 23000000000000 22999994980560 2 • + - 4795832 % 2397916 swap row_value
828 23000000000000 22999994980562 • - 4795832 % 2397916 swap row_value
829 5019438 • 4795832 % 2397916 swap row_value
830 5019438 4795832 • % 2397916 swap row_value
831 223606 • 2397916 swap row_value
832 223606 2397916 • swap row_value
833 2397916 223606 • row_value
834 2397916 223606 • over -- - abs +
835 2397916 223606 2397916 • -- - abs +
836 2397916 223606 2397915 • - abs +
837 2397916 -2174309 • abs +
842 rank_of == -- sqrt 2 / 0.5 - floor ++
843 offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %
844 row_value == over -- - abs +
846 aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value