-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3_Part1.java
73 lines (67 loc) · 2 KB
/
Day3_Part1.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
public class Day3_Part1 {
public static void main(String[] args)
{
int number = 312051;
if (number == 1)
System.out.println(0);
else
{
int[] circleAndPosition = detectCircleAndPosition(number);
int[] range = generateRange(circleAndPosition[0]);
System.out.println(range[(circleAndPosition[1] - 1) % range.length]);
}
}
private static int[] detectCircleAndPosition(int number)
{
int maxNumberInCircle = 1;
int circle = 1;
int position = 0;
for (int i = 1; i < number;)
{
int numOfNumberInCircle = ((2 * circle - 1) * 4 + 4);
if (number <= numOfNumberInCircle + maxNumberInCircle)
{
position = number - maxNumberInCircle;
return new int[] {circle, position};
}
else
{
maxNumberInCircle = maxNumberInCircle + numOfNumberInCircle;
circle++;
i += numOfNumberInCircle;
}
}
return null;
}
private static int[] generateRange(int circle)
{
int length = circle * 2;
int[] result = new int[length];
if (circle == 1)
{
result[0] = 1;
result[1] = 2;
}
else
{
for (int i = 0; i < length; i++)
{
int mid = length / 2;
for (int j = 0; j < mid; j++)
{
if (j == 0)
{
result[j] = circle + mid - 1;
result[j + mid] = circle + 1;
}
else
{
result[j] = result[j - 1] - 1;
result[j + mid] = result[j + mid - 1] + 1;
}
}
}
}
return result;
}
}