-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle14.java
204 lines (185 loc) · 5.7 KB
/
Puzzle14.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package advent2023;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* @author Éamonn McManus
*/
public class Puzzle14 {
public static void main(String[] args) throws Exception {
try (InputStream in = Puzzle14.class.getResourceAsStream("puzzle14.txt")) {
String lineString = new String(in.readAllBytes(), UTF_8);
List<String> lines = List.of(lineString.split("\n"));
char[][] chars = new char[lines.size()][];
for (int i = 0; i < lines.size(); i++) {
chars[i] = lines.get(i).toCharArray();
}
// Part 1
tiltNorth(chars);
System.out.println("Load " + load(chars));
// Part 2
// The assumption here is that there is a cycle, such that the state repeats, possibly
// after an initial sequence of states that don't repeat. So we just need to find the length
// of that initial sequence and the length of the cycle, and be a bit careful about the exact
// calculation.
Set<CharWrapper> seen = new LinkedHashSet<>();
CharWrapper seenWrapper;
while (true) {
cycle(chars);
CharWrapper wrapper = CharWrapper.copyOf(chars);
if (!seen.add(wrapper)) {
seenWrapper = wrapper;
break;
}
}
List<CharWrapper> seenList = new ArrayList<>(seen);
int cycleStart = seenList.indexOf(seenWrapper);
// For the small case, we see this:
// 1 2 3 4 5 6 7 8 9
// 10 11 12 13 14 15 16
// 17 18 19 20 21 22 23
// seen.size() is 9 and cycleStart is 2. So the zero-origin index of the ith iteration is
// 2 + (i - 2 - 1) % 7. For example, for the 11th it is 2 + 1. In general it is
// cycleStart + (i - cycleStart - 1) % (seen.size() - cycleStart).
System.out.println("Cycle after " + seen.size() + " iterations, starting at " + cycleStart);
int billionI = cycleStart + (1_000_000_000 - cycleStart - 1) % (seen.size() - cycleStart);
System.out.println(
"Load for billionth same as for i="
+ billionI
+ " = "
+ load(seenList.get(billionI).chars));
}
}
static class CharWrapper {
private final char[][] chars;
private int hashCode;
private CharWrapper(char[][] chars) {
this.chars = chars;
}
static CharWrapper copyOf(char[][] chars) {
char[][] copy = new char[chars.length][];
for (int i = 0; i < chars.length; i++) {
copy[i] = chars[i].clone();
}
return new CharWrapper(copy);
}
@Override
public boolean equals(Object o) {
if (o instanceof CharWrapper that) {
if (this.chars.length != that.chars.length) {
return false;
}
for (int i = 0; i < chars.length; i++) {
if (!Arrays.equals(this.chars[i], that.chars[i])) {
return false;
}
}
return true;
}
return false;
}
@Override
public int hashCode() {
if (hashCode == 0) {
hashCode = Arrays.deepHashCode(new Object[] {chars});
}
return hashCode;
}
@Override
public String toString() {
List<String> strings = new ArrayList<>();
for (char[] line : chars) {
strings.add(new String(line));
}
return String.join("\n", strings);
}
}
private static void show(char[][] chars) {
for (char[] line : chars) {
System.out.println(new String(line));
}
}
private static long load(char[][] chars) {
long load = 0;
for (int i = 1; i <= chars.length; i++) {
char[] line = chars[chars.length - i];
for (int j = 0; j < line.length; j++) {
if (line[j] == 'O') {
load += i;
}
}
}
return load;
}
private static void cycle(char[][] chars) {
tiltNorth(chars);
tiltWest(chars);
tiltSouth(chars);
tiltEast(chars);
}
// Could probably have unified these four methods into a single one but I was lazy.
private static void tiltNorth(char[][] chars) {
boolean changed = true;
while (changed) {
changed = false;
for (int i = 1; i < chars.length; i++) {
for (int j = 0; j < chars[i].length; j++) {
if (chars[i][j] == 'O' && chars[i - 1][j] == '.') {
changed = true;
chars[i - 1][j] = 'O';
chars[i][j] = '.';
}
}
}
}
}
private static void tiltWest(char[][] chars) {
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < chars.length; i++) {
for (int j = 1; j < chars[i].length; j++) {
if (chars[i][j] == 'O' && chars[i][j - 1] == '.') {
changed = true;
chars[i][j - 1] = 'O';
chars[i][j] = '.';
}
}
}
}
}
private static void tiltSouth(char[][] chars) {
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < chars.length - 1; i++) {
for (int j = 0; j < chars[i].length; j++) {
if (chars[i][j] == 'O' && chars[i + 1][j] == '.') {
changed = true;
chars[i + 1][j] = 'O';
chars[i][j] = '.';
}
}
}
}
}
private static void tiltEast(char[][] chars) {
boolean changed = true;
while (changed) {
changed = false;
for (int i = 0; i < chars.length; i++) {
for (int j = 0; j < chars[i].length - 1; j++) {
if (chars[i][j] == 'O' && chars[i][j + 1] == '.') {
changed = true;
chars[i][j + 1] = 'O';
chars[i][j] = '.';
}
}
}
}
}
}