-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.java
56 lines (51 loc) · 1.44 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
54
55
56
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Day2 {
public static int directions_aim(String fname) throws NumberFormatException, IOException{
int X=0;
int Y=0;
int aim=0;
BufferedReader brdr=new BufferedReader(new FileReader(fname));
String ln;
while ((ln=brdr.readLine())!=null) {
String[] command=ln.split(" ");
int mv=Integer.parseInt(command[1]);
if (command[0].equals("forward"))
{
X+=mv;
Y+=(mv*aim);
}
else if (command[0].equals("up"))
aim-=mv;
else if (command[0].equals("down"))
aim+=mv;
}
return Y*X;
}
public static int directions(String fname) throws NumberFormatException, IOException{
int X=0;
int Y=0;
BufferedReader brdr=new BufferedReader(new FileReader(fname));
String ln;
while ((ln=brdr.readLine())!=null) {
String[] command=ln.split(" ");
int mv=Integer.parseInt(command[1]);
if (command[0].equals("forward"))
X+=mv;
else if (command[0].equals("up"))
Y-=mv;
else if (command[0].equals("down"))
Y+=mv;
}
return Y*X;
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
System.out.println(directions("input/day2_sample.txt"));
System.out.println(directions("input/day2.txt"));
//
System.out.println(directions_aim("input/day2_sample.txt"));
System.out.println(directions_aim("input/day2.txt"));
}
}