-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8b.java
119 lines (88 loc) · 3.09 KB
/
Day8b.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
118
119
package aoc22;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Day8b {
private static String filename = "./src/aoc22/input8.txt";
// private static String filename = "./src/aoc22/example8.txt";
private final Tree[][] treeGrid;
private int highestScenicScore;
public Day8b(List<String> input) {
treeGrid = new Tree[input.get(0).length()][input.size()];
parse(input);
determineVisibility();
System.out.println("Highest scenic score: " + highestScenicScore);
}
private void parse(List<String> input) {
for (int i = 0; i < input.get(0).length(); i++) {
for (int j = 0; j < input.size(); j++) {
treeGrid[i][j] = new Tree(Integer.parseInt(input.get(i).charAt(j) + ""));
}
}
}
private void determineVisibility() {
for (int row = 0; row < treeGrid[0].length; row++) {
for (int column = 0; column < treeGrid.length; column++) {
if (treeAtEdge(row, column)) {
continue;
}
int scenicScore = visibileDown(row, column);
scenicScore *= visibileUp(row, column);
scenicScore *= visibileRight(row, column);
scenicScore *= visibileLeft(row, column);
if (scenicScore > highestScenicScore) {
highestScenicScore = scenicScore;
}
}
}
}
private boolean treeAtEdge(int row, int column) {
return row == 0 || column == 0 || row == treeGrid[0].length || column == treeGrid.length;
}
private int visibileDown(int row, int column) {
int scenicScore = 0;
for (int i = row + 1; i < treeGrid.length; i++) {
scenicScore++;
if (treeGrid[row][column].height <= treeGrid[i][column].height) {
break;
}
}
return scenicScore;
}
private int visibileUp(int row, int column) {
int scenicScore = 0;
for (int i = row - 1; i >= 0; i--) {
scenicScore++;
if (treeGrid[row][column].height <= treeGrid[i][column].height) {
break;
}
}
return scenicScore;
}
private int visibileRight(int row, int column) {
int scenicScore = 0;
for (int i = column + 1; i < treeGrid[0].length; i++) {
scenicScore++;
if (treeGrid[row][column].height <= treeGrid[row][i].height) {
break;
}
}
return scenicScore;
}
private int visibileLeft(int row, int column) {
int scenicScore = 0;
for (int i = column - 1; i >= 0; i--) {
scenicScore++;
if (treeGrid[row][column].height <= treeGrid[row][i].height) {
break;
}
}
return scenicScore;
}
public static void main(String[] args) throws IOException {
new Day8b(Files.readAllLines(Path.of(filename)));
}
private record Tree(int height) {
}
}