-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.java
251 lines (205 loc) · 6.46 KB
/
day3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
public class Main {
public static void main(String[] args) {
int input = 347991;
System.out.println("Distance: " + distance(input));
System.out.println("Value: " + value(input));
System.out.println("Alternate Distance: " + alternatedistance(input)); //better
}
public static int distance(int input) {
int x = 0;
int y = 0;
int max_x = 0;
int min_x = 0;
int max_y = 0;
int min_y = 0;
int direction = 0;
int square = 1;
while (square < input) {
//right
if (direction == 0) {
while (x <= max_x) {
x++;
square++;
if (square == input)
break;
}
if (max_x < x) {
max_x = x;
direction = 1;
}
continue;
}
//up
if (direction == 1) {
while (y <= max_y) {
y++;
square++;
if (square == input)
break;
}
if (max_y < y) {
max_y = y;
direction = 2;
}
continue;
}
//left
if (direction == 2) {
while (x >= min_x) {
x--;
square++;
if (square == input)
break;
}
if (min_x > x) {
min_x = x;
direction = 3;
}
continue;
}
//down
if (direction == 3) {
while (y >= min_y) {
y--;
square++;
if (square == input)
break;
}
if (min_y > y) {
min_y = y;
direction = 0;
}
}
}
if(x < 0)
x = x * -1;
if(y < 0)
y = y * -1;
return (x + y);
}
public static void calcvalue(int[][] values, int start, int xx, int yy){
values[start+xx][start+yy] = values[start+(xx+1)][start+yy] + values[start+xx][start+(yy+1)] + values[start+(xx-1)][start+yy] + values[start+xx][start+(yy-1)] + values[start+(xx+1)][start+(yy+1)] + values[start+(xx+1)][start+(yy-1)] + values[start+(xx-1)][start+(yy+1)] + values[start+(xx-1)][start+(yy-1)];
}
public static int value(int input){
int x = 0;
int y = 0;
int max_x = 0;
int min_x = 0;
int max_y = 0;
int min_y = 0;
int direction = 0;
int square = 1;
int a = (int) Math.sqrt(input);
int[][] values = new int[a][];
for(int i=0; i < a; i++)
values[i] = new int[a];
int start = a / 2;
values[start][start] = 1;
while (square < input) {
//right
if (direction == 0) {
while (x <= max_x) {
x++;
square++;
calcvalue(values, start, x, y);
if(values[start+x][start+y] > input)
return values[start+x][start+y];
if (square == input)
break;
}
if (max_x < x) {
max_x = x;
direction = 1;
}
continue;
}
//up
if (direction == 1) {
while (y <= max_y) {
y++;
square++;
calcvalue(values, start, x, y);
if(values[start+x][start+y] > input)
return values[start+x][start+y];
if (square == input)
break;
}
if (max_y < y) {
max_y = y;
direction = 2;
}
continue;
}
//left
if (direction == 2) {
while (x >= min_x) {
x--;
square++;
calcvalue(values, start, x, y);
if(values[start+x][start+y] > input)
return values[start+x][start+y];
if (square == input)
break;
}
if (min_x > x) {
min_x = x;
direction = 3;
}
continue;
}
//down
if (direction == 3) {
while (y >= min_y) {
y--;
square++;
calcvalue(values, start, x, y);
if(values[start+x][start+y] > input)
return values[start+x][start+y];
if (square == input)
break;
}
if (min_y > y) {
min_y = y;
direction = 0;
}
}
}
return 0;
}
public static int alternatedistance(int input){
int a = (int) Math.pow(((int) Math.sqrt(input)), 2);
int a_x;
int a_y;
int input_x;
int input_y;
if( ((int) Math.sqrt(a)) % 2 == 0){
a_x = -((int) Math.sqrt(a)/2 -1);
a_y = (int) Math.sqrt(a)/2;
if((input - a) <= ((int) Math.sqrt(a)) + 1){
input_x = a_x - 1;
input_y = a_y - (input - a - 1);
}
else {
input_x = a_x + ((input - a) - ((int) Math.sqrt(a) + 2));
input_y = a_y - ((int) Math.sqrt(a));
}
}
else{
a_x = ((int) Math.sqrt(a)-1)/2;
a_y = -(((int) Math.sqrt(a)-1)/2);
if((input - a) <= ((int) Math.sqrt(a)) + 1){
input_x = a_x + 1;
input_y = a_y + (input - a - 1);
}
else {
input_x = a_x - ((input - a) - ((int) Math.sqrt(a) + 2));
input_y = a_y + ((int) Math.sqrt(a));
}
}
if(input_x < 0)
input_x = input_x * -1;
if(input_y < 0)
input_y = input_y * -1;
return (input_x + input_y);
}
}