-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day24.java
180 lines (147 loc) · 5.17 KB
/
Day24.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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Day24 {
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("input24.txt"));
Map<Pair<Integer, Integer>, Character> map = new HashMap<>();
for (String line : lines) {
flip(line, map);
}
System.out.println(countBlack(map));
for (int i = 0; i < 100; i++) {
map = flipDay(map);
}
System.out.println(countBlack(map));
}
private static int countBlack(Map<Pair<Integer, Integer>, Character> map) {
int black = 0;
for (Pair<Integer, Integer> key : map.keySet()) {
if (map.get(key) == 'b') ++black;
}
return black;
}
// nw w sw e e | nw, ne, e, w, sw, se
private static void flip(String line, Map<Pair<Integer, Integer>, Character> map) {
int row = 0, column = 0;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == 'e') {
column++;
} else if (line.charAt(i) == 'w') {
column--;
} else if (line.charAt(i) == 's' && line.charAt(i + 1) == 'e') {
if (row % 2 == 0) {
column++;
} else {
//column is not changing
}
row--;
++i;
} else if (line.charAt(i) == 's' && line.charAt(i + 1) == 'w') {
if (row % 2 == 0) {
//column is not changing
} else {
column--;
}
row--;
++i;
} else if (line.charAt(i) == 'n' && line.charAt(i + 1) == 'e') {
if (row % 2 == 0) {
column++;
} else {
//column is not changing
}
row++;
++i;
} else if (line.charAt(i) == 'n' && line.charAt(i + 1) == 'w') {
if (row % 2 == 0) {
//column is not changing
} else {
column--;
}
row++;
++i;
}
}
Pair<Integer, Integer> dimension = new Pair<Integer, Integer>(row, column);
if (map.containsKey(dimension)) {
char color = map.get(dimension);
if (color == 'b') map.put(dimension, 'w');
else map.put(dimension, 'b');
} else {
map.put(dimension, 'b');
}
}
private static Map<Pair<Integer, Integer>, Character> flipDay(Map<Pair<Integer, Integer>, Character> map) {
Map<Pair<Integer, Integer>, Character> newMap = new HashMap<>(map);
int size = 70;
for (int i = -size; i < size; i++) {
for (int j = -size; j < size; j++) {
Pair<Integer, Integer> pos = new Pair<Integer, Integer>(i, j);
if (shouldFlip(pos, map)) {
if (!newMap.containsKey(pos)) newMap.put(pos, 'b');
else {
if (newMap.get(pos) == 'b') newMap.put(pos, 'w');
else if (newMap.get(pos) == 'w') newMap.put(pos, 'b');
}
}
}
}
return newMap;
}
private static boolean shouldFlip(Pair<Integer, Integer> pos, Map<Pair<Integer, Integer>, Character> map) {
int blackNeighbour = 0;
if (map.get(new Pair<Integer, Integer>(pos.getFirst(), pos.getSecond() + 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst(), pos.getSecond() + 1)) == 'b') {
++blackNeighbour;// east
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst(), pos.getSecond() - 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst(), pos.getSecond() - 1)) == 'b') {
++blackNeighbour; // west
}
if(pos.getFirst() % 2 == 0) {
if (map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond())) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond())) == 'b') {
++blackNeighbour;// north west
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond() + 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond() + 1)) == 'b') {
++blackNeighbour; // north east
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond())) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond())) == 'b') {
++blackNeighbour;// south west
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond() + 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond() + 1)) == 'b') {
++blackNeighbour; // south east
}
} else {
if (map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond() - 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond() - 1)) == 'b') {
++blackNeighbour;// north west
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond())) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() + 1, pos.getSecond())) == 'b') {
++blackNeighbour; // north east
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond() - 1)) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond() - 1)) == 'b') {
++blackNeighbour;// south west
}
if (map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond())) != null
&& map.get(new Pair<Integer, Integer>(pos.getFirst() - 1, pos.getSecond())) == 'b') {
++blackNeighbour; // south east
}
}
if(map.containsKey(pos) && map.get(pos) == 'b') {
if(blackNeighbour == 0 || blackNeighbour > 2) return true;
} else {
if(blackNeighbour == 2) return true;
}
return false;
}
}