-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.groovy
162 lines (144 loc) · 4.63 KB
/
day14.groovy
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
import groovy.time.TimeCategory;
import groovy.time.TimeDuration;
import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;
import groovy.transform.TupleConstructor;
@EqualsAndHashCode
@ToString
@TupleConstructor
class Pos {
final int x;
final int y;
}
class Platform {
char[] arr;
int width;
int height;
Platform(List<String> input) {
arr = input.join("").toCharArray();
width = input[0].size();
height = input.size();
}
def getAt(Pos pos) {
int arrpos = pos.x + pos.y * width;
return arr[arrpos];
}
def putAt(Pos pos, char value) {
int arrpos = pos.x + pos.y * width;
arr[arrpos] = value;
}
def posOf(char c) {
return arr.findIndexValues { it == c }.collect {
new Pos(it % width as int, Math.floor(it / width) as int)
};
}
def tiltNorth() {
def roundRocks = posOf('O' as char);
for (def rock in roundRocks) {
// Move the rocks as far north as possible.
for (int i = rock.y - 1; i >= 0; i--) {
if (this[new Pos(rock.x, i)] == '.' as char) {
assert this[new Pos(rock.x, i + 1)] == 'O' as char;
this[new Pos(rock.x, i)] = 'O' as char;
this[new Pos(rock.x, i + 1)] = '.' as char;
} else {
break;
}
}
}
}
def tiltWest() {
def roundRocks = posOf('O' as char);
for (def rock in roundRocks) {
// Move the rocks as far north as possible.
for (int i = rock.x - 1; i >= 0; i--) {
if (this[new Pos(i, rock.y)] == '.' as char) {
assert this[new Pos(i + 1, rock.y)] == 'O' as char;
this[new Pos(i, rock.y)] = 'O' as char;
this[new Pos(i + 1, rock.y)] = '.' as char;
} else {
break;
}
}
}
}
def tiltSouth() {
def roundRocks = posOf('O' as char).reverse();
for (def rock in roundRocks) {
// Move the rocks as far north as possible.
for (int i = rock.y + 1; i < height; i++) {
if (this[new Pos(rock.x, i)] == '.' as char) {
assert this[new Pos(rock.x, i - 1)] == 'O' as char;
this[new Pos(rock.x, i)] = 'O' as char;
this[new Pos(rock.x, i - 1)] = '.' as char;
} else {
break;
}
}
}
}
def tiltEast() {
def roundRocks = posOf('O' as char).reverse();
for (def rock in roundRocks) {
// Move the rocks as far north as possible.
for (int i = rock.x + 1; i < width; i++) {
if (this[new Pos(i, rock.y)] == '.' as char) {
assert this[new Pos(i - 1, rock.y)] == 'O' as char;
this[new Pos(i, rock.y)] = 'O' as char;
this[new Pos(i - 1, rock.y)] = '.' as char;
} else {
break;
}
}
}
}
def tiltCycle() {
tiltNorth();
tiltWest();
tiltSouth();
tiltEast();
}
def loadSumNorth() {
def roundRocks = posOf('O' as char);
return roundRocks.collect { height - it.y }.sum();
}
String toString() {
def result = "";
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result += this[new Pos(x, y)];
}
result += "\n";
}
return result;
}
}
def input = new File("input/day14.txt").readLines();
{ // Part 1
def platform = new Platform(input);
platform.tiltNorth();
println(platform.loadSumNorth());
}
{ // Part 2
def platform = new Platform(input);
def state = [:];
def index = 0L;
def firstRepeat = null;
def totalCycles = 1000000000L;
while (index < totalCycles) {
if (state[platform.toString()] != null) {
if (firstRepeat == null) {
firstRepeat = [index, platform.toString()];
} else if (firstRepeat[1] == platform.toString()) {
def step = index - firstRepeat[0];
def skipTo = Math.floor(totalCycles / step) as long;
println("Repeat at $index, skipping to ${index + (skipTo - 3) * step}");
index += (skipTo - 3) * step;
}
}
state[platform.toString()] = platform.loadSumNorth();
platform.tiltCycle();
index++;
}
println(platform.loadSumNorth());
}