-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolidayCode1.java
More file actions
139 lines (114 loc) · 3.99 KB
/
HolidayCode1.java
File metadata and controls
139 lines (114 loc) · 3.99 KB
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
import java.util.*;
public class HolidayCode1 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String rawIn = input.nextLine();
String[] singleDirections = rawIn.split(", ");
int direction = 1; //1 = N, 2 = E, 3 = S, 4 = W
ArrayList<Integer> xs = new ArrayList<Integer>();
ArrayList<Integer> ys = new ArrayList<Integer>();
xs.add(0);
ys.add(0);
for(int i = 0; i < singleDirections.length; i++){
//System.out.print(i);
int currentNS = ys.get(ys.size() - 1);
int currentEW = xs.get(xs.size() - 1);
String current = singleDirections[i];
if(current.charAt(0) == 'R'){
if(direction == 4){
direction = 1;
} else {
direction++;
}
} else if(current.charAt(0) == 'L'){
if(direction == 1){
direction = 4;
} else {
direction--;
}
} else {
System.out.println("This shouldn't happen.");
}
int number = Integer.parseInt(current.substring(1));
if(direction == 4){
//currentEW -= number;
for(int j = 0; j < number; j++){
currentEW--;
int currentX = currentEW;
int currentY = currentNS;
for(int k = 0; k < xs.size(); k++){
if(currentX == xs.get(k)){
if(ys.get(k) == currentY){
System.out.println("This is the location: " + currentEW + ", " + currentNS);
System.out.println(currentNS + currentEW);
return;
}
}
}
xs.add(currentX);
ys.add(currentY);
}
} else if (direction == 1){
//currentNS += number;
for(int j = 0; j < number; j++){
currentNS++;
int currentX = currentEW;
int currentY = currentNS;
for(int k = 0; k < xs.size(); k++){
int testX = xs.get(k);
if(currentX == testX){
if(ys.get(k) == currentY){
System.out.println("This is the location: " + currentEW + ", " + currentNS);
System.out.println(currentNS + currentEW);
return;
}
}
}
xs.add(currentX);
ys.add(currentY);
}
} else if (direction == 2){
//currentEW += number;
for(int j = 0; j < number; j++){
currentEW++;
//System.out.println(currentEW);
int currentX = currentEW;
int currentY = currentNS;
for(int k = 0; k < xs.size(); k++){
int testX = xs.get(k);
if(currentX == testX){
if(ys.get(k) == currentY){
System.out.println("This is the location: " + currentEW + ", " + currentNS);
System.out.println(currentNS + currentEW);
return;
}
}
}
xs.add(currentX);
ys.add(currentY);
}
} else if (direction == 3){
//currentNS -= number;
for(int j = 0; j < number; j++){
currentNS--;
int currentX = currentEW;
int currentY = currentNS;
for(int k = 0; k < xs.size(); k++){
int testX = xs.get(k);
if(currentX == testX){
if(ys.get(k) == currentY){
System.out.println("This is the location: " + currentEW + ", " + currentNS);
System.out.println(currentNS + currentEW);
return;
}
}
}
xs.add(currentX);
ys.add(currentY);
}
} else {
System.out.println("That should't happen.");
}
}
}
}