-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode2018Day18.java
180 lines (169 loc) · 6.86 KB
/
AdventOfCode2018Day18.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.util.*;
public class AdventOfCode2018Day18 {
private static boolean treesNearbyForOpen(Character[][] grid, int row, int col) {
int treeCount = 0;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (c == col && r == row) continue;
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) continue;
if (grid[r][c] == '|') treeCount++;
}
}
return (treeCount >= 3 ? true : false);
}
private static boolean lumberyardNearbyForTrees(Character[][] grid, int row, int col) {
int lumberyardCount = 0;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (c == col && r == row) continue;
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) continue;
if (grid[r][c] == '#') lumberyardCount++;
}
}
return (lumberyardCount >= 3 ? true : false);
}
private static boolean lumberyardNearbyForLumberyard(Character[][] grid, int row, int col) {
int lumberyardCount = 0, treeCount = 0;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (c == col && r == row) continue;
if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length) continue;
if (grid[r][c] == '#') lumberyardCount++;
else if (grid[r][c] == '|') treeCount++;
}
}
return (lumberyardCount >= 1 && treeCount >= 1 ? true : false);
}
private static Character[][] processGrid(Character[][] grid) {
Character[][] newGrid = new Character[grid.length][grid[0].length];
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
// check for open acre
if (grid[row][col] == '.' && treesNearbyForOpen(grid, row, col)) {
newGrid[row][col] = '|';
}
// check for tree acre
else if (grid[row][col] == '|' && lumberyardNearbyForTrees(grid, row, col)) {
newGrid[row][col] = '#';
}
// check for lumberyard acre
else if (grid[row][col] == '#') {
if (lumberyardNearbyForLumberyard(grid, row, col)) newGrid[row][col] = '#';
else newGrid[row][col] = '.';
}
else newGrid[row][col] = grid[row][col];
}
}
return newGrid;
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
ArrayList<String> input = new ArrayList<>();
while (true) {
String line = s.nextLine();
if (line.equals("")) break;
input.add(line);
}
s.close();
Character[][] grid = new Character[input.size()][input.get(0).length()];
for (int row = 0; row < input.size(); row++) {
for (int col = 0; col < input.get(0).length(); col++) {
grid[row][col] = input.get(row).charAt(col);
}
}
ArrayList<Integer> answers = new ArrayList<>();
boolean noGridProcessing = false;
int startRep = -1, index = -1;
for (long i = 0; i < 1000000000; i++) {
if (startRep == -1) {
if (!noGridProcessing) grid = processGrid(grid);
if (i == 9) System.out.println("Answer to the first part: " + countWoodAndLumberyards(grid));
int a = countWoodAndLumberyards(grid);
index = answers.lastIndexOf(a);
if (index != -1 && answers.size() - index == 28 && index > 400) { // this number of trees and lumberyards have been seen before
noGridProcessing = true; // at this point, the results are repetitive
startRep = index;
}
else {
answers.add(a);
}
}
else {
++startRep;
if (startRep == answers.size()) startRep = index;
}
}
System.out.print("Answer to the second part: " + answers.get(startRep));
}
private static int countWoodAndLumberyards(Character[][] grid) {
int trees = 0, lumberyard = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == '|') trees++;
else if (grid[row][col] == '#') lumberyard++;
}
}
return trees * lumberyard;
}
private static void printGrid(Character[][] grid) {
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
System.out.print(grid[row][col]);
}
System.out.println();
}
System.out.println();
}
}
/*
.||.###.|.#|.|...|#|..##...|..#....#.|..|..##.#|..
..|..|#..##..|..#.......|#.#|##..|#...#...|.|..||.
#..#|..........#...#.#.|...||...##||..#..|#..#..||
......|#...#|.#|#..|.#.#|....#|...|.#.....#..|#.|.
.##.###||..#..||..|#.|...#.|.||..|.||.|##|#|...|..
.|...||....##||...##....|..#|....#|#.|..#..|.#.##.
.#...||#.....#|...#...#||#...###..|.#.||..#...||..
......|.|.|.#..#..#.|..|.#.#||..|||#.||..|.###..|.
......#|...|..#||..|##.#....#.#...|.....|.|.|.###.
......|.....##....|...|.##|.|..#..........|....#..
.|.|...|.||...#.....|.#...#.##....#..#..........|.
..|..|##.#...|.....#|.|.|..|....|.......|||.......
..#|..|...#..||.......|.|...#..........##.#..|#.|.
||#|.##...##.#.#||....#.||.#....#|....|...#.|.#|..
|.|.#.|.#..#|.#..|.#.||....#..|.|.|#..|||.|.|.#..#
..#..|#|.##....||#|.....|#..|...|##..|#....|##.#..
|...||...##.#..#.|....#..##|.|.#||#.#.|.#..|.#....
.#..||.|...#...#.#.#..#.||..#...#|.##......||||...
...#...|#|.....#|....#...##......##.#...|..#..#..|
.|..|||..|#.|....|#.##..||..#|....|....##.|.|.....
........#...|#......|##|##....##.#|#.|#|........|#
.|....##.|.||#..####..#.##|#....|...|#..##.......#
..|#.|.|..|...|.||.|#..|......#..#...|.#.#..###||.
#.##||.#||.|.|#|.....#|.#|...#|.#|#||#||.|#...#|.|
|..|..#...|##.|...........#.|##.|..##..||.#|..|||#
..|.|..|.|..|..||..##.|.#...#|..|.|..|#.##.#||....
.....#..|.|..|||.###...|#|#..|..#....#......##.|..
|...##.|.#.............|.#.#.....#|#..#...#...|.|.
|##|.|...|.|.....#|....##....|.#....|...##..#...|.
|.....##..#.#.|.|..|##.#|#.....||.#...|.#..#|.#|..
#.#..#......#........|.|..#|...###...|...#..#..#..
.||..#.....##.##..||.|...#.|..||.......|#|.|.#|..|
||##.....##...#...#|.#|....|||.|.#..#.#...|....|..
|....||...|.||......#...#.#..|.#..|.|##.|#........
..#.|.|...|.|..|#...#.|.|..|........#.|.#...|.|...
.#|.|.|.|#..##|.|..|.||.|.#.##..|...|.|....|...|..
|#..|....#...#|##.....|...|..#|..##.||#.#|...|#.||
|..#||...#.|......|..|#.##.#.....##.#|#...###||...
|.|.#..#|...|#.|...|...#.|.......##....#|...|.|.|#
...|...#..##|..|.....#.|...|.#..|.|#..|...........
...##|..#...#|...#...#..###..||....##.#..###.#|.|.
.#.|.....#...#..#|||#..|#||.#||.#..|.|.#|..#.#||..
#..|..#..||#.|##...|..#|.||#|####.#.|...|..|#|#...
|#..#..#..#..#.......|.#.|..||..|........|...#.|..
.|#.#....|..##..|..|||.|.#.||....|#|....|#...#|.#.
#..|.....||#....##.|...#.#|.......|..|||||..#..#|#
#|.#|..#....#|#...||..|......||.#|.|.|...||..#..#.
#|..|##..##|#...||..||#.#.|.|...#...#..||.|..#.|..
|#.....#..#.#.#...||.....|...||.#.#.|....|.||.#.#.
##.##|..||...|###|.||.||..#||..#.||.#|.||..|...|..
*/