-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDay15.java
69 lines (61 loc) · 2.38 KB
/
Day15.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
package com.sbaars.adventofcode.year24.days;
import static com.sbaars.adventofcode.common.Direction.EAST;
import static com.sbaars.adventofcode.common.Direction.WEST;
import static com.sbaars.adventofcode.util.AoCUtils.alternating;
import static com.sbaars.adventofcode.util.AoCUtils.recurse;
import static java.util.stream.Stream.concat;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.common.grid.InfiniteGrid;
import com.sbaars.adventofcode.common.location.Loc;
import com.sbaars.adventofcode.year24.Day2024;
public class Day15 extends Day2024 {
public Day15() {
super(15);
}
public static void main(String[] args) {
new Day15().printParts();
}
@Override
public Object part1() {
String in = day();
String[] parts = in.split("\n\n");
InfiniteGrid g = new InfiniteGrid(parts[0]);
return solve(g, parts[1]);
}
@Override
public Object part2() {
String in = day();
String[] parts = in.split("\n\n");
InfiniteGrid g = new InfiniteGrid(parts[0]).zoom(2, 1);
g.findAll('@').skip(1).forEach(l -> g.set(l, '.'));
alternating(g.findAll('O').sorted((l1, l2) -> l1.intY() - l2.intY()), l -> g.set(l, '['), l -> g.set(l, ']'));
return solve(g, parts[1]);
}
public static InfiniteGrid extractComponent(InfiniteGrid g, Loc startLoc, Direction initialDirection) {
return recurse(new InfiniteGrid(), startLoc, (component, stack, currentLoc) -> {
if (g.get(currentLoc).filter(c -> c == '#' || c == '.').isPresent()) return component;
char ch = g.set(currentLoc, '.');
component.set(currentLoc, ch);
stack.add(currentLoc.move(initialDirection));
if (ch == '[') {
stack.add(currentLoc.move(EAST));
} else if (ch == ']') {
stack.add(currentLoc.move(WEST));
}
return component;
});
}
public static long solve(InfiniteGrid g, String moves) {
Loc robotPos = g.find('@');
for (char move : moves.replace("\n", "").toCharArray()) {
Direction d = Direction.caretToDirection(move);
InfiniteGrid component = extractComponent(g, robotPos, d);
if (component.stream().allMatch(loc -> g.get(loc.move(d)).filter(c -> c == '.').isPresent())) {
component = component.move(d);
robotPos = robotPos.move(d);
}
g.place(component);
}
return concat(g.findAll('O'), g.findAll('[')).mapToLong(l -> l.y * 100 + l.x).sum();
}
}