-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2022_22.java
240 lines (225 loc) Β· 9.5 KB
/
AoC2022_22.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import static com.github.pareronia.aoc.Utils.asCharacterStream;
import static com.github.pareronia.aoc.Utils.last;
import static java.util.stream.Collectors.toList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import com.github.pareronia.aoc.Grid.Cell;
import com.github.pareronia.aoc.geometry.Direction;
import com.github.pareronia.aoc.geometry.Turn;
import com.github.pareronia.aocd.Aocd;
import com.github.pareronia.aocd.Puzzle;
public class AoC2022_22 extends AoCBase {
private static final Pattern REGEX = Pattern.compile("([LR])([0-9]+)");
private static final char WALL = '#';
private final List<String> grid;
private final List<Move> moves;
private final Cell start;
private AoC2022_22(final List<String> input, final boolean debug) {
super(debug);
final List<List<String>> blocks = toBlocks(input);
this.grid = blocks.get(0);
this.moves = REGEX.matcher("R" + blocks.get(1).get(0)).results()
.map(r -> new Move(
Turn.fromString(r.group(1)), Integer.parseInt(r.group(2))))
.collect(toList());
trace(this.moves);
this.start = Cell.at(
0,
IntStream.range(0, this.grid.get(0).length())
.filter(i -> this.grid.get(0).charAt(i) != ' ')
.findFirst().orElseThrow());
}
public static final AoC2022_22 create(final List<String> input) {
return new AoC2022_22(input, false);
}
public static final AoC2022_22 createDebug(final List<String> input) {
return new AoC2022_22(input, true);
}
private int ans(final int row, final int col, final Direction facing) {
return 1000 * (row + 1)
+ 4 * (col + 1)
+ List.of(Direction.RIGHT, Direction.DOWN,
Direction.LEFT, Direction.UP).indexOf(facing);
}
@Override
public Integer solvePart1() {
int row = this.start.getRow();
int col = this.start.getCol();
trace(Cell.at(row, col));
Direction facing = Direction.UP;
final Map<Integer, List<Integer>> rowsCache = new HashMap<>();
final Map<Integer, List<Integer>> colsCache = new HashMap<>();
for (final Move move : this.moves) {
facing = facing.turn(move.turn);
for (int i = 0; i < move.steps; i++) {
final Cell moved = Cell.at(row, col).at(facing);
int rr = moved.getRow();
int cc = moved.getCol();
if (facing == Direction.UP || facing == Direction.DOWN) {
final List<Integer> rows = rowsCache.computeIfAbsent(
col ,
c -> IntStream.range(0, this.grid.size())
.filter(r -> c < this.grid.get(r).length())
.filter(r -> this.grid.get(r).charAt(c) != ' ')
.boxed().collect(toList()));
if (rr < rows.get(0)) {
rr = last(rows);
} else if (rr > last(rows)) {
rr = rows.get(0);
}
} else {
final List<Integer> cols = colsCache.computeIfAbsent(
row,
r -> IntStream.range(0, this.grid.get(r).length())
.filter(c -> this.grid.get(r).charAt(c) != ' ')
.boxed().collect(toList()));
if (cc < cols.get(0)) {
cc = last(cols);
} else if (cc > last(cols)) {
cc = cols.get(0);
}
}
if (this.grid.get(rr).charAt(cc) == WALL) {
break;
} else {
row = rr;
col = cc;
}
trace(Cell.at(row, col));
}
}
return ans(row, col, facing);
}
// cube layout: |1|2|
// |3|
// |4|5|
// |6|
@Override
public Integer solvePart2() {
final long area = this.grid.stream()
.flatMap(line -> asCharacterStream(line).filter(ch -> ch != ' '))
.count();
final int size = (int) Math.sqrt(area / 6);
int row = this.start.getRow();
int col = this.start.getCol();
Direction facing = Direction.UP;
for (final Move move : this.moves) {
facing = facing.turn(move.turn);
for (int i = 0; i < move.steps; i++) {
final Cell moved = Cell.at(row, col).at(facing);
int rr = moved.getRow();
int cc = moved.getCol();
Direction ff = facing;
if (rr < 0 && size <= cc && cc < 2 * size && ff == Direction.UP) {
// top edge 1
rr = cc + 2 * size;
cc = 0;
ff = Direction.RIGHT;
} else if (cc < size && 0 <= rr && rr < size && ff == Direction.LEFT) {
// left edge 1
rr = 3 * size - 1 - rr;
cc = 0;
ff = Direction.RIGHT;
} else if (rr < 0 && 2 * size <= cc && cc < 3 * size && ff == Direction.UP) {
// top edge 2
rr = 4 * size - 1;
cc -= 2 * size;
ff = Direction.UP;
} else if (cc == 3 * size && 0 <= rr && rr < size && ff == Direction.RIGHT) {
// right edge 2
rr = 3 * size - 1 - rr;
cc = 2 * size - 1;
ff = Direction.LEFT;
} else if (rr == size && 2 * size <= cc && cc < 3 * size && ff == Direction.DOWN) {
// bottom edge 2
rr = cc - size;
cc = 2 * size - 1;
ff = Direction.LEFT;
} else if (cc < size && size <= rr && rr < 2 * size && ff == Direction.LEFT) {
// left edge 3
cc = rr - size;
rr = 2 * size;
ff = Direction.DOWN;
} else if (cc == 2 * size && size <= rr && rr < 2 * size && ff == Direction.RIGHT) {
// right edge 3
cc = rr + size;
rr = size - 1;
ff = Direction.UP;
} else if (cc < 0 && 2 * size <= rr && rr < 3 * size && ff == Direction.LEFT) {
// left edge 4
rr = 3 * size - 1 - rr;
cc = size;
ff = Direction.RIGHT;
} else if (rr < 2 * size && 0 <= cc && cc < size && ff == Direction.UP) {
// top edge 4
rr = cc + size;
cc = size;
ff = Direction.RIGHT;
} else if (cc == 2 * size && 2 * size <= rr && rr < 3 * size && ff == Direction.RIGHT) {
// right edge 5
rr = 3 * size - 1 - rr;
cc = 3 * size - 1;
ff = Direction.LEFT;
} else if (rr == 3 * size && size <= cc && cc < 2 * size && ff == Direction.DOWN) {
// bottom edge 5
rr = 2 * size + cc;
cc = size - 1;
ff = Direction.LEFT;
} else if (cc < 0 && 3 * size <= rr && rr < 4 * size && ff == Direction.LEFT) {
// left edge 6
cc = rr - 2 * size;
rr = 0;
ff = Direction.DOWN;
} else if (cc == size && 3 * size <= rr && rr < 4 * size && ff == Direction.RIGHT) {
// right edge 6
cc = rr - 2 * size;
rr = 3 * size - 1;
ff = Direction.UP;
} else if (rr == 4 * size && 0 <= cc && cc < size && ff == Direction.DOWN) {
// bottom edge 6
rr = 0;
cc += 2 * size;
ff = Direction.DOWN;
}
if (this.grid.get(rr).charAt(cc) == WALL) {
break;
} else {
row = rr;
col = cc;
facing = ff;
}
}
}
return ans(row, col, facing);
}
public static void main(final String[] args) throws Exception {
assert AoC2022_22.createDebug(TEST).solvePart1() == 6032;
// assert AoC2022_22.createDebug(TEST).solvePart2() == 0;
final Puzzle puzzle = Aocd.puzzle(2022, 22);
final List<String> inputData = puzzle.getInputData();
puzzle.check(
() -> lap("Part 1", AoC2022_22.create(inputData)::solvePart1),
() -> lap("Part 2", AoC2022_22.create(inputData)::solvePart2)
);
}
private static final List<String> TEST = splitLines(
" ...#\r\n" +
" .#..\r\n" +
" #...\r\n" +
" ....\r\n" +
"...#.......#\r\n" +
"........#...\r\n" +
"..#....#....\r\n" +
"..........#.\r\n" +
" ...#....\r\n" +
" .....#..\r\n" +
" .#......\r\n" +
" ......#.\r\n" +
"\r\n" +
"10R5L5R10L4R5L5"
);
record Move(Turn turn, int steps) {}
}