-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
158 lines (147 loc) · 5.35 KB
/
Main.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
package day14;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static char[][] parseContent(String content) {
String[] lines = content.split("\\n");
char[][] table = new char[lines.length][];
for (int i = 0; i < lines.length; i++) {
table[i] = lines[i].toCharArray();
}
return table;
}
private static void rollNorth(char[][] table) {
for (int col = 0; col < table[0].length; col++) {
int nextValidRow = 0;
for (int row = 0; row < table.length; row++) {
switch (table[row][col]) {
case '#': // Cube-shaped rocks
nextValidRow = row + 1;
break;
case 'O': // Rounded rock
table[row][col] = '.';
table[nextValidRow][col] = 'O';
nextValidRow++;
break;
}
}
}
}
private static void rollSouth(char[][] table) {
for (int col = 0; col < table[0].length; col++) {
int nextValidRow = table.length-1;
for (int row = table.length-1; row >= 0; row--) {
switch (table[row][col]) {
case '#': // Cube-shaped rocks
nextValidRow = row - 1;
break;
case 'O': // Rounded rock
table[row][col] = '.';
table[nextValidRow][col] = 'O';
nextValidRow--;
break;
}
}
}
}
private static void rollWest(char[][] table) {
for (int row = 0; row < table.length; row++) {
int nextValidCol = 0;
for (int col = 0; col < table[0].length; col++) {
switch (table[row][col]) {
case '#': // Cube-shaped rocks
nextValidCol = col + 1;
break;
case 'O': // Rounded rock
table[row][col] = '.';
table[row][nextValidCol] = 'O';
nextValidCol++;
break;
}
}
}
}
private static void rollEast(char[][] table) {
for (int row = 0; row < table.length; row++) {
int nextValidCol = table[0].length - 1;
for (int col = table[0].length - 1; col >= 0; col--) {
switch (table[row][col]) {
case '#': // Cube-shaped rocks
nextValidCol = col - 1;
break;
case 'O': // Rounded rock
table[row][col] = '.';
table[row][nextValidCol] = 'O';
nextValidCol--;
break;
}
}
}
}
private static int computeLoad(final char[][] table) {
int load = 0;
for (int col = 0; col < table[0].length; col++) {
for (int row = 0; row < table.length; row++) {
if (table[row][col] == 'O') { // Rounded rock
final int weight = table.length - row;
load += weight;
}
}
}
return load;
}
private static int checkForPeriodicity(List<Integer> array) {
for (int period = 2; period < array.size()/2; period++) {
boolean isPeriodic = true;
for (int k = 0; k < period; k++) {
final int i = array.size() - 1 - k;
final int j = i - period;
if (!array.get(i).equals(array.get(j))) {
isPeriodic = false;
break;
}
}
if (isPeriodic) {
return period;
}
}
return -1;
}
public static int part01(String filepath) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filepath)));
char[][] table = parseContent(content);
rollNorth(table);
return computeLoad(table);
}
public static int part02(String filepath) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filepath)));
char[][] table = parseContent(content);
final int numberOfCycles = 1000000000;
List<Integer> loadPerCycle = new ArrayList<>();
int period = -1;
for (int cycle = 0; cycle < numberOfCycles; cycle++) {
rollNorth(table);
rollWest(table);
rollSouth(table);
rollEast(table);
loadPerCycle.add(computeLoad(table));
period = checkForPeriodicity(loadPerCycle);
if (period > 0) {
break;
}
}
final int index = loadPerCycle.size() - 1 - period + (numberOfCycles - loadPerCycle.size()) % period;
return loadPerCycle.get(index);
}
public static void main(String[] args) {
try {
System.out.println("Part 01: " + part01("day14/input.txt"));
System.out.println("Part 02: " + part02("day14/input.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
}