-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8-2.java
117 lines (94 loc) · 3.91 KB
/
day8-2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws FileNotFoundException {
File f = new File("./day8input.txt");
Scanner in = new Scanner(f);
Tree[][] grid = new Tree[99][99]; // [x][y]
int x = 0;
while (in.hasNextLine()) {
String s = in.nextLine();
char[] carr = s.toCharArray(); // array of chars, line x
for (int y = 0; y < carr.length; y++) {
int i = carr[y] - '0'; // convert char into int of character
grid[x][y] = new Tree(i);
}
x++;
}
in.close();
Tree max = new Tree(0);
for (x = 0; x < grid.length; x++) {
for (int y = 0; y < grid.length; y++) {
Tree t = grid[x][y];
int n = 0;
int s = 0;
int e = 0;
int w = 0;
for (int i = x - 1; i >= 0; i--) { // North, checks above grid point
if (grid[i][y].height >= t.height) {
n++;
break; // if a tree is taller then break, stop adding to scenic score
} else {
n++; // otherwise add to number of visible trees in said direction and keep checking
}
}
for (int i = x + 1; i < grid.length; i++) { // South, checks below grid point
if (i == grid.length) {
break; // stops it from breaking by preventing indexOutOfBounds
}
if (grid[i][y].height >= t.height) {
s++;
break; // if a tree is taller then break, stop adding to scenic score
} else {
s++; // otherwise add to number of visible trees in said direction and keep checking
}
}
for (int i = y - 1; i >= 0; i--) { // West, checks west of grid point
if (grid[x][i].height >= t.height) {
w++;
break; // if a tree is taller then break, stop adding to scenic score
} else {
w++; // otherwise add to number of visible trees in said direction and keep checking
}
}
for (int i = y + 1; i < grid.length; i++) { // East, checks east of grid point
if (i == grid.length) {
break; // stops it from breaking by preventing indexOutOfBounds
}
if (grid[x][i].height >= t.height) {
e++;
break; // if a tree is taller then break, stop adding to scenic score
} else {
e++; // otherwise add to number of visible trees in said direction and keep checking
}
}
/*
* System.out.println(x + " " + y);
* System.out.println(n + " " + s + " " + w + " " + e);
* System.out.println();
*/
int score = grid[x][y].calcScenicScore(new int[] { n, s, w, e });
if (score > max.scenicscore) {
max = t;
System.out.println(x + " " + y);
System.out.println(n + " " + s + " " + w + " " + e);
System.out.println();
}
}
}
System.out.println(max.scenicscore);
}
}
class Tree {
Boolean visible;
int height;
int scenicscore = 0;
public Tree(int height) {
this.height = height;
}
public int calcScenicScore(int[] dir) {
this.scenicscore = dir[0] * dir[1] * dir[2] * dir[3];
return this.scenicscore;
}
}