-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.java
53 lines (50 loc) · 1.62 KB
/
Day2.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Day2 {
public static void main(String[] args) {
File file = new File("day2.txt");
part1(file); part2(file);
}
public static void part2(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int h = 0, d = 0, aim = 0;
while (scan.hasNextLine()) {
String[] line = scan.nextLine().split(" ");
if (line[0].equals("forward")) {
h += Integer.parseInt(line[1]);
d += aim*Integer.parseInt(line[1]);
} else if (line[0].equals("down")) {
aim += Integer.parseInt(line[1]);
} else if (line[0].equals("up")) {
aim -= Integer.parseInt(line[1]);
}
}
System.out.println(d*h);
}
public static void part1(File file) {
Scanner scan = null;
try {
scan = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int h = 0, d = 0;
while (scan.hasNextLine()) {
String[] line = scan.nextLine().split(" ");
if (line[0].equals("forward")) {
h += Integer.parseInt(line[1]);
} else if (line[0].equals("down")) {
d += Integer.parseInt(line[1]);
} else if (line[0].equals("up")) {
d -= Integer.parseInt(line[1]);
}
}
System.out.println(d*h);
}
}