-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTobogganTrajectoryThreeTwo.java
More file actions
66 lines (51 loc) · 1.62 KB
/
TobogganTrajectoryThreeTwo.java
File metadata and controls
66 lines (51 loc) · 1.62 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TobogganTrajectoryThreeTwo {
private static int[] xDeltaValues = { 1, 3, 5, 7, 1 };
private static int[] yDeltaValues = { 1, 1, 1, 1, 2 };
public static void main(String[] args) {
long count = 1;
for (int idx = 0; idx < xDeltaValues.length; idx++) {
count *= calculateTrees(idx);
}
System.out.println(count);
}
private static int calculateTrees(int idx) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("toboggantrajectory.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int row = 0;
int xDelta = xDeltaValues[idx];
int yDelta = yDeltaValues[idx];
int count = 0;
try {
reader.readLine();
do {
int skip = yDelta - 1;
while (skip != 0) {
reader.readLine();
skip--;
}
String road = reader.readLine();
if (road == null) {
break;
}
int width = road.length();
int position = ((row + 1) * xDelta) % width;
if (road.charAt(position) == '#') {
count++;
}
row++;
} while (true);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return count;
}
}