-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day09Rope.java
74 lines (69 loc) · 1.75 KB
/
Day09Rope.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
import java.util.ArrayList;
import java.util.HashSet;
public class Day09Rope{
private int[] linksX;
private int[] linksY;
private ArrayList<Point> tailSeen;
public Day09Rope(int linkCount){
linksX = new int[linkCount];
linksY = new int[linkCount];
for(int k=0;k<linkCount;k++){
linksX[k] = 0;
linksY[k] = 0;
}
tailSeen = new ArrayList<Point>();
tailSeen.add(new Point(0,0));
}
public void move(char direction){
switch(direction){
case 'U': linksY[0]++; break;
case 'D': linksY[0]--; break;
case 'L': linksX[0]--; break;
case 'R': linksX[0]++; break;
}
for(int k=1;k<linksX.length;k++){
int distX = linksX[k] - linksX[k-1];
int distY = linksY[k] - linksY[k-1];
if(distX == 2){
linksX[k] = linksX[k-1] + 1;
if(distY == 1){
linksY[k]--;
}else if(distY == -1){
linksY[k]++;
}
}else if(distX == -2){
linksX[k] = linksX[k-1] - 1;
if(distY == 1){
linksY[k]--;
}else if(distY == -1){
linksY[k]++;
}
}
if(distY == 2){
linksY[k] = linksY[k-1] + 1;
if(distX == 1){
linksX[k]--;
}else if(distX == -1){
linksX[k]++;
}
}else if(distY == -2){
linksY[k] = linksY[k-1] - 1;
if(distX == 1){
linksX[k]--;
}else if(distX == -1){
linksX[k]++;
}
}
}
Point tail = new Point(linksX[linksX.length-1],linksY[linksY.length-1]);
if(!tailSeen.contains(tail)){
tailSeen.add(tail);
}
}
public int getSeenCount(){
/*for(Point p : tailSeen){
System.out.println("(" + p.getX() + "," + p.getY() + ")");
}*/
return tailSeen.size();
}
}