5 static int ringmax (int ring
) {
6 return (2 * ring
- 1) * (2 * ring
- 1);
9 static int corner (int ring
, int c
) {
10 return ringmax (ring
) - ((ring
- 1) * 2 * (4 - c
));
15 int main (int argc
, char **argv
) {
18 int goal
= atol(argv
[1]);
20 while (ringmax (ring
) < goal
)
22 printf ("ring = %ld\n", ring
);
24 while (corner (ring
, ++c
) < goal
);
25 printf ("between corners %d, %d\n", corner(ring
, c
- 1), corner(ring
, c
));
26 int mid
= (corner (ring
, c
- 1) + corner (ring
, c
)) / 2;
27 printf ("distance = %ld\n", ring
- 1 + abs (mid
- goal
));
33 static int ring_of (int cell
) {
35 while (ringmax (ring
) < cell
)
40 static int corner_of (int cell
, int ring
) {
42 while (corner (ring
, ++c
) < cell
);
46 static int e_of (int cell
);
47 static int n_of (int cell
);
48 static int w_of (int cell
);
49 static int s_of (int cell
);
54 int ring
= ring_of (cell
);
55 int c
= corner_of (cell
, ring
);
58 return cell
+ 8 * (ring
- 1) + 1;
63 return cell
== corner (ring
, c
) ? cell
+ 1 : cell
- 8 * (ring
- 1) + 3;
73 int ring
= ring_of (cell
);
74 int c
= corner_of (cell
, ring
);
77 return cell
== corner (ring
, c
) ? e_of (cell
) + 2 : cell
+ 1;
79 return cell
+ 8 * (ring
- 1) + 3;
83 return cell
- 8 * (ring
- 1) + 1;
91 int ring
= ring_of (cell
);
92 int c
= corner_of (cell
, ring
);
95 return (cell
== corner (ring
, c
) ? cell
+ 1 :
96 cell
== corner (ring
, c
- 1) + 1 ? cell
- 1 :
97 cell
- 8 * (ring
- 2) - 1);
99 return cell
== corner (ring
, c
) ? n_of (cell
) + 2 : cell
+ 1;
101 return cell
+ 8 * ring
- 3;
108 int s_of (int cell
) {
111 int ring
= ring_of (cell
);
112 int c
= corner_of (cell
, ring
);
115 return cell
== corner (ring
, c
- 1) + 1 ? corner (ring
, 4) : cell
- 1;
117 return cell
== corner (ring
, c
) ? cell
+ 1 : cell
- 8 * (ring
- 2) - 3;
119 return cell
== corner (ring
, c
) ? w_of (cell
) + 2 : cell
+ 1;
121 return cell
+ 8 * ring
- 1;
126 static void locate (int goal
) {
127 printf ("\nprocessing %d\n", goal
);
128 int ring
= ring_of (goal
);
129 printf ("ring = %d\n", ring
);
130 int c
= corner_of (goal
, ring
);
131 printf ("between corners %d, %d\n", corner(ring
, c
- 1), corner(ring
, c
));
132 printf ("%4d %4d %4d\n", w_of (n_of(goal
)), n_of(goal
), e_of (n_of(goal
)));
133 printf ("%4d %4d %4d\n", w_of (goal
), goal
, e_of (goal
));
134 printf ("%4d %4d %4d\n", w_of (s_of(goal
)), s_of(goal
), e_of (s_of(goal
)));
138 static int get(int cell
, int neighbor
) {
143 return array
[neighbor
];
147 int main (int argc
, char **argv
) {
150 int goal
= atoi(argv
[1]);
152 array
= calloc(sizeof(int), limit
+ 1);
154 for (i
= 2; i
<= limit
; i
++) {
155 array
[i
] = get(i
, e_of(i
)) + get(i
, e_of(n_of(i
))) + get(i
, n_of(i
))
156 + get(i
, w_of(n_of(i
))) + get(i
, w_of(i
)) + get(i
, w_of(s_of(i
)))
157 + get(i
, s_of(i
)) + get(i
, e_of(s_of(i
)));
162 printf("limit is too small, recompile\n");
166 printf("value = %d\n", array
[i
]);
178 typedef struct point
{
183 int main (int argc
, char **argv
) {
186 int limit
= atoi(argv
[1]);
187 point
*grid
= calloc(sizeof *grid
, limit
);
190 enum direction
{ R
, U
, L
, D
} dir
= R
;
192 for (int i
= 1; i
< limit
; i
++) {
195 grid
[i
].x
= grid
[i
- 1].x
+ 1;
196 grid
[i
].y
= grid
[i
- 1].y
;
197 if (grid
[i
].x
== -grid
[i
].y
+ 1)
201 grid
[i
].x
= grid
[i
- 1].x
;
202 grid
[i
].y
= grid
[i
- 1].y
+ 1;
203 if (grid
[i
].x
== grid
[i
].y
)
207 grid
[i
].x
= grid
[i
- 1].x
- 1;
208 grid
[i
].y
= grid
[i
- 1].y
;
209 if (grid
[i
].x
== -grid
[i
].y
)
213 grid
[i
].x
= grid
[i
- 1].x
;
214 grid
[i
].y
= grid
[i
- 1].y
- 1;
215 if (grid
[i
].x
== grid
[i
].y
)
219 for (int j
= 0; j
< i
; j
++)
220 if (abs(grid
[j
].x
- grid
[i
].x
) <= 1 &&
221 abs(grid
[j
].y
- grid
[i
].y
) <= 1)
222 grid
[i
].value
+= grid
[j
].value
;
224 if (grid
[i
].value
> limit
) {
225 printf("%d = %d\n", i
+ 1, grid
[i
].value
);
230 printf("%d,%d = %d\n", grid
[limit
- 1].x
, grid
[limit
- 1].y
,
231 abs(grid
[limit
- 1].x
) + abs(grid
[limit
- 1].y
));