-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.java
65 lines (55 loc) · 1.77 KB
/
Day02.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
//part 1
package org.thehuglio;
import javax.swing.*;
import java.io.File;
import java.util.List;
public class Main {
private static final List<String> data = new Reader(new File("data.txt")).data;
public static void main(String[] args) {
int horizontal = 0;
int depth = 0;
for (String d : data) {
String[] split = d.split(" ");
if (split[0].equals("forward")) {
horizontal += Integer.parseInt(split[1]);
} else if (split[0].equals("down")) {
depth -= Integer.parseInt(split[1]);
} else if (split[0].equals("up")) {
depth += Integer.parseInt(split[1]);
}
System.out.println(depth);
System.out.println(horizontal);
System.out.println();
}
System.out.println(horizontal * -depth);
}
}
//part 2
package org.thehuglio;
import javax.swing.*;
import java.io.File;
import java.util.List;
public class Main {
private static final List<String> data = new Reader(new File("data.txt")).data;
public static void main(String[] args) {
int horizontal = 0;
int aim = 0;
int depth = 0;
for (String d : data) {
String[] split = d.split(" ");
int number = Integer.parseInt(split[1]);
if (split[0].equals("forward")) {
horizontal += number;
depth += number * aim;
} else if (split[0].equals("down")) {
aim += number;
} else if (split[0].equals("up")) {
aim -= number;
}
System.out.println(depth);
System.out.println(horizontal);
System.out.println();
}
System.out.println(horizontal * depth);
}
}