-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay04.java
More file actions
88 lines (80 loc) · 2.73 KB
/
Day04.java
File metadata and controls
88 lines (80 loc) · 2.73 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.List;
public class Day04 {
private char[][] _rollMap;
private int _rows;
private int _cols;
public Day04(List<String> rollMap) {
super();
_rows= rollMap.size();
_cols= rollMap.get(0).length();
_rollMap = new char[_rows][_cols];
for (int r=0; r<_rows; r++) {
_rollMap[r] = rollMap.get(r).toCharArray();
}
}
public static void Run(List<String> input) {
Day04 day04 = new Day04(input);
day04.part2();
}
void part1(){
int accessibleCount =0;
for (int r=0; r<_rows; r++) {
for (int c=0; c<_cols; c++) {
if (IsAccessibleRoll(r,c)) {
accessibleCount++;
}
}
}
System.out.println("04.1: "+Integer.toString(accessibleCount));
}
void part2() {
int totalRemoved = 0;
boolean removedSomething = true;
do {
removedSomething = false;
for (int r = 0; r < _rows; r++) {
for (int c = 0; c < _cols; c++) {
if (IsAccessibleRoll(r, c)) {
totalRemoved++;
removedSomething = true;
RemoveRoll(r, c);
}
}
}
} while (removedSomething);
System.out.println("04.2: " + Integer.toString(totalRemoved));
}
boolean SpaceIsOccupied(int row, int col) {
if (row<0 || row>=_rows || col<0 || col>=_cols) {
return false;
}
return _rollMap[row][col]=='@';
}
void RemoveRoll(int row, int col){
if (row<0 || row>=_rows || col<0 || col>=_cols) {
return;
}
_rollMap[row][col] = '.';
}
int CountAdjacentRolls(int row, int col){
int adjacentRolls=0;
for (int r=row-1; r<=row+1; r++) {
for (int c=col-1; c<=col+1; c++) {
if (r==row && c==col) {
continue;
}
if (SpaceIsOccupied(r,c)) {
adjacentRolls++;
}
}
}
return adjacentRolls;
}
boolean IsAccessibleRoll(int row, int col) {
if (!SpaceIsOccupied(row,col)) {
return false;
}
int adjacentRolls = CountAdjacentRolls(row,col);
return (adjacentRolls<4);
}
}