-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17A.java
147 lines (122 loc) · 4.56 KB
/
Day17A.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
package advent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Day17A {
public static void main(String[] args) throws IOException {
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Paste the input:");
int lineCount = 0;
Set<XYZ> cubes = new HashSet<>();
do {
final String readLine = bufferedReader.readLine();
if (readLine != null && !readLine.isEmpty()) {
for (int i = 0; i < readLine.trim().length(); i++) {
if (readLine.charAt(i) == '#') {
cubes.add(new XYZ(i, lineCount, 0));
}
}
lineCount++;
} else {
break;
}
} while (true);
System.out.println("Got " + lineCount + " lines");
System.out.println("Got " + cubes.size() + " cubes");
final Instant start = Instant.now();
for (int i = 1; i <= 6; i++) {
final Map<XYZ, Integer> neighborCubeCounter = new HashMap<>();
final Set<XYZ> newCubes = new HashSet<>();
for (final XYZ cube : cubes) {
addUpNeighbors(cube, neighborCubeCounter);
final int neighborCount = countNeighbors(cubes, cube);
if (neighborCount == 2 || neighborCount == 3) {
newCubes.add(cube);
}
}
for (final Map.Entry<XYZ, Integer> neighborCubeCount : neighborCubeCounter.entrySet()) {
if (neighborCubeCount.getValue() == 3 && !cubes.contains(neighborCubeCount.getKey())) {
newCubes.add(neighborCubeCount.getKey());
}
}
cubes = newCubes;
}
final Instant finish = Instant.now();
final long timeElapsed = Duration.between(start, finish).toMillis();
System.out.println("Cube count on the 6th iteration is " + cubes.size() + " in " + timeElapsed + "ms");
}
private static int countNeighbors(final Set<XYZ> cubes, final XYZ cube) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
for (int k = -1; k <= 1; k++) {
if (i == 0 && j == 0 && k == 0) {
continue;
}
final XYZ neighbor = new XYZ(cube.getX() + i, cube.getY() + j, cube.getZ() + k);
if (cubes.contains(neighbor)) {
count++;
}
}
}
}
return count;
}
private static void addUpNeighbors(final XYZ cube, final Map<XYZ, Integer> neighborCubeCounter) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
for (int k = -1; k <= 1; k++) {
if (i == 0 && j == 0 && k == 0) {
continue;
}
final XYZ neighbor = new XYZ(cube.getX() + i, cube.getY() + j, cube.getZ() + k);
final Integer count = neighborCubeCounter.get(neighbor);
if (count == null) {
neighborCubeCounter.put(neighbor, 1);
} else {
neighborCubeCounter.put(neighbor, count + 1);
}
}
}
}
}
private static class XYZ {
private final Integer x;
private final Integer y;
private final Integer z;
private XYZ(final Integer x, final Integer y, final Integer z) {
this.x = x;
this.y = y;
this.z = z;
}
public Integer getX() {
return x;
}
public Integer getY() {
return y;
}
public Integer getZ() {
return z;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final XYZ xyz = (XYZ) o;
return Objects.equals(x, xyz.x) &&
Objects.equals(y, xyz.y) &&
Objects.equals(z, xyz.z);
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
}
}