-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD5.java
More file actions
224 lines (208 loc) · 10.4 KB
/
D5.java
File metadata and controls
224 lines (208 loc) · 10.4 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
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
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.lang.Math;
public class D5 {
private static BufferedReader dataReader() {
File file = new File("pathToYourPuzzle");
try {
BufferedReader data = new BufferedReader(new FileReader(file));
return data;
} catch (FileNotFoundException e) {
System.out.println("The file does not exist.");
return null;
}
}
private static int isPlusOne(Integer yValue) {
return yValue > 1 ? 1 : 0;
}
private static void part1() throws IOException {
BufferedReader inputData = dataReader();
String depth;
ArrayList<HashMap<String, Integer>> data = new ArrayList<>();
while ((depth = inputData.readLine()) != null) {
String[] splited = depth.split("->");
List<String> splitedList = new ArrayList<String>(Arrays.asList(splited));
int beforeAfter = 1;
HashMap<String, Integer> temp = new HashMap<>();
for (String coordinate : splitedList) {
List<String> coordinateList = new ArrayList<String>(Arrays.asList(coordinate.split(",")));
temp.put(String.format("x%s", beforeAfter), Integer.parseInt(coordinateList.get(0).replaceAll("\\s", "")));
temp.put(String.format("y%s", beforeAfter), Integer.parseInt(coordinateList.get(1).replaceAll("\\s", "")));
beforeAfter += 1;
}
data.add(temp);
// check the coordinates form in lines
if (beforeAfter == 3 && data.size() > 0) {
HashMap<String, Integer> lastElement = data.get(data.size() - 1);
boolean isLine = lastElement.get("x1").equals(lastElement.get("x2")) || lastElement.get("y1").equals(lastElement.get("y2"));
if (!isLine) data.remove(data.size() - 1);
}
}
// collect the overlap check results
// results = {x : {y: times}}
HashMap<Integer, HashMap<Integer, Integer>> results = new HashMap<>();
for (HashMap<String, Integer> coordinate : data) {
// if x1 = x2
if (coordinate.get("x1").equals(coordinate.get("x2"))) {
int xValue = coordinate.get("x1");
int largeY = coordinate.get("y1") >= coordinate.get("y2") ? coordinate.get("y1") : coordinate.get("y2");
int smallY = largeY == coordinate.get("y1") ? coordinate.get("y2") : coordinate.get("y1");
while (smallY <= largeY) {
// this coordinate is in results
if (results.containsKey(xValue)) {
if (results.get(xValue).containsKey(smallY)) {
results.get(xValue).put(smallY, results.get(xValue).get(smallY) + 1);
} else {
results.get(xValue).put(smallY, 1);
}
} else {
HashMap<Integer, Integer> tempY = new HashMap<>();
tempY.put(smallY, 1);
results.put(xValue, tempY);
}
smallY += 1;
}
} else {
// if y1 = y2
int yValue = coordinate.get("y1");
int largeX = coordinate.get("x1") >= coordinate.get("x2") ? coordinate.get("x1") : coordinate.get("x2");
int smallX = largeX == coordinate.get("x1") ? coordinate.get("x2") : coordinate.get("x1");
while (smallX <= largeX) {
// this coordinate is in results
if (results.containsKey(smallX)) {
if (results.get(smallX).containsKey(yValue)) {
results.get(smallX).put(yValue, results.get(smallX).get(yValue) + 1);
} else {
results.get(smallX).put(yValue, 1);
}
} else {
HashMap<Integer, Integer> tempY = new HashMap<>();
tempY.put(yValue, 1);
results.put(smallX, tempY);
}
smallX += 1;
}
}
}
// calculate overlap
int overlap = 0;
for (HashMap<Integer, Integer> xValue : results.values()) {
for (Integer yValue : xValue.values()) {
overlap += isPlusOne(yValue);
}
}
System.out.println(String.format("part 1: %s", overlap));
}
private static void part2() throws IOException {
BufferedReader inputData = dataReader();
String depth;
ArrayList<HashMap<String, Integer>> data = new ArrayList<>();
while ((depth = inputData.readLine()) != null) {
String[] splited = depth.split("->");
List<String> splitedList = new ArrayList<String>(Arrays.asList(splited));
int beforeAfter = 1;
HashMap<String, Integer> temp = new HashMap<>();
for (String coordinate : splitedList) {
List<String> coordinateList = new ArrayList<String>(Arrays.asList(coordinate.split(",")));
temp.put(String.format("x%s", beforeAfter), Integer.parseInt(coordinateList.get(0).replaceAll("\\s", "")));
temp.put(String.format("y%s", beforeAfter), Integer.parseInt(coordinateList.get(1).replaceAll("\\s", "")));
beforeAfter += 1;
}
data.add(temp);
// check the coordinates form a line or a diagonal line at exactly 45 degrees
if (beforeAfter == 3 && data.size() > 0) {
HashMap<String, Integer> lastElement = data.get(data.size() - 1);
boolean isLine = lastElement.get("x1").equals(lastElement.get("x2")) || lastElement.get("y1").equals(lastElement.get("y2"));
boolean is45Degrees = Math.abs(lastElement.get("x1") - lastElement.get("x2")) == Math.abs(lastElement.get("y1") - lastElement.get("y2"));
if (!isLine && !is45Degrees) data.remove(data.size() - 1);
}
}
// collect the overlap check results
// results = {x : {y: times}}
HashMap<Integer, HashMap<Integer, Integer>> results = new HashMap<>();
for (HashMap<String, Integer> coordinate : data) {
// if x1 = x2
if (coordinate.get("x1").equals(coordinate.get("x2"))) {
int xValue = coordinate.get("x1");
int largeY = coordinate.get("y1") >= coordinate.get("y2") ? coordinate.get("y1") : coordinate.get("y2");
int smallY = largeY == coordinate.get("y1") ? coordinate.get("y2") : coordinate.get("y1");
while (smallY <= largeY) {
// this coordinate is in results
if (results.containsKey(xValue)) {
if (results.get(xValue).containsKey(smallY)) {
results.get(xValue).put(smallY, results.get(xValue).get(smallY) + 1);
} else {
results.get(xValue).put(smallY, 1);
}
} else {
HashMap<Integer, Integer> tempY = new HashMap<>();
tempY.put(smallY, 1);
results.put(xValue, tempY);
}
smallY += 1;
}
} else if (coordinate.get("y1").equals(coordinate.get("y2"))) {
// if y1 = y2
int yValue = coordinate.get("y1");
int largeX = coordinate.get("x1") >= coordinate.get("x2") ? coordinate.get("x1") : coordinate.get("x2");
int smallX = largeX == coordinate.get("x1") ? coordinate.get("x2") : coordinate.get("x1");
while (smallX <= largeX) {
// this coordinate is in results
if (results.containsKey(smallX)) {
if (results.get(smallX).containsKey(yValue)) {
results.get(smallX).put(yValue, results.get(smallX).get(yValue) + 1);
} else {
results.get(smallX).put(yValue, 1);
}
} else {
HashMap<Integer, Integer> tempY = new HashMap<>();
tempY.put(yValue, 1);
results.put(smallX, tempY);
}
smallX += 1;
}
} else {
int largeX = coordinate.get("x1") >= coordinate.get("x2") ? coordinate.get("x1") : coordinate.get("x2");
int smallX = largeX == coordinate.get("x1") ? coordinate.get("x2") : coordinate.get("x1");
int yForLargeX = largeX == coordinate.get("x1") ? coordinate.get("y1") : coordinate.get("y2");
int yForSmallX = yForLargeX == coordinate.get("y1") ? coordinate.get("y2") : coordinate.get("y1");
while (smallX <= largeX) {
// this coordinate is in results
if (results.containsKey(largeX)) {
if (results.get(largeX).containsKey(yForLargeX)) {
results.get(largeX).put(yForLargeX, results.get(largeX).get(yForLargeX) + 1);
} else {
results.get(largeX).put(yForLargeX, 1);
}
} else {
HashMap<Integer, Integer> tempY = new HashMap<>();
tempY.put(yForLargeX, 1);
results.put(largeX, tempY);
}
// move the diagonal way
largeX -= 1;
if (yForLargeX < yForSmallX) {
yForLargeX += 1;
} else {
yForLargeX -= 1;
}
}
}
}
// calculate overlap
int overlap = 0;
for (HashMap<Integer, Integer> xValue : results.values()) {
for (Integer yValue : xValue.values()) {
overlap += isPlusOne(yValue);
}
}
System.out.println(String.format("part 2: %s", overlap));
}
public static void main(String[] args) throws Exception {
part1();
part2();
}
}