-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD2.java
62 lines (55 loc) · 1.88 KB
/
D2.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
import java.io.*;
public class D2 {
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 void part1() throws IOException {
BufferedReader inputData = dataReader();
String depth;
int x = 0;
int y = 0;
while ((depth = inputData.readLine()) != null) {
String[] splited = depth.split("\\s+");
int depthNum = Integer.parseInt(splited[1]);
if (splited[0].equals("forward")) {
x += depthNum;
} else if (splited[0].equals("up")) {
y += depthNum;
} else if (splited[0].equals("down")) {
y -= depthNum;
}
}
System.out.println(String.format("part 1: %s", x*y));
}
private static void part2() throws IOException {
BufferedReader inputData = dataReader();
String depth;
int x = 0;
int y = 0;
int aim = 0;
while ((depth = inputData.readLine()) != null) {
String[] splited = depth.split("\\s+");
int depthNum = Integer.parseInt(splited[1]);
if (splited[0].equals("forward")) {
x += depthNum;
y += aim* depthNum;
} else if (splited[0].equals("up")) {
aim += depthNum;
} else if (splited[0].equals("down")) {
aim -= depthNum;
}
}
System.out.println(String.format("part 2: %s", x*y));
}
public static void main(String[] args) throws Exception {
part1();
part2();
}
}