-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvent08.java
139 lines (120 loc) · 4.79 KB
/
Advent08.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.IOException;
import java.util.Arrays;
public class Advent08 {
public static void main(String[] args) throws IOException {
boolean example = ((args.length > 0) && (args[0].equals("example")));
String file = (example) ? "data/example_08.txt" : "data/08.txt";
String[] data = Shared.readFile(file);
// Part 1:
int[][] grid = new int[data.length][data[0].length()];
for (int i = 0; i < data.length; i++) {
String[] row = data[i].split("");
for (int j = 0; j < row.length; j++) {
grid[i][j] = Integer.parseInt(row[j]);
}
}
// start with everything marked as hidden, then go through and update if visible
int[][] visibility = new int[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
// left visibility
int[] leftArray = Arrays.copyOfRange(grid[i], 0, j);
if (j == 0 || grid[i][j] > Arrays.stream(leftArray).max().getAsInt()) {
visibility[i][j] = 1;
continue;
}
// right visibility
int [] rightArray = Arrays.copyOfRange(grid[i], j+1, grid[i].length);
if (j == grid[i].length - 1 || grid[i][j] > Arrays.stream(rightArray).max().getAsInt()) {
visibility[i][j] = 1;
continue;
}
// top visibility
int[] col = getColumn(grid, j);
int [] topArray = Arrays.copyOfRange(col, 0, i);
if (i == 0 || grid[i][j] > Arrays.stream(topArray).max().getAsInt()) {
visibility[i][j] = 1;
continue;
}
// bottom visibility
int [] bottomArray = Arrays.copyOfRange(col, i+1, col.length);
if (i == col.length - 1 || grid[i][j] > Arrays.stream(bottomArray).max().getAsInt()) {
visibility[i][j] = 1;
}
}
}
int solution_a = 0;
for (int[] row : visibility) {
for (int val : row) {
solution_a += val;
}
}
System.out.println("Solution 08a: " + solution_a);
if (example) {
int correct_a = 21;
Shared.checkResult(solution_a, correct_a);
}
// Part 2:
// don't need to check first/last rows/columns because they have 0 trees visible in one direction
int maxScore = 0;
for (int i = 1; i < grid.length - 1; i++) {
for (int j = 1; j < grid[i].length - 1; j++) {
int height = grid[i][j];
// looking left
int leftScore = 0;
int[] leftArray = Arrays.copyOfRange(grid[i], 0, j);
for (int k = leftArray.length - 1; k >= 0; k = k - 1) {
leftScore += 1;
if (leftArray[k] >= height) {
break;
}
}
// looking right
int rightScore = 0;
int[] rightArray = Arrays.copyOfRange(grid[i], j+1, grid[i].length);
for (int value : rightArray) {
rightScore += 1;
if (value >= height) {
break;
}
}
// looking up
int[] col = getColumn(grid, j);
int topScore = 0;
int [] topArray = Arrays.copyOfRange(col, 0, i);
for (int k = topArray.length - 1; k >= 0; k = k - 1) {
topScore += 1;
if (topArray[k] >= height) {
break;
}
}
// looking down
int bottomScore = 0;
int [] bottomArray = Arrays.copyOfRange(col, i+1, col.length);
for (int value : bottomArray) {
bottomScore += 1;
if (value >= height) {
break;
}
}
int score = leftScore * rightScore * topScore * bottomScore;
if (score > maxScore) {
maxScore = score;
}
}
}
int solution_b = maxScore;
System.out.println("Solution 08b: " + solution_b);
if (example) {
int correct_b = 8;
Shared.checkResult(solution_b, correct_b);
}
}
public static int[] getColumn(int[][] array, int index){
int[] column = new int[array[0].length];
for(int i=0; i<column.length; i++){
column[i] = array[i][index];
}
return column;
}
}